Page 1 of 2

<p>All work and no play makes Jack a dull boy</p> !(=^..^=)!

Posted: Wed Jul 06, 2016 3:19 pm
by Mariasole
Dear friends more experienced, I have another question to ask yourself.
I have to give a format to a long string of fairy tales...

My text is so:

Code: Select all

<p>All work and 
no play 
makes Jack a 
dull boy
All work and no play makes 
Jack a dull boy
All work and no play makes 
Jack a dull boy
</p>

I would replace returns with space, from <p> to </p> with "replaceText" and make it so:

Code: Select all

<p>All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy </p>

I tried this solution I found here (http://stackoverflow.com/questions/1605 ... een-p-tags):

Code: Select all

[\r\n]+(?=(?:[^<]+|<(?!/?p\b))*</p>)
then...

Code: Select all

put replaceText(tFairy,"[\r\n]+(?=(?:[^<]+|<(?!/?p\b))*</p>)"," ")  into tFairy
But it does not work! :shock: I think it "regex" and not "PCRE" although I would not say nonsense! :?

Can you help me? :roll:

Thank you so much for the support given to us beginners!
Never lose patience with our stupid demands!
We are learning! Sooner or later we will learn!
And when we are good we will help beginners, based on your generosity and your example!

Thank you all!

Mariasole
(=^..^=)

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Wed Jul 06, 2016 3:53 pm
by dunbarx
Maria.

Why not simply:

Code: Select all

replace return with space in yourContainer

Craig Newman

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Wed Jul 06, 2016 4:08 pm
by Mariasole
Hi Craig!
I wish it were that simple! :oops:
Unfortunately my stories are in HTML, and to me it is mainly that between <p> and </p> tags the returns are replaced with a space. Other tags must remain intact with their respective return!
Thank you for your reply!

Mariasole
(=^..^=)

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Wed Jul 06, 2016 5:18 pm
by [-hh]
Works with LC 7 and later.

Code: Select all

on mouseUp
  put fld "IN" into txt
  set linedel to "<p>"
  set itemdel to "</p>"
  repeat with i=2 to the num of lines of txt
    replace cr with space in item 1 of line i of txt
  end repeat
  put txt into fld "OUT"
end mouseUp

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Wed Jul 06, 2016 5:48 pm
by mwieder
Herman is much too fast for me. I was still trying to figure out an elegant solution for this.

"...in item 1 of"... brilliant.

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Wed Jul 06, 2016 5:58 pm
by Mariasole
Dear [-hh], thanks!

Wow, really an elegant and good solution! :D
But unfortunately I still working on the 6.7.7 version of LC!
I think that I need a PCRE regex solution... :?

put replaceText(tFairy,"[\r\n]+(?=(?:[^<]+|<(?!/?p\b))*</p>)"," ") into tFairy


Thierry, are you there? :wink:


Grazie ancora a tutti...

Mariasole
(=^..^=)

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Wed Jul 06, 2016 6:29 pm
by [-hh]
Why use for simple things a regex that nobody than Thierry can really handle?
You may use one-char delimiters with LC 6.

Code: Select all

on mouseUp
  put fld "IN" into txt
  replace "<p>" with numToChar(1) in txt
  replace "</p>" with numToChar(2) in txt
  set linedel to numToChar(1)
  set itemdel to numToChar(2)
  repeat with i=2 to the num of lines of txt
    replace cr with space in item 1 of line i of txt --> see ***
  end repeat
  replace numToChar(1) with "<p>" in txt
  replace numToChar(2) with "</p>" in txt
  put txt into fld "OUT"
end mouseUp
*** As Craig said:

Code: Select all

replace return with space in yourContainer
We only had to fix the yourContainer :-)

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Wed Jul 06, 2016 6:46 pm
by Mariasole
Dear [-hh],
thanks to downgrade your solution! :D
I now will test and let you know! 8)

You're a knight [-hh], you are saving a damsel!

