File Coverage

blib/lib/Markdent/Handler/HTMLStream/Document.pm
Criterion Covered Total %
statement 31 31 100.0
branch 2 4 50.0
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 41 45 91.1


line stmt bran cond sub pod time code
1             package Markdent::Handler::HTMLStream::Document;
2              
3 31     31   6596725 use strict;
  31         227  
  31         852  
4 31     31   158 use warnings;
  31         61  
  31         742  
5 31     31   11473 use namespace::autoclean;
  31         436122  
  31         157  
6              
7             our $VERSION = '0.39';
8              
9 31     31   13983 use Markdent::Types;
  31         125  
  31         717  
10              
11 31     31   752995 use Moose;
  31         11609803  
  31         782  
12 31     31   237762 use MooseX::SemiAffordanceAccessor;
  31         335357  
  31         160  
13              
14             with 'Markdent::Role::HTMLStream';
15              
16             has title => (
17             is => 'ro',
18             isa => t('Str'),
19             required => 1,
20             );
21              
22             has charset => (
23             is => 'ro',
24             isa => t('Str'),
25             predicate => '_has_charset',
26             );
27              
28             has language => (
29             is => 'ro',
30             isa => t('Str'),
31             predicate => '_has_language',
32             );
33              
34             my $Doctype = <<'EOF';
35             <!DOCTYPE html>
36             EOF
37              
38             sub start_document {
39 1     1 0 3 my $self = shift;
40              
41 1         7 $self->_stream_raw($Doctype);
42 1 50       40 $self->_stream_start_tag(
43             'html', {
44             $self->_has_language ? ( lang => $self->language ) : (),
45             },
46             );
47 1         6 $self->_stream_start_tag('head');
48 1 50       87 $self->_stream_start_tag( 'meta', { charset => $self->charset } )
49             if $self->_has_charset;
50 1         6 $self->_stream_start_tag('title');
51 1         36 $self->_stream_text( $self->title );
52 1         7 $self->_stream_end_tag('title');
53 1         5 $self->_stream_end_tag('head');
54 1         5 $self->_stream_start_tag('body');
55             }
56              
57             sub end_document {
58 1     1 0 3 my $self = shift;
59              
60 1         4 $self->_stream_end_tag('body');
61 1         4 $self->_stream_end_tag('html');
62             }
63              
64             __PACKAGE__->meta->make_immutable;
65              
66             1;
67              
68             # ABSTRACT: Turns Markdent events into a complete HTML document
69              
70             __END__
71              
72             =pod
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             Markdent::Handler::HTMLStream::Document - Turns Markdent events into a complete HTML document
79              
80             =head1 VERSION
81              
82             version 0.39
83              
84             =head1 DESCRIPTION
85              
86             This class takes an event stream and turns it into a complete HTML document.
87              
88             =head1 METHODS
89              
90             This role provides the following methods:
91              
92             =head2 Markdent::Handler::HTMLStream::Document->new(...)
93              
94             This method creates a new handler. It accepts the following parameters:
95              
96             =over 4
97              
98             =item * title => $title
99              
100             The title of the document. This is required.
101              
102             =item * charset => $charset
103              
104             If provided, a C<< <meta charset="..."> >> tag will be added to the document's
105             C<< <head> >>.
106              
107             =item * language => $language
108              
109             If provided, a "lang" attribute will be added to the document's C<< <html> >>
110             tag.
111              
112             =item * output => $fh
113              
114             The file handle or object to which HTML output will be streamed. If you want
115             to capture the output in a string, you can open a filehandle to a string:
116              
117             my $buffer = q{};
118             open my $fh, '>', \$buffer;
119              
120             If you pass a file handle (or L<IO::Handle> object), then all calls to
121             C<print()> will be checked, and an error will be thrown.
122              
123             You can pass an object of any other class, it must implement its own
124             C<print()> method, and error handling is left up to this method.
125              
126             =back
127              
128             =head1 ROLES
129              
130             This class does the L<Markdent::Role::HTMLStream>,
131             L<Markdent::Role::EventsAsMethods>, and L<Markdent::Role::Handler> roles.
132              
133             =head1 BUGS
134              
135             See L<Markdent> for bug reporting details.
136              
137             Bugs may be submitted at L<https://github.com/houseabsolute/Markdent/issues>.
138              
139             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
140              
141             =head1 SOURCE
142              
143             The source code repository for Markdent can be found at L<https://github.com/houseabsolute/Markdent>.
144              
145             =head1 AUTHOR
146              
147             Dave Rolsky <autarch@urth.org>
148              
149             =head1 COPYRIGHT AND LICENSE
150              
151             This software is copyright (c) 2021 by Dave Rolsky.
152              
153             This is free software; you can redistribute it and/or modify it under
154             the same terms as the Perl 5 programming language system itself.
155              
156             The full text of the license can be found in the
157             F<LICENSE> file included with this distribution.
158              
159             =cut