HTML Paragraphs from Text

Discussion in 'Perl Misc' started by poopdeville@gmail.com, Dec 24, 2005.

  1. Guest

    Hello,

    I'm working on a project, and I'm having a little bit of trouble.
    Basically, what I'm trying to do is take some text as input, split
    along groups of newlines (so that multiple newlines in a row have the
    same effect as a single one), and make each of the elements of the new
    array an HTML paragraph. Here is the relevant subroutine:

    #!/usr/bin/perl -w
    #use strict;

    #sub paragrapher {
    # my @paragraphs = split /\n+/, $_[0];
    # my $new;
    # for (@paragraphs) {
    # if (length($_)) {
    # $new = $new . "<p>$_<\/p>";
    # }
    # }
    # return $new;
    #}

    If we feed the subroutine the text:

    This is the super dooper intro.

    OMG, this is more of a test than an intro, but whatever.

    stored in a variable, the output is

    <p>This is the super dooper intro.</p><p>
    </p><p>
    OMG, this is more of a test than an intro, but whatever.</p>

    Note the totally unnecessary <p></p>. Does anyone see what could be
    going wrong?

    Thanks,
    'cid 'ooh
     
    , Dec 24, 2005
    #1
    1. Advertising

  2. Guest

    wrote:

    > Note the totally unnecessary <p></p>. Does anyone see what could be
    > going wrong?


    I'd rather not shift through the code you posted, but I can offer an
    alternative approach that may work better for you (see also perldoc -q
    "more than one line"):

    #!/usr/bin/perl
    use strict; #use warnings;

    $/ = ''; # read in more whole paragraph, not just one line
    while ( <DATA> ) {chomp;
    print "<p>$_</p>\n";
    }

    __DATA__
    This is the super dooper intro.

    this is more of a test than an intro, but whatever.


    more stuff


    ########## OUTPUT ###############3
    <p>This is the super dooper intro.</p>
    <p>this is more of a test than an intro, but whatever. </p>
    <p>more stuff</p>
     
    , Dec 24, 2005
    #2
    1. Advertising

  3. wrote:
    > Basically, what I'm trying to do is take some text as input, split
    > along groups of newlines (so that multiple newlines in a row have the
    > same effect as a single one), and make each of the elements of the new
    > array an HTML paragraph. Here is the relevant subroutine:
    >
    > #!/usr/bin/perl -w
    > #use strict;
    >
    > #sub paragrapher {
    > # my @paragraphs = split /\n+/, $_[0];


    Even if David posted a more elegant way to do it, this might make a
    difference:

    my @paragraphs = split /\s*\n\s*/, $_[0];

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 24, 2005
    #3
  4. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > Hello,
    >
    > I'm working on a project, and I'm having a little bit of trouble.
    > Basically, what I'm trying to do is take some text as input, split
    > along groups of newlines (so that multiple newlines in a row have the
    > same effect as a single one), and make each of the elements of the new
    > array an HTML paragraph. Here is the relevant subroutine:
    >
    > #!/usr/bin/perl -w
    > #use strict;
    >
    > #sub paragrapher {
    > # my @paragraphs = split /\n+/, $_[0];
    > # my $new;
    > # for (@paragraphs) {
    > # if (length($_)) {


    The test for length is unnecessary. split( /\n+/, ...) can never return
    an empty element. /\n+/ would have to match in the middle of a series
    of line feeds, which it can't.

    > # $new = $new . "<p>$_<\/p>";


    Better written as

    $new .= "<p>$_<\/p>";

    > # }
    > # }
    > # return $new;
    > #}
    >
    > If we feed the subroutine the text:
    >
    > This is the super dooper intro.
    >
    > OMG, this is more of a test than an intro, but whatever.
    >
    > stored in a variable, the output is


    Where does the string begin and end? Don't specify string data in prose,
    it's ambiguous. Use Perl code. I'm assuming

    my $str = <<EOF;
    this is the super dooper intro.

    OMG, this is more of a test than an intro, but whatever.
    EOF

    > <p>This is the super dooper intro.</p><p>
    > </p><p>
    > OMG, this is more of a test than an intro, but whatever.</p>
    >
    > Note the totally unnecessary <p></p>. Does anyone see what could be
    > going wrong?


    What are you talking about? For me the output is (all on one line)

    <p>This is the super dooper intro.</p><p>OMG, this is more of a test
    than an intro, but whatever.</p>

    If you add a line feed to each paragraph for readability, as in

    sub paragrapher {
    join '', map "<p>$_</p>\n", split( /\n+/, shift);
    }

    the output becomes

    <p>This is the super dooper intro.</p>
    <p>OMG, this is more of a test than an intro, but whatever.</p>

    How is that not what you want?

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Dec 24, 2005
    #4
  5. <> wrote:


    > Basically, what I'm trying to do is take some text as input, split
    > along groups of newlines (so that multiple newlines in a row have the
    > same effect as a single one), and make each of the elements of the new
    > array an HTML paragraph. Here is the relevant subroutine:
    >
    > #!/usr/bin/perl -w
    > #use strict;
    >
    > #sub paragrapher {
    > # my @paragraphs = split /\n+/, $_[0];
    > # my $new;
    > # for (@paragraphs) {
    > # if (length($_)) {
    > # $new = $new . "<p>$_<\/p>";
    > # }
    > # }
    > # return $new;
    > #}



    There _is no_ subroutine there, only a slew of comments.

    You have been here long enough to have seen the Posting
    Guidelines by now.

    If you post a short and complete program that we can run,
    then we can surely help you fix it.

    But you didn't, so we can't.


    > If we feed the subroutine the text:
    >
    > This is the super dooper intro.
    >
    > OMG, this is more of a test than an intro, but whatever.
    >
    > stored in a variable,



    Please speak Perl whenever possible.

    In your prose, the variable many contain anywhere from 3 to 5
    newlines in it, since the start and end of the string are
    not clearly delimited.

    We need the real data if we are to provide a real answer.


    > the output is
    >
    ><p>This is the super dooper intro.</p><p>
    ></p><p>
    > OMG, this is more of a test than an intro, but whatever.</p>



    Not when I do it in a short and complete program:

    -------------------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    my $str =<<ENDSTR;
    This is the super dooper intro.

    OMG, this is more of a test than an intro, but whatever.
    ENDSTR

    print paragrapher($str);


    sub paragrapher {
    my @paragraphs = split /\n+/, $_[0];
    my $new;
    for (@paragraphs) {
    if (length($_)) {
    $new = $new . "<p>$_<\/p>";
    }
    }
    return $new;
    }
    -------------------------------------



    > Does anyone see what could be
    > going wrong?



    Nope.

    Could it be that the code you have shown is not the code that
    is producing that output?

    Your output could be easily explained if there were 2 more
    characters than what you have shown us:

    my @paragraphs = split /(\n+)/, $_[0];

    Do you have parenthesis in your split pattern?

    I sure hope not...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 24, 2005
    #5
  6. Anno Siegel Guest

    Tad McClellan <> wrote in comp.lang.perl.misc:
    > <> wrote:


    [lots]

    > Your output could be easily explained if there were 2 more
    > characters than what you have shown us:
    >
    > my @paragraphs = split /(\n+)/, $_[0];
    >
    > Do you have parenthesis in your split pattern?


    Very clever guess. It may not be clairvoyance, but can I call you Sherlock?

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Dec 24, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rlrcstr

    Collapsable paragraphs...

    Rlrcstr, May 16, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    767
    Rlrcstr
    May 17, 2005
  2. jersie0
    Replies:
    0
    Views:
    697
    jersie0
    Nov 23, 2003
  3. Jeff
    Replies:
    3
    Views:
    9,231
    kleinbaas
    Apr 13, 2010
  4. City Dweller

    Default leading for paragraphs

    City Dweller, Apr 6, 2006, in forum: HTML
    Replies:
    13
    Views:
    1,132
    Nije Nego
    Apr 8, 2006
  5. alfonsobaldaserra

    removing paragraphs from text files

    alfonsobaldaserra, Jul 13, 2009, in forum: Perl Misc
    Replies:
    7
    Views:
    98
Loading...

Share This Page