summaryrefslogtreecommitdiff
path: root/lib/VNDB
diff options
context:
space:
mode:
Diffstat (limited to 'lib/VNDB')
-rw-r--r--lib/VNDB/Func.pm168
1 files changed, 65 insertions, 103 deletions
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 .= '</a>';
- $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/&/&amp;/g;
+ s/>/&gt;/g;
+ s/</&lt;/g;
+ s/\n/<br \/>/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 .= '</a>';
+ pop @open;
+ }
next;
- }
- # Process [url=.+] tags
- if (s/\[url=((https?:\/\/|\/)[^\]>]+)\]/<a href="$1" rel="nofollow">/i)
- {
+ } elsif(s{\[url=((https?://|/)[^\]>]+)\]}{<a href="$1" rel="nofollow">}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])?'':'<br />');
- }
- elsif (!$inRaw && !$inUrl && s/(http|https):\/\/(.+[0-9a-zA-Z=\/])/<a href="$1:\/\/$2" rel="nofollow">/)
- {
- # Parse automatic links
- $length += 4;
- if ($length <= $maxLength)
- {
- $lit = 'link';
-
- # ROT-13 of 'link'
- $lit = 'yvax' if $inSpoiler;
-
- $result .= $_ . $lit . '</a>';
- }
- }
- elsif (!$inRaw && !$inUrl && (s/^(.*[^\w]|)([tdvpr][1-9][0-9]*)\.([1-9][0-9]*)([^\w].*|)$/"$1<a href=\"\/$2.$3\">". ($inSpoiler?rot13("$2.$3"):"$2.$3") ."<\/a>$4"/e ||
- s/^(.*[^\w]|)([tduvpr][1-9][0-9]*)([^\w].*|)$/"$1<a href=\"\/$2\">". ($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|<a href="$2://|.$e->($3, 1).'" rel="nofollow">'.$e->('link').'</a>'.$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/\&/&amp;/g;
- s/>/&gt;/g;
- s/</&lt;/g;
-
+ next;
+ } elsif(!grep(/url/, @open) && (
+ s{^(.*[^\w]|)([tdvpr][1-9][0-9]*)\.([1-9][0-9]*)([^\w].*|)$}{$e->($1).qq|<a href="/$2.$3">$2.$3</a>|.$e->($4)}e ||
+ s{^(.*[^\w]|)([tduvpr][1-9][0-9]*)([^\w].*|)$}{$e->($1).qq|<a href="/$2">$2</a>|.$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 .= '</a>' if $inUrl;
+ $result .= '</a>'
+ 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;