]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Include.pm
Replaced intelquery function with a view
[ndwebbie.git] / lib / NDWeb / Include.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19
20 package NDWeb::Include;
21 use strict;
22 use warnings;
23 require Exporter;
24 use Parse::BBCode;
25 use CGI qw/:standard/;
26
27 our @ISA = qw/Exporter/;
28
29 our @EXPORT = qw/parseMarkup
30         html_escape
31         comma_value array_expand/;
32
33 sub html_escape($) {
34         return CGI::escapeHTML @_;
35 }
36
37 sub comma_value ($) {
38         my ($v) = @_;
39         $v =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
40         return $v;
41 }
42
43
44 my $bbc = Parse::BBCode->new({
45                 tags => {
46                         Parse::BBCode::HTML->defaults,
47                         '' => sub {
48                                 my $e = ($_[2]);
49                                 $e =~ s/\r?\n|\r/<br>\n/g;
50                                 $e
51                         },
52                         url   => 'url:<a href="%{link}A" rel="external">%s</a>',
53                         quote => 'block:<div class="bbcode-quote">
54 <div class="bbcode-quote-head"><b>%{html}a wrote:</b></div>
55 <div class="bbcode-quote-body">%s</div></div>',
56                         code => 'block:<div class="bbcode-quote"><pre class="bbcode-code">%{html}s</pre></div>',
57                         img   => 'url:<a href="%{link}A" rel="external">%s</a>',
58                         li  => 'block:<li>%{parse}s</li>',
59                         size  => '<span style="font-size: %{num}a%">%s</span>',
60
61                 },
62                 close_open_tags   => 1,
63         });
64
65 sub parseMarkup ($) {
66         my ($text) = @_;
67
68         $text = $bbc->render($text);
69         if ($bbc->error){
70                 my $tree = $bbc->get_tree;
71                 $text = $tree->raw_text;
72                 $text = $bbc->render($text);
73         }
74         $text =~ s/\x{3}\d\d?//g; #mirc color TODO: possibly match until \x{0F} and change to [color] block
75         $text =~ s/[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//g;
76         return $text;
77 }
78
79 sub array_expand ($) {
80         my ($array) = @_;
81
82         my @arrays;
83         for my $string (@{$array}){
84                 $string =~ s/^\((.*)\)$/$1/;
85                 $string =~ s/"//g;
86                 my @array = split /,/, $string;
87                 push @arrays,\@array;
88         }
89         return \@arrays;
90 }
91
92
93
94 1;