OSDN Git Service

debian: fix searchd sample config file name
[newslash/newslash.git] / src / newslash_web / lib / Newslash / Model / deprecated / Util.pm
1 package Newslash::Model::Util;
2 use Newslash::Model::Base -base;
3
4 use Data::Dumper;
5 use Mojo::Log;
6
7 use EscapeHTML qw();
8 use HTML::Tidy;
9
10 =head2 tidy_html($html)
11
12 tidy HTML
13
14 =over 4
15
16 =item Parameters
17
18 =over 4
19
20 =item $html
21
22 input HTML
23
24 =back
25
26 =item Return value
27
28 tidied HTML
29
30 =back
31
32 =cut
33
34 sub tidy_html {
35     my ($self, $html) = @_;
36     my $tidy = HTML::Tidy->new( {
37                                  tidy_mark => 0,
38                                  doctype => 'omit',
39                                  'show-body-only' => 1,
40                      } );
41     my $tidied = $tidy->clean($html);
42     return $tidied;
43 }
44
45 =head2 clean_html(\%allowed, $html)
46
47 escape and tidy HTML
48
49 =over 4
50
51 =item Parameters
52
53 =over 4
54
55 =item $html
56
57 input HTML
58
59 =item \%allowed
60
61 allowed tag and attribute
62
63 =back
64
65 =item Return value
66
67 tidied HTML
68
69 =back
70
71 =cut
72
73 sub clean_html {
74     my ($self, $allowed, $html) = @_;
75     my $escaped = $self->escape_html($allowed, $html);
76     $escaped = EscapeHTML::blank_line_to_paragraph($escaped);
77
78     my $tidy = HTML::Tidy->new( {
79                                  tidy_mark => 0,
80                                  doctype => 'omit',
81                                  'show-body-only' => 1,
82                      } );
83     my $tidied = $tidy->clean($escaped);
84     chomp($tidied);
85     return $tidied;
86 }
87
88
89 =head2 escape_html(\%allowed, $html)
90
91 escape  HTML
92
93 =over 4
94
95 =item Parameters
96
97 =over 4
98
99 =item $html
100
101 input HTML
102
103 =item \%allowed
104
105 allowed tag and attribute
106
107 =back
108
109 =item Return value
110
111 escaped HTML
112
113 =back
114
115 =cut
116
117 sub escape_html {
118     my ($self, $allowed, $html) = @_;
119     return EscapeHTML::escape($allowed, $html);
120 }
121
122
123 =head2 escape_plaintext($text)
124
125 escape  HTML
126
127 =over 4
128
129 =item Parameters
130
131 =over 4
132
133 =item $text
134
135 input plain text
136
137 =back
138
139 =item Return value
140
141 escaped text
142
143 =back
144
145 =cut
146
147 sub escape_plaintext {
148     my ($self, $text) = @_;
149
150     $text =~ s/(?!|lt;|gt;)/&/g;
151     $text =~ s/</&lt;/g;
152     $text =~ s/>/&gt;/g;
153
154     return $text;
155 }
156
157 1;