diff options
Diffstat (limited to 'lib/VNDB/Func.pm')
-rw-r--r-- | lib/VNDB/Func.pm | 120 |
1 files changed, 119 insertions, 1 deletions
diff --git a/lib/VNDB/Func.pm b/lib/VNDB/Func.pm index 0bfd58de..92f31616 100644 --- a/lib/VNDB/Func.pm +++ b/lib/VNDB/Func.pm @@ -5,7 +5,7 @@ use strict; use warnings; use Exporter 'import'; use POSIX 'strftime'; -our @EXPORT = qw| shorten date datestr |; +our @EXPORT = qw| shorten date datestr bb2html |; # I would've done this as a #define if this was C... @@ -45,3 +45,121 @@ 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; + next; + } + # Process [url=.+] tags + if (s/\[url=((https?:\/\/|\/)[^\]>]+)\]/<a href="$1" rel="nofollow">/i) + { + $result .= $_; + $inUrl = 1; + 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) + { + $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/</</g; + + $result .= $_; + } + } + + # 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; + } + } + + # Close any un-terminated url tags + $result .= '</a>' if $inUrl; + + return $result; +} + +# Performs a ROT-13 cypher (used by bb2html) +sub rot13 { + return tr/A-Za-z/N-ZA-Mn-za-m/; +} + + +1; + |