Base.pm

filter: Statementstmt 52.0% 52 / 100 Branchbran 44.2% 31 / 70 Conditioncond 50.0% 2 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 64.2% 9 / 14 Podpod 100% 2 / 2 totaltotal 50.5% 97 / 192 SCAR 55.8
CC
41
Cov
49%
export_elements
99.0
_included_children
65.4
export
14.4
CRAP 266.0
« HTML.pm ↑ Summary Role.pm »
1 package Org::To::Base;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use experimental 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 4 our $DATE = '2022-02-12'; # DATE
13 4 our $DIST = 'Org-To-HTML'; # DIST
14 4 our $VERSION = '0.235'; # VERSION
15  
16 4 has source_file => (is => 'rw'); # for informational purposes
17 4 has include_tags => (is => 'rw');
18 4 has exclude_tags => (is => 'rw');
19  
20 sub _included_children {
21 5 ▶ my ($self, $elem) = @_;
Subroutine _included_children: called CC=11 SCAR=41.8
22  
23 5 my @htags = $elem->get_tags;
24 5 ▶ my @children = @{$elem->children // []};
Condition100%2/2
$elem->children // []
l!l
23
MC/DC100%1/1
$elem->children // []
$elem->children
Truth table100%2/2
A
A= $elem->children
Aresult
11
00
25 5 ▶ if ($self->include_tags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->include_tags) 0 5 5
26 0 ▶ if (!defined(first {$_ ~~ @htags} @{$self->include_tags})) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &first(sub { $_ ~~ \@htags; } , @{$self->include_tags;})) 0 0 0
27 # headline doesn't contain include_tags, select only
28 # suheadlines that contain them
29 0 @children = ();
30 0 ▶ for my $c (@{ $elem->children // []}) {
Condition0%0/2
$elem->children // []
l!l
00
MC/DC0%0/1
$elem->children // []
$elem->children
Truth table0%0/2
A
A= $elem->children
Aresult
11
00
31 0 ▶ next unless $c->isa('Org::Element::Headline');
Branch: 1 branch
BranchTrueFalseTotal
unless $c->isa("Org::Element::Headline") 0 0 0
32 my @hl_included = $elem->find(
33 sub {
34 0 ▶ my $el = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
35 return unless
36 0 ▶ $elem->isa('Org::Element::Headline');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("Org::Element::Headline") 0 0 0
37 0 my @t = $elem->get_tags;
38 0 ▶ return defined(first {$_ ~~ @t}
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 @{$self->include_tags});
40 0 });
41 0 ▶ next unless @hl_included;
Branch: 1 branch
BranchTrueFalseTotal
unless @hl_included 0 0 0
42 0 push @children, $c;
43 }
44 0 ▶ return () unless @children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 0 0 0
45 }
46 }
47 5 ▶ if ($self->exclude_tags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exclude_tags) 0 5 5
48 0 ▶ return () if defined(first {$_ ~~ @htags}
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 ▶ @{$self->exclude_tags});
Branch: 1 branch
BranchTrueFalseTotal
if defined &first(sub { $_ ~~ \@htags; } , @{$self->exclude_tags;}) 0 0 0
50 }
51 5 @children;
52 }
53  
54 sub export {
55 4 ▶ my ($self, $doc) = @_;
Subroutine export: called CC=5 SCAR=26.7
Pod documented
56  
57 4 my $inct = $self->include_tags;
58 4 ▶ if ($inct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inct) 0 4 4
59 0 my $doc_has_include_tags;
60 0 for my $h ($doc->find('Org::Element::Headline')) {
61 0 my @htags = $h->get_tags;
62 0 ▶ if (defined(first {$_ ~~ @htags} @$inct)) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (defined &first(sub { $_ ~~ \@htags; } , @$inct)) 0 0 0
63 0 $doc_has_include_tags++;
64 0 last;
65 }
66 }
67 0 ▶ $self->include_tags(undef) unless $doc_has_include_tags;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc_has_include_tags 0 0 0
68 }
69  
70 4 $self->export_elements($doc);
71 }
72  
73 sub export_elements {
74 18 ▶ my ($self, @elems) = @_;
Subroutine export_elements: called CC=26 SCAR=46.0
Pod documented
75  
76 18 my $res = [];
77 ELEM:
78 18 for my $elem (@elems) {
79 21 ▶ if ($self->can("before_export_element")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("before_export_element")) 21 0 21
80 21 $self->before_export_element(
81 hook => 'before_export_element',
82 elem => $elem,
83 );
84 }
85 21 ▶ if (log_is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if (log_is_trace()) 0 21 21
86 0 require String::Escape;
87 0 log_trace("exporting element %s (%s) ...", ref($elem),
88 String::Escape::elide(
89 String::Escape::printable($elem->as_string), 30));
90 }
91 21 my $elc = ref($elem);
92  
93 21 ▶ if ($elc eq 'Org::Element::Block') {
Branch: 20 branches
BranchTrueFalseTotal
if ($elc eq 'Org::Element::Block') { } 1 20 21
elsif ($elc eq 'Org::Element::FixedWidthSection') { } 1 19 20
elsif ($elc eq 'Org::Element::Comment') { } 0 19 19
elsif ($elc eq 'Org::Element::Drawer') { } 0 19 19
elsif ($elc eq 'Org::Element::Footnote') { } 0 19 19
elsif ($elc eq 'Org::Element::Headline') { } 5 14 19
elsif ($elc eq 'Org::Element::List') { } 0 14 14
elsif ($elc eq 'Org::Element::ListItem') { } 0 14 14
elsif ($elc eq 'Org::Element::RadioTarget') { } 0 14 14
elsif ($elc eq 'Org::Element::Setting') { } 2 12 14
elsif ($elc eq 'Org::Element::Table') { } 0 12 12
elsif ($elc eq 'Org::Element::TableCell') { } 0 12 12
elsif ($elc eq 'Org::Element::TableRow') { } 0 12 12
elsif ($elc eq 'Org::Element::TableVLine') { } 0 12 12
elsif ($elc eq 'Org::Element::Target') { } 0 12 12
elsif ($elc eq 'Org::Element::Text') { } 8 4 12
elsif ($elc eq 'Org::Element::Link') { } 0 4 4
elsif ($elc eq 'Org::Element::TimeRange') { } 0 4 4
elsif ($elc eq 'Org::Element::Timestamp') { } 0 4 4
elsif ($elc eq 'Org::Document') { } 4 0 4
94 1 push @$res, $self->export_block($elem);
95 } elsif ($elc eq 'Org::Element::FixedWidthSection') {
96 1 push @$res, $self->export_fixed_width_section($elem);
97 } elsif ($elc eq 'Org::Element::Comment') {
98 0 push @$res, $self->export_comment($elem);
99 } elsif ($elc eq 'Org::Element::Drawer') {
100 0 push @$res, $self->export_drawer($elem);
101 } elsif ($elc eq 'Org::Element::Footnote') {
102 0 push @$res, $self->export_footnote($elem);
103 } elsif ($elc eq 'Org::Element::Headline') {
104 5 push @$res, $self->export_headline($elem);
105 } elsif ($elc eq 'Org::Element::List') {
106 0 push @$res, $self->export_list($elem);
107 } elsif ($elc eq 'Org::Element::ListItem') {
108 0 push @$res, $self->export_list_item($elem);
109 } elsif ($elc eq 'Org::Element::RadioTarget') {
110 0 push @$res, $self->export_radio_target($elem);
111 } elsif ($elc eq 'Org::Element::Setting') {
112 2 push @$res, $self->export_setting($elem);
113 } elsif ($elc eq 'Org::Element::Table') {
114 0 push @$res, $self->export_table($elem);
115 } elsif ($elc eq 'Org::Element::TableCell') {
116 0 push @$res, $self->export_table_cell($elem);
117 } elsif ($elc eq 'Org::Element::TableRow') {
118 0 push @$res, $self->export_table_row($elem);
119 } elsif ($elc eq 'Org::Element::TableVLine') {
120 0 push @$res, $self->export_table_vline($elem);
121 } elsif ($elc eq 'Org::Element::Target') {
122 0 push @$res, $self->export_target($elem);
123 } elsif ($elc eq 'Org::Element::Text') {
124 8 push @$res, $self->export_text($elem);
125 } elsif ($elc eq 'Org::Element::Link') {
126 0 push @$res, $self->export_link($elem);
127 } elsif ($elc eq 'Org::Element::TimeRange') {
128 0 push @$res, $self->export_time_range($elem);
129 } elsif ($elc eq 'Org::Element::Timestamp') {
130 0 push @$res, $self->export_timestamp($elem);
131 } elsif ($elc eq 'Org::Document') {
132 4 push @$res, $self->export_document($elem);
133 } else {
134 0 log_warn("Don't know how to export $elc element, skipped");
135 0 ▶ push @$res, $self->export_elements(@{$elem->children})
Branch: 1 branch
BranchTrueFalseTotal
if $elem->children 0 0 0
136 if $elem->children;
137 }
138  
139 21 ▶ if ($self->can("after_export_element")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("after_export_element")) 0 21 21
140 0 $self->after_export_element(
141 hook => 'after_export_element',
142 elem => $elem,
143 );
144 }
145 }
146  
147 18 join "", @$res;
148 }
149  
150 4 1;
151 # ABSTRACT: Base class for Org exporters
152  
153 __END__
154  
155 =pod
156  
157 =encoding UTF-8
158  
159 =head1 NAME
160  
161 Org::To::Base - Base class for Org exporters
162  
163 =head1 VERSION
164  
165 This document describes version 0.235 of Org::To::Base (from Perl distribution Org-To-HTML), released on 2022-02-12.
166  
167 =head1 SYNOPSIS
168  
169 # Not to be used directly. Use one of its subclasses, like Org::To::HTML.
170  
171 =head1 DESCRIPTION
172  
173 This module is a base class for Org exporters. To create an exporter, subclass
174 from this class (as well as add L<Org::To::Role> role) and provide an
175 implementation for the export_*() methods. Add extra attributes for export
176 options as necessary (for example, Org::To::HTML adds C<html_title>, C<css_url>,
177 and so on).
178  
179 =for Pod::Coverage BUILD
180  
181 =head1 ATTRIBUTES
182  
183 =head2 include_tags
184  
185 Arrayref. Works like Org's 'org-export-select-tags' variable. If the whole
186 document doesn't have any of these tags, then the whole document will be
187 exported. Otherwise, trees that do not carry one of these tags will be excluded.
188 If a selected tree is a subtree, the heading hierarchy above it will also be
189 selected for export, but not the text below those headings.
190  
191 =head2 exclude_tags
192  
193 Arrayref. If the whole document doesn't have any of these tags, then the whole
194 document will be exported. Otherwise, trees that do not carry one of these tags
195 will be excluded. If a selected tree is a subtree, the heading hierarchy above
196 it will also be selected for export, but not the text below those headings.
197  
198 C<exclude_tags> is evaluated after L</include_tags>.
199  
200 =head2 source_file
201  
202 String. Optional. Can be set for informational purposes, e.g. will be used as
203 default title when document does not set C>#+TITLE>.
204  
205 =head1 METHODS
206  
207 =head2 $exp->export($doc) => STR
208  
209 Export Org.
210  
211 =head2 $exp->export_elements(@elems) => STR
212  
213 Export Org element objects and with the children, recursively. Will call various
214 C<export_*()> methods according to element class. Should return a string which
215 is the exported document.
216  
217 Several hooks are recognized and will be invoked if defined:
218  
219 =over
220  
221 =item * before_export_element
222  
223 Will be called before calling each C<export_*()>. Will be passed hash argument
224 C<%hash> containing these keys: C<hook> (hook name, in this case
225 C<before_export_element>), C<elem> (the element object).
226  
227 =item * after_export_element
228  
229 Will be called after calling each C<export_*()>. Will be passed hash argument
230 C<%hash> containing these keys: C<hook> (hook name, in this case
231 C<after_export_element>), C<elem> (the element object).
232  
233 =back
234  
235 =head1 HOMEPAGE
236  
237 Please visit the project's homepage at L<https://metacpan.org/release/Org-To-HTML>.
238  
239 =head1 SOURCE
240  
241 Source repository is at L<https://github.com/perlancar/perl-Org-To-HTML>.
242  
243 =head1 AUTHOR
244  
245 perlancar <perlancar@cpan.org>
246  
247 =head1 CONTRIBUTING
248  
249  
250 To contribute, you can send patches by email/via RT, or send pull requests on
251 GitHub.
252  
253 Most of the time, you don't need to build the distribution yourself. You can
254 simply modify the code, then test via:
255  
256 % prove -l
257  
258 If you want to build the distribution (e.g. to try to install it locally on your
259 system), you can install L<Dist::Zilla>,
260 L<Dist::Zilla::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
261 Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required
262 beyond that are considered a bug and can be reported to me.
263  
264 =head1 COPYRIGHT AND LICENSE
265  
266 This software is copyright (c) 2022, 2020, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011 by perlancar <perlancar@cpan.org>.
267  
268 This is free software; you can redistribute it and/or modify it under
269 the same terms as the Perl 5 programming language system itself.
270  
271 =head1 BUGS
272  
273 Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Org-To-HTML>
274  
275 When submitting a bug or request, please include a test-file or a
276 patch to an existing test-file that illustrates the bug or desired
277 feature.
278  
279 =cut
« HTML.pm ↑ Summary Role.pm »