Silki/Markdent/Handler/HTMLGenerator.pm untested

filter: Statementstmt 0.0% 0 / 154 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 244 CC 17 SCAR 27.8
CC
17
Cov
0%
_link_to_page
56.0
_link_to_file
30.0
start_link
12.0
CRAP 16.1
1 package Silki::Markdent::Handler::HTMLGenerator;
2 {
3 $Silki::Markdent::Handler::HTMLGenerator::VERSION = '0.29';
4 }
5  
6 use strict;
7 use warnings;
8 use namespace::autoclean;
9  
10 use HTML::Entities qw( encode_entities );
11 use Markdent::Types qw( HeaderLevel );
12 use Silki::I18N qw( loc );
13 use Silki::Schema::Page;
14 use Silki::Schema::Permission;
15 use Silki::Types qw( Bool HashRef Int ScalarRef Str );
16 use Text::TOC::HTML;
17  
18 use Moose;
19 use MooseX::Params::Validate qw( validated_list validated_hash );
20 use MooseX::SemiAffordanceAccessor;
21 use MooseX::StrictConstructor;
22  
23 extends 'Markdent::Handler::HTMLStream::Fragment';
24  
25 with 'Silki::Markdent::Role::WikiLinkResolver';
26  
27 has _user => (
28 is => 'ro',
29 isa => 'Silki::Schema::User',
30 required => 1,
31 init_arg => 'user',
32 );
33  
34 has _cached_perms => (
35 is => 'ro',
36 isa => HashRef,
37 default => sub { {} },
38 init_arg => undef,
39 );
40  
41 has _include_toc => (
42 is => 'ro',
43 isa => Bool,
44 init_arg => 'include_toc',
45 default => 0,
46 );
47  
48 has _nofollow_external => (
49 is => 'ro',
50 isa => Bool,
51 init_arg => 'nofollow_external',
52 default => 1,
53 );
54  
55 has _for_editor => (
56 is => 'ro',
57 isa => Bool,
58 init_arg => 'for_editor',
59 default => 0,
60 );
61  
62 has '+_output' => (
63 builder => '_build_output',
64 );
65  
66 has _buffer => (
67 is => 'ro',
68 isa => ScalarRef,
69 init_arg => undef,
70 lazy => 1,
71 default => sub { my $buf = q{}; \$buf },
72 );
73  
74 has _header_count => (
75 traits => ['Counter'],
76 is => 'ro',
77 isa => Int,
78 init_arg => undef,
79 default => 0,
80 handles => { _inc_header_count => 'inc' },
81 );
82  
83 sub _build_output {
84 my $self = shift;
85  
86 open my $fh, '>:utf8', $self->_buffer();
87  
88 return $fh;
89 }
90  
91 after start_header => sub {
92 my $self = shift;
93 my ($level) = validated_list(
94 \@_,
95 level => { isa => HeaderLevel },
96 );
97  
98 return unless $level <= 4;
99  
100 $self->_inc_header_count();
101 };
102  
103 sub placeholder {
104 my $self = shift;
105 my ($id) = validated_list(
106 \@_,
107 id => { isa => Str },
108 );
109  
110 $self->_stream()->comment( 'Placeholder: ' . $id );
111 }
112  
113 sub _replace_placeholder {
114 my $self = shift;
115 my $id = shift;
116 my $link_data = shift;
117  
118 my $snippet = $self->_link_to_page($link_data);
119  
120 ${ $self->_buffer() } =~ s/<!--\s*Placeholder: \Q$id\E\s*-->\n/$snippet/g;
121  
122 return;
123 }
124  
125 sub file_link {
126 my $self = shift;
127 my ( $link, $display_text ) = validated_list(
128 \@_,
129 link_text => { isa => Str },
130 display_text => { isa => Str, optional => 1 },
131 );
132  
133 my $link_data = $self->_resolve_file_link( $link, $display_text );
134  
135 $self->_link_to_file($link_data);
136  
137 return;
138 }
139  
140 sub image_link {
141 my $self = shift;
142 my $link = validated_list(
143 \@_,
144 link_text => { isa => Str },
145 );
146  
147 my $link_data = $self->_resolve_file_link($link);
148  
149 if ( $link_data->{file}
150 && $link_data->{file}->is_browser_displayable_image() ) {
151  
152 $self->_link_to_file( $link_data, undef, 'as image' );
153 }
154 else {
155 $self->_link_to_file($link_data);
156 }
157  
158 return;
159 }
160  
161 sub _link_to_file {
162 my $self = shift;
163 my $p = shift;
164 my $display_text = shift;
165 my $as_image = shift;
166  
167 my $file = $p->{file};
168  
169 unless ( defined $file ) {
170 $self->_stream()->text( $p->{text} );
171 return;
172 }
173  
174 unless ( $self->_check_for_read_permission( $file->wiki() ) ) {
175 $self->_stream()->text( loc('(inaccessible file)') );
176 return;
177 }
178  
179 my $file_uri = $file->uri();
180  
181 my $title
182 = $file->is_displayable_in_browser()
183 ? loc('View this file')
184 : loc('Download this file');
185  
186 $self->_stream()->tag(
187 a => (
188 href => $file_uri,
189 title => $title,
190 )
191 );
192  
193 if ($as_image) {
194 $self->_stream->tag(
195 img => (
196 src => $file->uri( view => 'small' ),
197 alt => $file->filename(),
198 ),
199 '/', # XXX - should fix HTML::Stream to not need this
200 );
201 }
202 else {
203 $self->_stream()->text( $p->{text} );
204 }
205  
206 $self->_stream()->tag('_a');
207 }
208  
209 sub auto_link {
210 my $self = shift;
211 my ($uri) = validated_list(
212 \@_,
213 uri => { isa => Str, optional => 1 },
214 );
215  
216 my %tag_attr = ( href => $uri );
217 $tag_attr{rel} = 'nofollow'
218 if $self->_nofollow_external();
219  
220 $self->_stream()->tag( 'a', %tag_attr );
221 $self->_stream()->text($uri);
222 $self->_stream()->tag('_a');
223 }
224  
225 sub start_link {
226 my $self = shift;
227 my %p = validated_hash(
228 \@_,
229 uri => { isa => Str },
230 title => { isa => Str, optional => 1 },
231 id => { isa => Str, optional => 1 },
232 is_implicit_id => { isa => Bool, optional => 1 },
233 );
234  
235 delete @p{ grep { !defined $p{$_} } keys %p };
236  
237 my %tag_attr = ( href => $p{uri} );
238 $tag_attr{title} = $p{title}
239 if exists $p{title};
240 $tag_attr{rel} = 'nofollow'
241 if $self->_nofollow_external();
242  
243 $self->_stream()->tag( 'a', %tag_attr );
244 }
245  
246 sub _link_to_page {
247 my $self = shift;
248 my $link_data = shift;
249  
250 unless ( $link_data->{page} || $link_data->{wiki} ) {
251 return encode_entities( $link_data->{text} );
252 }
253  
254 my $page = $link_data->{page};
255  
256 my $wiki = $link_data->{wiki} || $page->wiki();
257  
258 unless ( $self->_check_for_read_permission($wiki) ) {
259 return encode_entities( loc('(inaccessible page)') );
260 }
261  
262 if ( $self->_for_editor() ) {
263 $page ||= Silki::Schema::Page->new(
264 page_id => 0,
265 wiki_id => $link_data->{wiki}->wiki_id(),
266 title => $link_data->{title},
267 uri_path =>
268 Silki::Schema::Page->TitleToURIPath( $link_data->{title} ),
269 _from_query => 1,
270 );
271 }
272  
273 my $uri
274 = $page
275 ? $page->uri()
276 : $link_data->{wiki}->uri(
277 view => 'new_page_form',
278 query => { title => $link_data->{title} }
279 );
280  
281 my $class = $page ? 'existing-page' : 'new-page';
282  
283 my $title
284 = $page
285 ? loc( 'Read %1', $page->title() )
286 : loc('This page has not yet been created');
287  
288 return sprintf(
289 qq{<a href="%s" class="%s" title="%s">%s</a>},
290 map { encode_entities($_) } $uri, $class, $title, $link_data->{text}
291 );
292 }
293  
294 sub _check_for_read_permission {
295 my $self = shift;
296 my $wiki = shift;
297  
298 my $cached_perms = $self->_cached_perms;
299  
300 if ( exists $cached_perms->{ $wiki->wiki_id() } ) {
301 return $cached_perms->{ $wiki->wiki_id() };
302 }
303  
304 return $cached_perms->{ $wiki->wiki_id() }
305 = $self->_user()->has_permission_in_wiki(
306 wiki => $wiki,
307 permission => Silki::Schema::Permission->Read(),
308 );
309 }
310  
311 sub final_html_output {
312 my $self = shift;
313  
314 my $html = ${ $self->_buffer() };
315  
316 utf8::decode($html);
317  
318 return $html unless $self->_header_count() > 2;
319  
320 my $toc = Text::TOC::HTML->new(
321 filter => sub { $_[0]->tagName() =~ /^h[1-4]$/i } );
322  
323 my $fake_file = $self . q{};
324 $toc->add_file( file => $fake_file, content => $html );
325  
326 return
327 q{<div id="table-of-contents">} . "\n"
328 . $toc->html_for_toc() . "\n"
329 . '</div>' . "\n"
330 . $toc->html_for_document($fake_file);
331 }
332  
333 __PACKAGE__->meta()->make_immutable();
334  
335 1;
336  
337 # ABSTRACT: A subclass of Markdent::Handler::HTMLStream which handles Silki-specific markup
338  
339 __END__