From 464037b2c29b37ce95d82c5cdefef90a2d31d0c3 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Fri, 14 Nov 2008 21:21:01 +0100 Subject: Rewrote & fixed bb2html() The general concept and method is really the same, this version just does a few things different to ensure correct parsing and HTML output. --- lib/VNDB/Func.pm | 168 +++++++++++++++++++++---------------------------------- 1 file changed, 65 insertions(+), 103 deletions(-) (limited to 'lib/VNDB') diff --git a/lib/VNDB/Func.pm b/lib/VNDB/Func.pm index 92f31616..bc6cbc87 100644 --- a/lib/VNDB/Func.pm +++ b/lib/VNDB/Func.pm @@ -45,120 +45,82 @@ sub datestr { } -# Parses BBCode-enhanced strings into the correct HTML variant with an optional maximum length -sub bb2html { # input, length - return $_[2] || ' ' if !$_[0]; # No clue what this is for, but it is included from the previous verison for posterity... - my $raw = $_[0]; - my $maxLength; - if (defined($_[1])) {$maxLength = $_[1]} - else {$maxLength = length $_[0]} - - my $result = ''; - my $length = 0; - my $inRaw = 0; - my $inSpoiler = 0; - my $inUrl = 0; - - # Split the input string into segments - foreach (split /(\s|\[.+?\])/, $raw) - { - if (!defined($_)) {next} - - if (!$inRaw) - { - # Cases for BBCode tags - if ($_ eq '[raw]') {$inRaw = 1; next} - elsif ($_ eq '[spoiler]') {$inSpoiler = 1; next} - elsif ($_ eq '[/spoiler]') {$inSpoiler = 0; next} - elsif ($_ eq '[/url]') - { - $result .= ''; - $inUrl = 0; +# Arguments: input, and optionally the maximum length +# Parses: +# [url=..] [/url] +# [raw] .. [/raw] +# [spoiler] .. [/spoiler] +# v+, v+.+ +# http://../ +sub bb2html { + my $raw = shift; + my $maxlength = shift; + $raw =~ s/\r//g; + return '' if !$raw && $raw != 0; + + my($result, $length, @open) = ('', 0, 'first'); + + my $e = sub { + local $_ = shift; + tr/A-Za-z/N-ZA-Mn-za-m/ if !@_ && grep /spoiler/, @open; + s/&/&/g; + s/>/>/g; + s//g if !$maxlength; + s/\n/ /g if $maxlength; + return $_; + }; + + for (split /(\s|\n|\[[^\]]+\])/, $raw) { + next if !defined $_; + + my $lit = $_; + if($open[$#open] ne 'raw') { + if ($_ eq '[raw]') { push @open, 'raw'; next } + elsif ($_ eq '[spoiler]') { push @open, 'spoiler'; next } + elsif ($_ eq '[/spoiler]') { pop @open if $open[$#open] eq 'spoiler'; next } + elsif ($_ eq '[/url]') { + if($open[$#open] eq 'url') { + $result .= ''; + pop @open; + } next; - } - # Process [url=.+] tags - if (s/\[url=((https?:\/\/|\/)[^\]>]+)\]//i) - { + } elsif(s{\[url=((https?://|/)[^\]>]+)\]}{}i) { $result .= $_; - $inUrl = 1; + push @open, 'url'; next; - } - } - - my $lit = $_; # Literal version of the segment to refence in case we link-ify the original - - if ($_ eq '[/raw]') - { - # Special case for leaving raw mode - $inRaw = 0; - } - elsif ($_ =~ m/\n/) - { - # Parse line breaks - $result .= (defined($_[1])?'':'
'); - } - elsif (!$inRaw && !$inUrl && s/(http|https):\/\/(.+[0-9a-zA-Z=\/])/
/) - { - # Parse automatic links - $length += 4; - if ($length <= $maxLength) - { - $lit = 'link'; - - # ROT-13 of 'link' - $lit = 'yvax' if $inSpoiler; - - $result .= $_ . $lit . ''; - } - } - elsif (!$inRaw && !$inUrl && (s/^(.*[^\w]|)([tdvpr][1-9][0-9]*)\.([1-9][0-9]*)([^\w].*|)$/"$1". ($inSpoiler?rot13("$2.$3"):"$2.$3") ."<\/a>$4"/e || - s/^(.*[^\w]|)([tduvpr][1-9][0-9]*)([^\w].*|)$/"$1". ($inSpoiler?rot13($2):$2) ."<\/a>$3"/e)) - { - # Parse VNDBID - $length += length $lit; - if ($length <= $maxLength) - { + } elsif(!grep(/url/, @open) && + s{(.*)(http|https)://(.+[0-9a-zA-Z=/])(.*)} + {$e->($1).qq|'.$e->('link').''.$e->($4)}e) { + $length += 4; + last if $maxlength && $length > $maxlength; $result .= $_; - } - } - else - { - # Normal text processing - $length += length $_; - if ($length <= $maxLength) - { - # ROT-13 - tr/A-Za-z/N-ZA-Mn-za-m/ if $inSpoiler; - - # Character escaping - s/\&/&/g; - s/>/>/g; - s/($1).qq|$2.$3|.$e->($4)}e || + s{^(.*[^\w]|)([tduvpr][1-9][0-9]*)([^\w].*|)$}{$e->($1).qq|$2|.$e->($3)}e)) { + $length += length $lit; + last if $maxlength && $length > $maxlength; $result .= $_; + next; } - } - - # End if we've reached the maximum length/string end - if ($length >= $maxLength) - { - # Tidy up the end of the string - $result =~ s/\s+$//; - $result .= '...'; - last; - } + } elsif($_ eq '[/raw]') { + pop @open if $open[$#open] eq 'raw'; + next; + } + + # normal text processing + $length += length $_; + last if $maxlength && $length > $maxlength; + $result .= $e->($_); } - # Close any un-terminated url tags - $result .= '' if $inUrl; + $result .= '' + while((local $_ = pop @open) ne 'first'); + $result .= '...' if $maxlength && $length > $maxlength; return $result; } - -# Performs a ROT-13 cypher (used by bb2html) -sub rot13 { - return tr/A-Za-z/N-ZA-Mn-za-m/; -} 1; -- cgit v1.2.3