Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > HTML Paragraphs from Text

Reply
Thread Tools

HTML Paragraphs from Text

 
 
poopdeville@gmail.com
Guest
Posts: n/a
 
      12-24-2005
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

 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      12-24-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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>

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-24-2005
(E-Mail Removed) 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-24-2005
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-24-2005
(E-Mail Removed) <(E-Mail Removed)> 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
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-24-2005
Tad McClellan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed) <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie: css & "removing blank line between paragraphs" Jeff HTML 3 04-13-2010 09:27 AM
removing paragraphs from text files alfonsobaldaserra Perl Misc 7 07-15-2009 06:35 PM
Default leading for paragraphs City Dweller HTML 13 04-08-2006 10:37 AM
Collapsable paragraphs... Rlrcstr ASP .Net 7 05-17-2005 12:34 AM
"left floated" paragraphs and images using CSS? jersie0 HTML 0 11-23-2003 02:43 AM



Advertisments