diff options
Diffstat (limited to 'lib/VNDB/Util/Misc.pm')
-rw-r--r-- | lib/VNDB/Util/Misc.pm | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/lib/VNDB/Util/Misc.pm b/lib/VNDB/Util/Misc.pm index 48bc226d..a3a156d3 100644 --- a/lib/VNDB/Util/Misc.pm +++ b/lib/VNDB/Util/Misc.pm @@ -7,7 +7,7 @@ use Exporter 'import'; use TUWF ':html'; use VNDB::Func; -our @EXPORT = qw|filFetchDB ieCheck|; +our @EXPORT = qw|filFetchDB ieCheck bbSubstLinks|; my %filfields = ( @@ -143,5 +143,74 @@ sub ieCheck { return 0; } + +sub bbSubstLinks { + my ($self, $msg) = @_; + + # pre-parse vndb links within message body + my (%lookup, %links); + while ($msg =~ m/(?:^|\s)\K([vcpgi])([1-9][0-9]*)(?=$|\b[^.])/g) { + $lookup{$1}{$2} = 1; + } + return $msg unless %lookup; + # lookup parsed links + if ($lookup{v}) { + $links{"v$_->{id}"} = $_->{title} for (@{$self->dbVNTitles(keys %{$lookup{v}})}); + } + if ($lookup{c}) { + $links{"c$_->{id}"} = $_->{name} for (@{$self->dbCharNames(keys %{$lookup{c}})}); + } + if ($lookup{p}) { + $links{"p$_->{id}"} = $_->{name} for (@{$self->dbProducerNames(keys %{$lookup{p}})}); + } + if ($lookup{g}) { + $links{"g$_->{id}"} = $_->{name} for (@{$self->dbTagGet(id => [keys %{$lookup{g}}])}); + } + if ($lookup{i}) { + $links{"i$_->{id}"} = $_->{name} for (@{$self->dbTraitGet(id => [keys %{$lookup{i}}])}); + } + return $msg unless %links; + my($result, @open) = ('', 'first'); + + while($msg =~ m{ + (\b[tdvprcugi][1-9][0-9]*(?=$|\b[^.])) | # 1. id + (\[[^\s\]]+\]) | # 2. tag + ((?:https?|ftp)://[^><"\n\s\]\[]+[\d\w=/-]) # 3. url + }x) { + my($match, $id, $tag) = ($&, $1, $2); + $result .= $`; + $msg = $'; + + if($open[$#open] ne 'raw' && $open[$#open] ne 'code') { + # handle tags + if($tag) { + $tag = lc $tag; + if($tag eq '[raw]') { + push @open, 'raw'; + } elsif($tag eq '[quote]') { + push @open, 'quote'; + } elsif($tag eq '[code]') { + push @open, 'code'; + } elsif($tag eq '[/quote]' && $open[$#open] eq 'quote') { + pop @open; + } elsif($match =~ m{\[url=((https?://|/)[^\]>]+)\]}i) { + push @open, 'url'; + } elsif($tag eq '[/url]' && $open[$#open] eq 'url') { + pop @open; + } + } elsif($id && !grep(/^(?:quote|url)/, @open) && $links{$match}) { + $match = sprintf '[url=/%s]%s[/url]', $match, $links{$match}; + } + } + pop @open if($tag && $open[$#open] eq 'raw' && lc$tag eq '[/raw]'); + pop @open if($tag && $open[$#open] eq 'code' && lc$tag eq '[/code]'); + + $result .= $match; + } + $result .= $msg; + + return $result; +} + 1; |