Pikachu
Kelp is good! Yum yum!
- Pronoun
- he
I've decide to code my own BBCode parser in Perl:
It works fine but when translated to HTML, it isn't parsed correctly. For example, [b][i]bold[/b][/i] becomes <b><i>bold</b></i>. How would I write the script so that it parses correctly in HTML?
Code:
#!/usr/bin/perl
use strict;
use CGI qw(:standard);
our $query = new CGI;
our $_ = $query->param('text');
print "Content-Type: text/html\n\n";
s@\[b\](.*?)\[/b\]@<b>$1</b>@ig;
s@\[i\](.*?)\[/i\]@<i>$1</i>@ig;
s@\[u\](.*?)\[/u\]@<u>$1</u>@ig;
s@\[big\](.*?)\[/img\]@<span style='font-size: large'>$1</span>@ig;
s@\[url=((?![^:]+script).*?)\](.*?)\[/url\]@<a href="$1">$2</a>@ig;
s@\[url\]((?![^:]+script).*?)\[/url\]@<a href="$1">$1</a>@ig;
s@\[img\]((?![^:]+script).*?)\[/img\]@<img src="$1" alt="[IMAGE]" />@ig;
s@\[center\](.*?)\[/center\]@<p style="text-align:center">$1</p>@ig;
s@\[left\](.*?)\[/left\]@<p style="text-align:left">$1</p>@ig;
print "<form action='' method='post'><textarea name='text'></textarea><input type='submit' value='submit' /></form>";
print $_;