Home: Perl Programming Help: Regular Expressions:
HTML code generator



inlimbo
User

Jan 29, 2005, 6:50 PM


Views: 7548
HTML code generator

I want to create a little script that adds the <p>,</p> and <br> tags to some text. i.e. If I have the following text:

my $text = "This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1.

This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2.
This is a new line.
This is a new line.
This is a new line.
This is a new line.";

I want it to come out as:


Code
my $text = "<p>This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This 
is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1.
This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph
1. This is paragraph 1.</p>
<p>This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph
2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is
paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This
is paragraph 2. This is paragraph 2. This is paragraph 2.<br>
This is a new line.<br>
This is a new line.<br>
This is a new line.<br>
This is a new line.</p>"


ie. just put the <p> and </p> at the beginning and end of the paragraph and put a <br> tag if there is a new line.

Thanx


davorg
Thaumaturge / Moderator

Feb 2, 2005, 2:01 AM


Views: 7525
Re: [inlimbo] HTML code generator

This seems to do the job on your text data.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $text = 'This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1. This is paragraph 1.

This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2. This is paragraph 2.
This is a new line.
This is a new line.
This is a new line.
This is a new line.';

# Add <p> and </p> to start and end of text
$text = "<p>$text</p>";

# Convert double newlines to </p>\n\n</p>
$text =~ s|\n\n|</p>\n\n<p>|g;

# Add a <br /> at the end of any non-blank line that doesn't
# already end with </p>
$text =~ s|\S(?<!</p>)\n|<br />\n|g;

print $text;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks