in reply to ASCII to HTML

I'm not sure why that's not working for you, actually... it looks like it would, from what I can tell, and from the little test I did.

But anyway, depending on how complicated your text is, you may need something more powerful than just a regex. Take a look at HTML::FromText, which formats your text into HTML. It can handle a lot more formatting issues than just paragraphs.

use HTML::FromText; my $str = <<TEXT; Foo is on this line, and bar is in this paragraph. Baz is in a new paragraph. TEXT print text2html($str, paras => 1);
The result:
<P>Foo is on this line, and bar is in this paragraph.</P> <P>Baz is in a new paragraph.</P>
If you decide to go with a regex, I've always just used
$str =~ s/\n\n/<p>\n\n/g; $str =~ s/\n/<br>\n/g;
which first replaces double-newlines and makes them paragraphs, and then formats line breaks. Plus it keeps the newlines there as a visual distinction, in case anyone actually needs to *read* the HTML. :)

Replies are listed 'Best First'.
RE: Re: ASCII to HTML
by chromatic (Archbishop) on Apr 05, 2000 at 02:12 UTC
    Whoa! That second regex will Do Weird Things after the first. (It picks up the newlines after the fresh

    tag). Here's another approach:

    my $str = TEXT; Foo is on this line, and bar is in this paragraph. Baz is in a new paragraph. TEXT my @para = split(/\n\n/, $str); s!\n!<br>\n! foreach @para; $str = join "\n<p>\n", @para; print ">>$str<<\n";
    For extra credit, put that in a one-liner. *sigh*
      Oops oops oops. Thanks for catching that--I guess I must not always use that regex. :)

      But here's your one-liner:

      $str = join "\n<p>\n", grep s/\n/<br>\n/g || 1, split /\n\n/, $str;
      (The "|| 1" in there makes it so that even paragraphs that don't contain any carriage returns inside of them, and thus don't match in the substitution, still get included in the final list of paragraphs.)
Re: Re: ASCII to HTML
by tecdady (Initiate) on Jun 19, 2002 at 00:56 UTC
    i to have been using this: $str =~ s/\n\n/

    /g; to format text going into a flat file db. obviously to keep a new line from being written to the db that would cause the entry to skip. but strange thing, it keeps the entry from going to a new line like i wanted yet it puts a "square (new line character)" before the "

    " in the entry. for the life of me, i cant figure out why. any suggestions? thanks much. tdidy