File Coverage

blib/lib/Org/To/Base.pm
Criterion Covered Total %
statement 44 92 47.8
branch 31 70 44.2
condition 2 4 50.0
subroutine 9 13 69.2
pod 2 2 100.0
total 88 181 48.6


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