Mariasole
(=^..^=)

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Wed Jul 06, 2016 6:52 pm
by [-hh]
Mariasole,
I don't look at gender. It's just because recently Germany had a bit more luck than Italy with penalties.

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Wed Jul 06, 2016 8:19 pm
by Mariasole
I did not know that Italy had a football team! :wink:

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Thu Jul 07, 2016 2:44 pm
by Mariasole
Dear [-hh],

I did the test on your nice solution, but unfortunately I found some problems! :shock:

First problem: the script changes the formatting of my html, which instead must remain intact. :oops:

original to change:

Code: Select all

<span>this is a span tag</span>
<span>this is another span tag</span>
<p>All work and
no play
makes Jack a
dull boy
All work and no play makes
Jack a dull boy
All work and no play makes
Jack a dull boy
</p>
<span>this is the end span tag</span>

result with your script:

Code: Select all

<span>this is a span tag</span> <span>this is another span tag</span> <p>All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy </p>
<span>this is the end span tag</span>

...then what I would like:

Code: Select all

<span>this is a span tag</span>
<span>this is another span tag</span>
<p>All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy </p>
<span>this is the end span tag</span>


Second problem: When html lines are many, the system is very slow ... :?


So, probably, the solution with regex might be the best, just that I have no idea how to spell !!! :cry:
put replaceText(tFairy,"[\r\n]+(?=(?:[^<]+|<(?!/?p\b))*</p>)"," ") into tFairy
????????????

I put an X on the window and turned on the light, let's see if PERLman will find a solution ... :roll:

Grazie [-hh], grazie a tutti!
Mariasole
(=^..^=)

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Thu Jul 07, 2016 3:44 pm
by FourthWorld
The presence or absence of white space between HTML tags does not affect how it renders. What is the goal of this exercise?

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Thu Jul 07, 2016 5:31 pm
by [-hh]
Mariasole,
this was a beginners error (sorry, corrected above):
Of course the repeat should start with i=2, because the first "<p>"-line is *before* the first "<p>".

Hope it works now as you wish.
Here is moreover a faster solution if your input is large, use with LC 6.
Needs here (2.5 GHz Mac mini) < 500 ms for 2 MByte of input.

Code: Select all

--- For LC 6 (easier but slower with LC 7/8)
on mouseUp
  lock screen; lock messages
  put the millisecs into m1
  put fld "IN" into txt; put the num of lines of txt into n1
  put numToChar(1) into c1
  put numToChar(2) into c2
  replace "<p>" with c1 in txt
  replace "</p>" with c2 in txt
  set linedel to c1; set itemdel to c2
  put line 1 of txt into txt2 --> we start AFTER first "<p>"
  repeat for each line L in (line 2 to -1 of txt)
    replace cr with space in item 1 of L
    replace c2 with "</p>" in L
    put "<p>" & L after txt2
  end repeat
  put txt2 into fld "OUT"
  set linedel to cr
  put length(txt2) & " / replaced " & (n1-the num of lines of txt2) & \
         " / " & (the millisecs - m1) & " ms" into fld "timing"
  unlock screen; unlock messages
end mouseUp

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Fri Jul 08, 2016 11:18 am
by Mariasole
FourthWorld wrote:The presence or absence of white space between HTML tags does not affect how it renders. What is the goal of this exercise?
Hi Richard!
It is not a Feng Shui test code :D , but rather for the readability of the code. I know it sounds strange, but for my "biggest experiment" :!: , that of fables that I mentioned a while ago, I want to keep the readability of sources in html!

Grazie!
!(=^..^=)!

Re: <p>All work and no play makes Jack a dull boy</p> !(=^..

Posted: Fri Jul 08, 2016 11:23 am
by Mariasole
Grazie [-hh]! :D
the new code works perfectly and is very fast! I do not understand the differences with LC7 and LC8 :roll: , but maybe I will understand when I pass to new versions.
For now 6.x is hard enough for me! Thank you so very much for giving me the solution, and for showing me the very elegant code that I can study and imitate! 8)
I will continue to test the code, and if you had something wrong I'll let you know.

Thanks again!

Mariasole
(=^..^=)