blib/lib/oEdtk/Doc.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 129 CC 10 SCAR 22.6
CC
10
Cov
0%
_line_cutoff
20.0
include
20.0
append
12.0
CRAP 9.6
1 package oEdtk::Doc;
2 use strict;
3  
4 our $VERSION = '2.1126';
5  
6 use Scalar::Util qw(blessed);
7 use overload '""' => \&dump;
8 use oEdtk::Config qw(config_read);
9  
10 # The maximum number of characters to output before inserting a newline
11 # character. The mainline value is 120; the EDTK_LINE_CUTOFF key of the COMPO
12 # section overrides it (the fork emits one tag per line). config_read()
13 # re-parses the ini on every call, so the switch is read once and cached.
14 use constant DEFAULT_LINE_CUTOFF => 120;
15  
16 my $LINE_CUTOFF;
17  
18 sub _line_cutoff {
19 unless ( defined $LINE_CUTOFF ) {
20 my $cfg = eval { config_read('COMPO') } || {};
21 my $val = ref $cfg ? $cfg->{'EDTK_LINE_CUTOFF'} : undef;
22 $LINE_CUTOFF = ( defined $val && $val =~ /\S/ ) ? $val : DEFAULT_LINE_CUTOFF;
23 }
24 return $LINE_CUTOFF;
25 }
26  
27 sub new {
28 my ($class) = @_;
29  
30 my $self = {};
31 bless $self, $class;
32 $self->reset();
33 return $self;
34 }
35  
36 sub reset {
37 my ($self) = @_;
38  
39 $self->{'taglist'} = [];
40 $self->{'emitted'} = 0;
41 return $self;
42 }
43  
44 sub append {
45 my ( $self, $name, $value ) = @_;
46  
47 if ( blessed($name) && $name->isa('oEdtk::Doc') ) {
48 push( @{ $self->{'taglist'} }, @{ $name->{'taglist'} } );
49 }
50 elsif ( ref($name) eq 'HASH' ) {
51 while ( my ( $key, $val ) = each(%$name) ) {
52 $self->append( $key, $val );
53 }
54 }
55 else {
56 my $tag = $self->mktag( $name, $value );
57 push( @{ $self->{'taglist'} }, $tag );
58 }
59 return $self;
60 }
61  
62 sub dump {
63 my ($self) = @_;
64  
65 my $out = '';
66 foreach ( @{ $self->{'taglist'} } ) {
67 my $tag = $_->emit;
68 my $taglen = length $tag;
69 if ( $self->{'emitted'} + $taglen > _line_cutoff() ) {
70 $out .= $self->line_break();
71 $self->{'emitted'} = 0;
72 }
73 $self->{'emitted'} += $taglen;
74 $out .= $tag;
75 }
76 return $out;
77 }
78  
79 sub include {
80 my ( $self, $file, $path ) = @_;
81  
82 if ( defined $path ) {
83 if ( $path =~ /^EDTK_DIR_/ ) {
84 my $cfg = config_read('ENVDESC');
85 $path = $cfg->{$path};
86 }
87 }
88 else {
89 $path = ".";
90 warn "INFO: include param1 is $file, assuming param2 is $path \n";
91 }
92  
93 my $link = $path . "/" . $file;
94 $link =~ s/\\/\//g;
95 if ( -e $link ) { }
96 else { die "ERROR: can't find include $link\n"; }
97  
98 $self->append( "_include_", $link );
99 return $self;
100 }
101  
102 # THE FOLLOWING METHODS SHOULD ONLY BE IMPLEMENTED BY
103 # THE SUBCLASSES (SEE TEXDOC).
104 sub mktag {
105 die "ERROR: oEdtk::Doc::mktag unimplemented method\n";
106 }
107  
108 sub append_table {
109 die "ERROR: oEdtk::Doc::append_table unimplemented method\n";
110 }
111  
112 sub line_break {
113 die "ERROR: oEdtk::Doc::line_break unimplemented method\n";
114 }
115  
116 sub escape {
117 die "ERROR: oEdtk::Doc::escape unimplemented method\n";
118 }
119  
120 1;
121  
122 =head1 NAME
123  
124 oEdtk::Doc - abstract base class for emitted documents
125  
126 =head1 SYNOPSIS
127  
128 use oEdtk::Doc; # usually used through a subclass, e.g. oEdtk::TexDoc
129  
130 my $doc = oEdtk::TexDoc->new;
131 $doc->append('NOM', 'value');
132 print $doc; # dump(), through the "" overload
133  
134 =head1 DESCRIPTION
135  
136 The base class for a document built as an ordered list of tags. It stores the
137 tags, dumps them as a string, inserting a line break once the running length
138 exceeds the line cutoff, and stringifies to C<dump> through the C<""> overload.
139 The tag factory and the escaping rules are provided by subclasses
140 (C<oEdtk::TexDoc>).
141  
142 =head1 METHODS
143  
144 =head2 new
145  
146 my $doc = oEdtk::Doc->new;
147  
148 Create an empty document.
149  
150 =head2 reset
151  
152 $doc->reset;
153  
154 Clear the tag list and the emitted-length counter, and return the object.
155  
156 =head2 append
157  
158 $doc->append($name, $value);
159  
160 Append a tag. C<$name> may be an C<oEdtk::Doc> (its tags are appended), a hash
161 reference (each key is appended in turn), or a tag name paired with C<$value>
162 (built through L</mktag>). Returns the object.
163  
164 =head2 dump
165  
166 my $out = $doc->dump;
167  
168 Concatenate the emitted tags, inserting L</line_break> whenever the running
169 length would cross the line cutoff (default C<120>, overridable with the
170 C<EDTK_LINE_CUTOFF> key of the C<COMPO> section). This is also the C<"">
171 overload.
172  
173 =head2 include
174  
175 $doc->include($file, $path);
176  
177 Append an C<_include_> tag for C<$file> under C<$path>. A C<$path> naming an
178 C<EDTK_DIR_*> key is resolved through the C<ENVDESC> section; a missing file is
179 an error. Returns the object.
180  
181 =head2 mktag
182  
183 Abstract: build the tag object for C<$name> and C<$value>. Unimplemented here
184 (it C<die>s); a concrete subclass such as L<oEdtk::TexDoc> provides it.
185  
186 =head2 append_table
187  
188 Abstract: append a table under C<$name>. Unimplemented here (it C<die>s).
189  
190 =head2 line_break
191  
192 Abstract: return the string inserted at a line break. Unimplemented here (it
193 C<die>s).
194  
195 =head2 escape
196  
197 Abstract: escape a string for the target format. Unimplemented here (it
198 C<die>s).
199  
200 The private helper C<_line_cutoff> resolves and caches the effective line
201 cutoff; it is not part of the public API.
202  
203 =head1 AUTHOR
204  
205 The oEdtk team.
206  
207 =cut