Pod/Handler.pm

filter: Statementstmt 67.7% 21 / 31 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 63.6% 7 / 11 Podpod n/a totaltotal 60.8% 28 / 46 CC 3 SCAR 9.6
CC
3
Cov
60%
start_document
6.0
text
6.0
_stream
2.0
CRAP 2.6
« Pod/script.pm ↑ Summary Pod.pm »
1 package Markdown::Pod::Handler;
2 {
3 $Markdown::Pod::Handler::VERSION = '0.005';
4 }
5 # ABSTRACT: Parser module to convert from markdown to POD
6  
7 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 30 use Markdent::Types qw(
11 Bool Str HashRef OutputStream HeaderLevel
12 30 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 30 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 30 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 30 ▶use MooseX::SemiAffordanceAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 30 ▶use MooseX::Params::Validate qw( validated_list validated_hash );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 with 'Markdent::Role::EventsAsMethods';
20  
21 has encoding => (
22 is => 'ro',
23 isa => Str,
24 default => q{},
25 );
26  
27 has _output => (
28 is => 'ro',
29 isa => OutputStream,
30 required => 1,
31 init_arg => 'output',
32 );
33  
34 my $link_buf;
35 my @blockquotes;
36 my @list_type;
37  
38 sub _stream {
39 0 ▶ my ( $self, @params ) = @_;
Subroutine _stream: not called CC=1 SCAR=6.9
40 0 print { $self->_output } @params;
41 }
42  
43 sub start_document {
44 0 ▶ my $self = shift;
Subroutine start_document: not called CC=2 SCAR=17.9
45  
46 0 ▶ $self->_stream( '=encoding ' . $self->encoding . "\n\n" ) if $self->encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $self->encoding 0 0 0
47 }
48  
49 ▶sub end_document { }
Subroutine end_document: not called CC=1 SCAR=0.0
50  
51 sub text {
52 0 ▶ my $self = shift;
Subroutine text: not called CC=2 SCAR=17.9
53 0 my ($text) = validated_list( \@_, text => { isa => Str } );
54  
55 0 ▶ if ( $link_buf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_buf) { } 0 0 0
56 0 $link_buf->{text} = $text;
57 }
58 else {
59 0 $self->_stream( $text );
60 }
61 }
62  
63 sub start_header {
64 my $self = shift;
65 my ($level) = validated_list(
66 \@_,
67 level => { isa => HeaderLevel },
68 );
69  
70 $self->_stream("\n=head$level ");
71 }
72  
73 sub end_header {
74 my $self = shift;
75 my ($level) = validated_list(
76 \@_,
77 level => { isa => HeaderLevel },
78 );
79  
80 $self->_stream( "\n" );
81 }
82  
83 sub start_paragraph {
84 my $self = shift;
85 }
86  
87 sub end_paragraph {
88 my $self = shift;
89  
90 $self->_stream("\n");
91 }
92  
93 sub start_link {
94 my $self = shift;
95 my %p = validated_hash(
96 \@_,
97 uri => { isa => Str },
98 title => { isa => Str, optional => 1 },
99 id => { isa => Str, optional => 1 },
100 is_implicit_id => { isa => Bool, optional => 1 },
101 );
102  
103 delete @p{ grep { ! defined $p{$_} } keys %p };
104  
105 $link_buf->{uri} = $p{uri};
106 $self->_stream('L<');
107 }
108  
109 sub end_link {
110 my $self = shift;
111  
112 if ($link_buf && $link_buf->{text}) {
113 $self->_stream( "$link_buf->{text}|$link_buf->{uri}>" );
114 }
115 else {
116 $self->_stream( "$link_buf->{uri}>" );
117 }
118  
119 $link_buf = undef;
120 }
121  
122 sub start_strong {
123 my $self = shift;
124  
125 $self->_stream('B<');
126 }
127  
128 sub end_strong {
129 my $self = shift;
130  
131 $self->_stream('>');
132 }
133  
134 sub start_emphasis {
135 my $self = shift;
136  
137 $self->_stream('I<');
138 }
139  
140 sub end_emphasis {
141 my $self = shift;
142  
143 $self->_stream('>');
144 }
145  
146 sub preformatted {
147 my $self = shift;
148 my ($text) = validated_list( \@_, text => { isa => Str }, );
149  
150 chomp $text;
151 $text =~ s/^/ /gsm;
152 $self->_stream( $text, "\n\n" );
153 }
154  
155 sub start_blockquote {
156 my $self = shift;
157  
158 $self->_stream("=begin blockquote\n\n");
159 }
160  
161 sub end_blockquote {
162 my $self = shift;
163  
164 $self->_stream("=end blockquote\n\n");
165 }
166  
167 sub start_unordered_list {
168 my $self = shift;
169  
170 $self->_stream("=over\n\n");
171 }
172  
173 sub end_unordered_list {
174 my $self = shift;
175  
176 $self->_stream("=back\n\n");
177 }
178  
179 sub start_ordered_list {
180 my $self = shift;
181  
182 $self->_stream("=over\n\n");
183 }
184  
185 sub end_ordered_list {
186 my $self = shift;
187  
188 $self->_stream("=back\n\n");
189 }
190  
191 sub start_list_item {
192 my $self = shift;
193 my %p = validated_hash(
194 \@_,
195 bullet => { isa => Str },
196 );
197  
198 $self->_stream("=item $p{bullet}\n\n");
199 }
200  
201 sub end_list_item {
202 my $self = shift;
203  
204 $self->_stream("\n\n");
205 }
206  
207 sub start_code {
208 my $self = shift;
209  
210 $self->_stream('C<');
211 }
212  
213 sub end_code {
214 my $self = shift;
215  
216 $self->_stream('>');
217 }
218  
219 sub image {
220 my $self = shift;
221 my %p = validated_hash(
222 \@_,
223 alt_text => { isa => Str },
224 uri => { isa => Str, optional => 1 },
225 title => { isa => Str, optional => 1 },
226 id => { isa => Str, optional => 1 },
227 is_implicit_id => { isa => Bool, optional => 1 },
228 );
229  
230 delete @p{ grep { ! defined $p{$_} } keys %p };
231  
232 my $alt_text = exists $p{alt_text} ? qq|alt="$p{alt_text}"| : q{};
233  
234 my $attr = exists $p{title} ? $p{title} : q{};
235 my $attr_text = q{};
236 while ($attr =~ s/(\S+)="(.*?)"//) {
237 $attr_text .= qq{ $1="$2"};
238 }
239 while ($attr =~ /(\S+)=(\S+)/g) {
240 $attr_text .= qq{ $1="$2"};
241 }
242  
243 $self->_stream( qq|=for html <img src="$p{uri}" $alt_text$attr_text />| );
244 }
245  
246 sub start_html_tag {
247 my $self = shift;
248 my ( $tag, $attributes ) = validated_list(
249 \@_,
250 tag => { isa => Str },
251 attributes => { isa => HashRef },
252 );
253 }
254  
255 sub end_html_tag {
256 my $self = shift;
257 my ( $tag, $attributes ) = validated_list(
258 \@_,
259 tag => { isa => Str },
260 );
261 }
262  
263 sub html_tag {
264 my $self = shift;
265 my ( $tag, $attributes ) = validated_list(
266 \@_,
267 tag => { isa => Str },
268 attributes => { isa => HashRef },
269 );
270  
271 my $attributes_str = q{};
272 $attributes_str = join q{ }, map { qq|$_="$attributes->{$_}"| } sort keys %$attributes;
273 if ( $tag =~ /^br$/i ) {
274 $self->_stream( qq|<$tag $attributes_str />\n| );
275 }
276 else {
277 $self->_stream( qq|<$tag $attributes_str />| );
278 }
279 }
280  
281 sub html_block {
282 my $self = shift;
283 my ($html) = validated_list( \@_, html => { isa => Str }, );
284  
285 chomp $html;
286 $self->_output()->print(
287 <<"END_HTML"
288  
289 =begin html
290  
291 $html
292  
293 =end html
294  
295 END_HTML
296 );
297 }
298  
299 sub line_break {
300 my $self = shift;
301 $self->_stream( "\n\n" );
302 }
303  
304 sub html_entity {
305 my $self = shift;
306 my ($entity) = validated_list( \@_, entity => { isa => Str } );
307  
308 $self->_stream( "E<$entity>" );
309 }
310  
311  
312 __PACKAGE__->meta->make_immutable;
313 no Moose;
314 1;
315  
316 __END__
317  
318 =pod
319  
320 =encoding UTF-8
321  
322 =head1 NAME
323  
324 Markdown::Pod::Handler - Parser module to convert from markdown to POD
325  
326 =head1 VERSION
327  
328 version 0.005
329  
330 =head1 SYNOPSIS
331  
332 my $handler = Markdown::Pod::Handler->new(
333 encoding => $encoding,
334 output => $fh,
335 );
336
337 my $parser = Markdent::Parser->new(
338 dialect => $dialect,
339 handler => $handler,
340 );
341  
342 =head1 DESCRIPTION
343  
344 This module is a handler of L<Markdent> Markdown parser.
345 It converts Markdown to POD.
346  
347 =head1 ATTRIBUTES
348  
349 =head2 markdown
350  
351 markdown text
352  
353 =head2 encoding
354  
355 encoding to use
356  
357 =head1 METHODS
358  
359 =head2 new
360  
361 create Markdown::Pod::Handler object
362  
363 =head2 markdown_to_pod
364  
365 convert markdown text to POD text
366  
367 =head1 SEE ALSO
368  
369 =over
370  
371 =item *
372  
373 L<Markdent>
374  
375 =item *
376  
377 L<Pod::Markdown>
378  
379 =item *
380  
381 L<Text::MultiMarkdown>, L<Text::Markdown>
382  
383 =back
384  
385 =head1 AUTHOR
386  
387 Keedi Kim - 김도형 <keedi@cpan.org>
388  
389 =head1 COPYRIGHT AND LICENSE
390  
391 This software is copyright (c) 2013 by Keedi Kim.
392  
393 This is free software; you can redistribute it and/or modify it under
394 the same terms as the Perl 5 programming language system itself.
395  
396 =cut
« Pod/script.pm ↑ Summary Pod.pm »