| 1 | package oEdtk::Doc; | ||||||||||||||||||||||||||||||||
| 2 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 22 | our $VERSION = '2.1126'; | |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use overload '""' => \&dump; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 22 | ▶ | use oEdtk::Config qw(config_read); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 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 | 22 | ▶ | use constant DEFAULT_LINE_CUTOFF => 120; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 22 | my $LINE_CUTOFF; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub _line_cutoff { | ||||||||||||||||||||||||||||||
|
Subroutine
_line_cutoff: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | unless ( defined $LINE_CUTOFF ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | my $cfg = eval { config_read('COMPO') } || {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
eval { || {}
MC/DC0%0/1
eval {
do {
config_read('COMPO')
}
} || {}
eval {
do {
config_read('COMPO')
}
}
Truth table0%0/2
A
A= eval {
do {
config_read('COMPO')
}
}
| |||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | my $val = ref $cfg ? $cfg->{'EDTK_LINE_CUTOFF'} : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | $LINE_CUTOFF = ( defined $val && $val =~ /\S/ ) ? $val : DEFAULT_LINE_CUTOFF; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $val and $val =~ /\S/
MC/DC0%0/2
defined $val and $val =~ /\S/
defined $val
$val =~ /\S/
Truth table0%0/3
A and B
A= defined $valB= $val =~ /\S/
| |||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||
| 24 | 0 | return $LINE_CUTOFF; | |||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 28 | 0 | my ($class) = @_; | |||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 0 | my $self = {}; | |||||||||||||||||||||||||||||||
| 31 | 0 | bless $self, $class; | |||||||||||||||||||||||||||||||
| 32 | 0 | $self->reset(); | |||||||||||||||||||||||||||||||
| 33 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub reset { | ||||||||||||||||||||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 37 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 0 | $self->{'taglist'} = []; | |||||||||||||||||||||||||||||||
| 40 | 0 | $self->{'emitted'} = 0; | |||||||||||||||||||||||||||||||
| 41 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub append { | ||||||||||||||||||||||||||||||
|
Subroutine
append: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 45 | 0 | my ( $self, $name, $value ) = @_; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ( blessed($name) && $name->isa('oEdtk::Doc') ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
builtin::blessed($name) and $name->isa("oEdtk::Doc")
MC/DC0%0/2
builtin::blessed($name) and $name->isa("oEdtk::Doc")
builtin::blessed($name)
$name->isa("oEdtk::Doc")
Truth table0%0/3
A and B
A= builtin::blessed($name)B= $name->isa("oEdtk::Doc")
| |||||||||||||||||||||||||||||||||
| 48 | 0 | push( @{ $self->{'taglist'} }, @{ $name->{'taglist'} } ); | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | elsif ( ref($name) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | while ( my ( $key, $val ) = each(%$name) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | 0 | $self->append( $key, $val ); | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | else { | ||||||||||||||||||||||||||||||||
| 56 | 0 | my $tag = $self->mktag( $name, $value ); | |||||||||||||||||||||||||||||||
| 57 | 0 | push( @{ $self->{'taglist'} }, $tag ); | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 63 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 0 | my $out = ''; | |||||||||||||||||||||||||||||||
| 66 | 0 | foreach ( @{ $self->{'taglist'} } ) { | |||||||||||||||||||||||||||||||
| 67 | 0 | my $tag = $_->emit; | |||||||||||||||||||||||||||||||
| 68 | 0 | my $taglen = length $tag; | |||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( $self->{'emitted'} + $taglen > _line_cutoff() ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 0 | $out .= $self->line_break(); | |||||||||||||||||||||||||||||||
| 71 | 0 | $self->{'emitted'} = 0; | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | 0 | $self->{'emitted'} += $taglen; | |||||||||||||||||||||||||||||||
| 74 | 0 | $out .= $tag; | |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | 0 | return $out; | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub include { | ||||||||||||||||||||||||||||||
|
Subroutine
include: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 80 | 0 | my ( $self, $file, $path ) = @_; | |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ( defined $path ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( $path =~ /^EDTK_DIR_/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 0 | my $cfg = config_read('ENVDESC'); | |||||||||||||||||||||||||||||||
| 85 | 0 | $path = $cfg->{$path}; | |||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | else { | ||||||||||||||||||||||||||||||||
| 89 | 0 | $path = "."; | |||||||||||||||||||||||||||||||
| 90 | 0 | warn "INFO: include param1 is $file, assuming param2 is $path \n"; | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 0 | my $link = $path . "/" . $file; | |||||||||||||||||||||||||||||||
| 94 | 0 | $link =~ s/\\/\//g; | |||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ( -e $link ) { } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 0 | else { die "ERROR: can't find include $link\n"; } | |||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 0 | $self->append( "_include_", $link ); | |||||||||||||||||||||||||||||||
| 99 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | # THE FOLLOWING METHODS SHOULD ONLY BE IMPLEMENTED BY | ||||||||||||||||||||||||||||||||
| 103 | # THE SUBCLASSES (SEE TEXDOC). | ||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub mktag { | ||||||||||||||||||||||||||||||
|
Subroutine
mktag: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 105 | 0 | die "ERROR: oEdtk::Doc::mktag unimplemented method\n"; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub append_table { | ||||||||||||||||||||||||||||||
|
Subroutine
append_table: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 109 | 0 | die "ERROR: oEdtk::Doc::append_table unimplemented method\n"; | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub line_break { | ||||||||||||||||||||||||||||||
|
Subroutine
line_break: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 113 | 0 | die "ERROR: oEdtk::Doc::line_break unimplemented method\n"; | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub escape { | ||||||||||||||||||||||||||||||
|
Subroutine
escape: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 117 | 0 | die "ERROR: oEdtk::Doc::escape unimplemented method\n"; | |||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 22 | 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 | ||||||||||||||||||||||||||||||||