While spreading <br> on occasional places during writing here in the textarea it came to my mind
that it could be awfully simple:
$ perl -e '$_=<>;s/(.{75}.+?\s)/$1<br>/gs;print$/x3;print$_,$/x3'


Replies are listed 'Best First'.
Re: posting text alignmen
by davidrw (Prior) on Sep 03, 2005 at 13:36 UTC
    Why do you want to force line wrapping of text w/<br>'s here on PM? It wraps text nicely already by letting it get handled as HTML, and code is wrapped at a user-defined line length...

    What's an example of a node or two where you find this useful?

    couple TWTOWTDI code comments:
    • The print might as well just be print $/x3, $_, $/x3 (for fun, print join $_, ($/x3)x2; works too).
    • The regex can be simplified a little (though i found your way clear as well) to be .{75,}?\s
    • also w/the regex, i think that, since this is HTML, the \s should be outside of the capture, so the BR actually replaces the whitespace character.
    • summary: perl -e '$_=<>;s/(.{75,}?)\s/$1<br>/gs;print$/x3,$_,$/x3'
      You are right in case that lines are over the max chars.

      without alignment:
      some text here then another paragraph and finally a long line with spaces ............. .......... ...... ... .. .

      with alignment:
      some text here
      then another paragraph
      and finally a long line with spaces ............. .......... ..... .
      ... .. .


      I guess the code should be sm.th. like:
      perl -e '$SIG{INT}=sub{print$/x2,"@__"};while(<>){s/(.{75,}?)\s|\n/$1< +br>/;push@__,$_}'

      Update: I do not guess, I am sure.


        While this could be useful at times, I think, I am confused as to what you are ultimately suggesting. Are you using <br>'s to break paragraphs? I personally use <p> to break paragraphs...

        If, on the other hand, you're suggesting to use the breaks on lines within a paragraph, and I think you are, then I completely agree with davidrw that how a paragraph wraps should be left upto the person viewing it. I have a large-ish screen, I thank you for allowing me to use the available space and set the width that I like, and not force your paragraphs to wrap sooner; unless you have good reason to do so.

        -Scott

        Update: fixed some grammer issues...