HTML.pm

filter: Statementstmt 50.2% 103 / 205 Branchbran 30.3% 20 / 66 Conditioncond 27.7% 10 / 36 MC/DCmcdc 10.5% 2 / 19 Subroutinesub 55.8% 19 / 34 Podpod 13.6% 3 / 22 totaltotal 41.1% 157 / 382 SCAR 61.9
CC
49
Cov
43%
export_link
132.0
export_list_item
42.0
export_list
30.0
CRAP 486.1
↑ Summary Base.pm »
1 package Org::To::HTML;
2  
3 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Slurper qw(read_text write_text);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use HTML::Entities qw/encode_entities/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Org::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use experimental 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 with 'Org::To::Role';
17 3 extends 'Org::To::Base';
18  
19 3 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
20 3 our $DATE = '2022-02-12'; # DATE
21 3 our $DIST = 'Org-To-HTML'; # DIST
22 3 our $VERSION = '0.235'; # VERSION
23  
24 3 our @EXPORT_OK = qw(org_to_html);
25  
26 3 has naked => (is => 'rw');
27 3 has html_title => (is => 'rw');
28 3 has css_url => (is => 'rw');
29 3 has inline_images => (is => 'rw');
30  
31 3 our %SPEC;
32 $SPEC{org_to_html} = {
33 3 v => 1.1,
34 summary => 'Export Org document to HTML',
35 description => <<'_',
36  
37 This is the non-OO interface. For more customization, consider subclassing
38 Org::To::HTML.
39  
40 _
41 args => {
42 source_file => {
43 summary => 'Source Org file to export',
44 schema => ['str' => {}],
45 },
46 source_str => {
47 summary => 'Alternatively you can specify Org string directly',
48 schema => ['str' => {}],
49 },
50 target_file => {
51 summary => 'HTML file to write to',
52 schema => ['str' => {}],
53 description => <<'_',
54  
55 If not specified, HTML string will be returned.
56  
57 _
58 },
59 include_tags => {
60 summary => 'Include trees that carry one of these tags',
61 schema => ['array' => {of => 'str*'}],
62 description => <<'_',
63  
64 Works like Org's 'org-export-select-tags' variable. If the whole document
65 doesn't have any of these tags, then the whole document will be exported.
66 Otherwise, trees that do not carry one of these tags will be excluded. If a
67 selected tree is a subtree, the heading hierarchy above it will also be selected
68 for export, but not the text below those headings.
69  
70 _
71 },
72 exclude_tags => {
73 summary => 'Exclude trees that carry one of these tags',
74 schema => ['array' => {of => 'str*'}],
75 description => <<'_',
76  
77 If the whole document doesn't have any of these tags, then the whole document
78 will be exported. Otherwise, trees that do not carry one of these tags will be
79 excluded. If a selected tree is a subtree, the heading hierarchy above it will
80 also be selected for export, but not the text below those headings.
81  
82 exclude_tags is evaluated after include_tags.
83  
84 _
85 },
86 html_title => {
87 summary => 'HTML document title, defaults to source_file',
88 schema => ['str' => {}],
89 },
90 css_url => {
91 summary => 'Add a link to CSS document',
92 schema => ['str' => {}],
93 },
94 naked => {
95 summary => 'Don\'t wrap exported HTML with HTML/HEAD/BODY elements',
96 schema => ['bool' => {}],
97 },
98 ignore_unknown_settings => {
99 schema => 'bool',
100 },
101 inline_images => {
102 summary => 'If set to true, will make link to an image filename into an <img> element instead of <a>',
103 schema => 'bool',
104 default => 1,
105 },
106 },
107 };
108 sub org_to_html {
109 2 ▶ my %args = @_;
Subroutine org_to_html: called CC=7 SCAR=24.6
Pod documented
110  
111 2 my $doc;
112 2 ▶ if ($args{source_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'source_file'}) { } 2 0 2
elsif (defined $args{'source_str'}) { } 0 0 0
113 $doc = Org::Document->new(
114 from_string => scalar read_text($args{source_file}),
115 ignore_unknown_settings => $args{ignore_unknown_settings},
116 2 );
117 } elsif (defined($args{source_str})) {
118 $doc = Org::Document->new(
119 from_string => $args{source_str},
120 ignore_unknown_settings => $args{ignore_unknown_settings},
121 0 );
122 } else {
123 0 return [400, "Please specify source_file/source_str"];
124 }
125  
126 my $obj = ($args{_class} // __PACKAGE__)->new(
127 source_file => $args{source_file} // '(source string)',
128 include_tags => $args{include_tags},
129 exclude_tags => $args{exclude_tags},
130 css_url => $args{css_url},
131 naked => $args{naked},
132 html_title => $args{html_title},
133 2 ▶ inline_images => $args{inline_images} // 1,
Condition50%3/6
$args{"_class"} // __PACKAGE__
l!l
02
$args{"source_file"} // "(source string)"
l!l
20
$args{"inline_images"} // 1
l!l
02
MC/DC0%0/1
$args{"_class"} // __PACKAGE__
$args{"_class"}
MC/DC0%0/1
$args{"source_file"} // "(source string)"
$args{"source_file"}
MC/DC0%0/1
$args{"inline_images"} // 1
$args{"inline_images"}
Truth table50%1/2
A
A= $args{"_class"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"source_file"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"inline_images"}
Aresult
11
00
134 );
135  
136 2 my $html = $obj->export($doc);
137 #$log->tracef("html = %s", $html);
138 2 ▶ if ($args{target_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'target_file'}) { } 0 2 2
139 0 write_text($args{target_file}, $html);
140 0 return [200, "OK"];
141 } else {
142 2 return [200, "OK", $html];
143 }
144 }
145  
146 sub export_document {
147 4 ▶ my ($self, $doc) = @_;
Subroutine export_document: called CC=9 SCAR=22.7
Pod documented
148  
149 4 $self->{_prev_elem_is_inline} = 0;
150  
151 4 my $html = [];
152 4 ▶ unless ($self->naked) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->naked) 1 3 4
153 1 push @$html, "<html>\n";
154 1 ▶ push @$html, (
Condition50%1/2
$VERSION // "?"
l!l
10
MC/DC0%0/1
$VERSION // "?"
$VERSION
Truth table50%1/2
A
A= $VERSION
Aresult
11
00
155 "<!-- Generated by ".__PACKAGE__,
156 " version ".($VERSION // "?"),
157 " on ".scalar(localtime)." -->\n\n");
158  
159 1 push @$html, "<head>\n";
160  
161 {
162 1 my @title_settings = $doc->settings('TITLE');
163 1 my $title_from_setting;
164 1 ▶ $title_from_setting = $title_settings[0]->raw_arg
Branch: 1 branch
BranchTrueFalseTotal
if @title_settings 0 1 1
165 if @title_settings;
166 1 ▶ push @$html, "<title>",
Condition37%3/8
(($self->html_title // $title_from_setting) // $self->source_file) // "(no title)"
l!l
10
($self->html_title // $title_from_setting) // $self->source_file
l!l&&r!l&&!r
100
$self->html_title // $title_from_setting
l!l&&r!l&&!r
100
MC/DC0%0/3
(($self->html_title // $title_from_setting) // $self->source_file) // "(no title)"
$self->html_title $title_from_setting $self->source_file
Truth table25%1/4
A // B // C
A= $self->html_titleB= $title_from_settingC= $self->source_file
ABCresult
1XX1
01X1
0011
0000
167 ($self->html_title // $title_from_setting // $self->source_file // '(no title)'),
168 "</title>\n";
169 }
170  
171 1 ▶ if ($self->css_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->css_url) 1 0 1
172 1 push @$html, (
173 "<link rel=\"stylesheet\" type=\"text/css\" href=\"",
174 $self->css_url, "\" />\n"
175 );
176 }
177 1 push @$html, "</head>\n\n";
178  
179 1 push @$html, "<body>\n";
180 }
181 4 push @$html, $self->export_elements(@{$doc->children});
182 4 ▶ unless ($self->naked) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->naked) 1 3 4
183 1 push @$html, "</body>\n\n";
184 1 push @$html, "</html>\n";
185 }
186  
187 4 join "", @$html;
188 }
189  
190 sub before_export_element {
191 21 ▶ my $self = shift;
Subroutine before_export_element: called CC=3 SCAR=11.0
Pod documented
192 21 my %args = @_;
193  
194 $self->{_prev_elem_is_inline} =
195 21 ▶ $args{elem}->can("is_inline") && $args{elem}->is_inline ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'elem'}->can('is_inline') && $args{'elem'}->is_inline ? : 8 13 21
Condition100%3/3
$args{"elem"}->can("is_inline") and $args{"elem"}->is_inline
!ll&&!rl&&r
498
MC/DC100%2/2
$args{"elem"}->can("is_inline") and $args{"elem"}->is_inline
$args{"elem"}->can("is_inline") $args{"elem"}->is_inline
Truth table100%3/3
A and B
A= $args{"elem"}->can("is_inline")B= $args{"elem"}->is_inline
ABresult
0X0
100
111
196 }
197  
198 sub export_block {
199 1 ▶ my ($self, $elem) = @_;
Subroutine export_block: called CC=1 SCAR=0.0
Pod undocumented
200 # currently all assumed to be <PRE>
201 1 join "", (
202 "<pre class=\"block block_", lc($elem->name), "\">",
203 encode_entities($elem->raw_content),
204 "</pre>\n\n"
205 );
206 }
207  
208 sub export_fixed_width_section {
209 1 ▶ my ($self, $elem) = @_;
Subroutine export_fixed_width_section: called CC=1 SCAR=0.0
Pod undocumented
210 1 join "", (
211 "<pre class=\"fixed_width_section\">",
212 encode_entities($elem->text),
213 "</pre>\n"
214 );
215 }
216  
217 sub export_comment {
218 0 ▶ my ($self, $elem) = @_;
Subroutine export_comment: not called CC=1 SCAR=6.9
Pod undocumented
219 0 join "", (
220 "<!-- ",
221 encode_entities($elem->_str),
222 " -->\n"
223 );
224 }
225  
226 sub export_drawer {
227 0 ▶ my ($self, $elem) = @_;
Subroutine export_drawer: not called CC=1 SCAR=6.9
Pod undocumented
228 # currently not exported
229 0 '';
230 }
231  
232 sub export_footnote {
233 0 ▶ my ($self, $elem) = @_;
Subroutine export_footnote: not called CC=1 SCAR=6.9
Pod undocumented
234 # currently not exported
235 0 '';
236 }
237  
238 sub export_headline {
239 5 ▶ my ($self, $elem) = @_;
Subroutine export_headline: called CC=1 SCAR=0.0
Pod undocumented
240  
241 5 my @children = $self->_included_children($elem);
242  
243 5 join "", (
244 "<h" , $elem->level, ">",
245 $self->export_elements($elem->title),
246 "</h", $elem->level, ">\n\n",
247 $self->export_elements(@children)
248 );
249 }
250  
251 sub export_list {
252 0 ▶ my ($self, $elem) = @_;
Subroutine export_list: not called CC=5 SCAR=34.0
Pod undocumented
253 0 my $tag;
254 0 my $type = $elem->type;
255 0 ▶ if ($type eq 'D') { $tag = 'dl' }
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'D') { } 0 0 0
elsif ($type eq 'O') { } 0 0 0
elsif ($type eq 'U') { } 0 0 0
256 0 elsif ($type eq 'O') { $tag = 'ol' }
257 0 elsif ($type eq 'U') { $tag = 'ul' }
258 join "", (
259 "<$tag>\n",
260 0 ▶ $self->export_elements(@{$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
261 "</$tag>\n\n"
262 );
263 }
264  
265 sub export_list_item {
266 0 ▶ my ($self, $elem) = @_;
Subroutine export_list_item: not called CC=6 SCAR=37.4
Pod undocumented
267  
268 0 my $html = [];
269 0 ▶ if ($elem->desc_term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->desc_term) { } 0 0 0
270 0 push @$html, "<dt>";
271 } else {
272 0 push @$html, "<li>";
273 }
274  
275 0 ▶ if ($elem->check_state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->check_state) 0 0 0
276 0 push @$html, "<strong>[", $elem->check_state, "]</strong>";
277 }
278  
279 0 ▶ if ($elem->desc_term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->desc_term) 0 0 0
280 0 push @$html, $self->export_elements($elem->desc_term);
281 0 push @$html, "</dt>";
282 0 push @$html, "<dd>";
283 }
284  
285 0 ▶ push @$html, $self->export_elements(@{$elem->children}) if $elem->children;
Branch: 1 branch
BranchTrueFalseTotal
if $elem->children 0 0 0
286  
287 0 ▶ if ($elem->desc_term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->desc_term) { } 0 0 0
288 0 push @$html, "</dd>\n";
289 } else {
290 0 push @$html, "</li>\n";
291 }
292  
293 0 join "", @$html;
294 }
295  
296 sub export_radio_target {
297 0 ▶ my ($self, $elem) = @_;
Subroutine export_radio_target: not called CC=1 SCAR=6.9
Pod undocumented
298 # currently not exported
299 0 '';
300 }
301  
302 sub export_setting {
303 1 ▶ my ($self, $elem) = @_;
Subroutine export_setting: called CC=1 SCAR=0.0
Pod undocumented
304 # currently not exported
305 1 '';
306 }
307  
308 sub export_table {
309 0 ▶ my ($self, $elem) = @_;
Subroutine export_table: not called CC=2 SCAR=17.9
Pod undocumented
310 join "", (
311 "<table border>\n",
312 0 ▶ $self->export_elements(@{$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
313 "</table>\n\n"
314 );
315 }
316  
317 sub export_table_row {
318 0 ▶ my ($self, $elem) = @_;
Subroutine export_table_row: not called CC=2 SCAR=17.9
Pod undocumented
319 join "", (
320 "<tr>",
321 0 ▶ $self->export_elements(@{$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
322 "</tr>\n"
323 );
324 }
325  
326 sub export_table_cell {
327 0 ▶ my ($self, $elem) = @_;
Subroutine export_table_cell: not called CC=2 SCAR=17.9
Pod undocumented
328  
329 join "", (
330 "<td>",
331 0 ▶ $self->export_elements(@{$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
332 "</td>"
333 );
334 }
335  
336 sub export_table_vline {
337 0 ▶ my ($self, $elem) = @_;
Subroutine export_table_vline: not called CC=1 SCAR=6.9
Pod undocumented
338 # currently not exported
339 0 '';
340 }
341  
342 sub __escape_target {
343 0 ▶ my $target = shift;
Subroutine __escape_target: not called CC=1 SCAR=6.9
344 0 $target =~ s/[^\w]+/_/g;
345 0 $target;
346 }
347  
348 sub export_target {
349 0 ▶ my ($self, $elem) = @_;
Subroutine export_target: not called CC=1 SCAR=6.9
Pod undocumented
350 # target
351 0 join "", (
352 "<a name=\"", __escape_target($elem->target), "\">"
353 );
354 }
355  
356 sub export_text {
357 8 ▶ my ($self, $elem) = @_;
Subroutine export_text: called CC=11 SCAR=29.5
Pod undocumented
358  
359 8 my $style = $elem->style;
360 8 my $tag;
361 8 ▶ if ($style eq 'B') { $tag = 'b' }
Branch: 6 branches
BranchTrueFalseTotal
if ($style eq 'B') { } 0 8 8
elsif ($style eq 'I') { } 0 8 8
elsif ($style eq 'U') { } 0 8 8
elsif ($style eq 'S') { } 0 8 8
elsif ($style eq 'C') { } 0 8 8
elsif ($style eq 'V') { } 0 8 8
362 0 elsif ($style eq 'I') { $tag = 'i' }
363 0 elsif ($style eq 'U') { $tag = 'u' }
364 0 elsif ($style eq 'S') { $tag = 'strike' }
365 0 elsif ($style eq 'C') { $tag = 'code' }
366 0 elsif ($style eq 'V') { $tag = 'tt' }
367  
368 8 my $html = [];
369  
370 8 ▶ push @$html, "<$tag>" if $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $tag 0 8 8
371 8 my $text = encode_entities($elem->text);
372 8 $text =~ s/\R\R+/\n\n<p>/g;
373 8 ▶ if ($self->{_prev_elem_is_inline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_prev_elem_is_inline"}) 8 0 8
374 8 $text =~ s/\A\R/ /;
375 }
376 8 $text =~ s/(?<=.)\R/ /g;
377 8 push @$html, $text;
378 8 ▶ push @$html, $self->export_elements(@{$elem->children}) if $elem->children;
Branch: 1 branch
BranchTrueFalseTotal
if $elem->children 0 8 8
379 8 ▶ push @$html, "</$tag>" if $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $tag 0 8 8
380  
381 8 join "", @$html;
382 }
383  
384 sub export_time_range {
385 0 ▶ my ($self, $elem) = @_;
Subroutine export_time_range: not called CC=1 SCAR=6.9
Pod undocumented
386  
387 0 encode_entities($elem->as_string);
388 }
389  
390 sub export_timestamp {
391 0 ▶ my ($self, $elem) = @_;
Subroutine export_timestamp: not called CC=1 SCAR=6.9
Pod undocumented
392  
393 0 encode_entities($elem->as_string);
394 }
395  
396 sub export_link {
397 0 ▶ require Filename::Image;
Subroutine export_link: not called CC=11 SCAR=48.8
Pod undocumented
398 0 require URI;
399  
400 0 my ($self, $elem) = @_;
401  
402 0 my $html = [];
403 0 my $link = $elem->link;
404 0 my $looks_like_image = Filename::Image::check_image_filename(filename => $link);
405 0 my $inline_images = $self->inline_images;
406  
407 0 ▶ if ($inline_images && $looks_like_image) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inline_images and $looks_like_image) { } 0 0 0
Condition0%0/3
$inline_images and $looks_like_image
!ll&&!rl&&r
000
MC/DC0%0/2
$inline_images and $looks_like_image
$inline_images $looks_like_image
Truth table0%0/3
A and B
A= $inline_imagesB= $looks_like_image
ABresult
0X0
100
111
408 # TODO: extract to method e.g. settings
409 0 my $elem_settings;
410 0 my $s = $elem;
411 0 while (1) {
412 0 $s = $s->prev_sibling;
413 0 ▶ last unless $s && $s->isa("Org::Element::Setting");
Branch: 1 branch
BranchTrueFalseTotal
unless $s and $s->isa("Org::Element::Setting") 0 0 0
Condition0%0/3
$s and $s->isa("Org::Element::Setting")
!ll&&!rl&&r
000
MC/DC0%0/2
$s and $s->isa("Org::Element::Setting")
$s $s->isa("Org::Element::Setting")
Truth table0%0/3
A and B
A= $sB= $s->isa("Org::Element::Setting")
ABresult
0X0
100
111
414 0 $elem_settings->{ $s->name } = $s->raw_arg;
415 }
416 #use DD; dd $settings;
417 0 my $caption = $elem_settings->{CAPTION};
418  
419 # TODO: extract to method e.g. settings of Org::Document
420 0 my $doc_settings;
421 0 $s = $elem->document->children->[0];
422 0 while (1) {
423 0 $s = $s->next_sibling;
424 0 ▶ last unless $s && $s->isa("Org::Element::Setting");
Branch: 1 branch
BranchTrueFalseTotal
unless $s and $s->isa("Org::Element::Setting") 0 0 0
Condition0%0/3
$s and $s->isa("Org::Element::Setting")
!ll&&!rl&&r
000
MC/DC0%0/2
$s and $s->isa("Org::Element::Setting")
$s $s->isa("Org::Element::Setting")
Truth table0%0/3
A and B
A= $sB= $s->isa("Org::Element::Setting")
ABresult
0X0
100
111
425 0 $doc_settings->{ $s->name } = $s->raw_arg;
426 }
427 #use DD; dd $settings;
428 0 my $img_base = $doc_settings->{IMAGE_BASE};
429  
430 0 ▶ my $url = defined($img_base) ? URI->new($link)->abs(URI->new($img_base)) : $link;
Branch: 1 branch
BranchTrueFalseTotal
defined $img_base ? : 0 0 0
431  
432 0 ▶ push @$html, "<figure>" if defined $caption;
Branch: 1 branch
BranchTrueFalseTotal
if defined $caption 0 0 0
433 0 push @$html, "<img src=\"";
434 0 push @$html, "$url";
435 0 push @$html, "\" />";
436 0 push @$html, "<figcaption>", encode_entities($caption), "</figcaption>";
437 0 ▶ push @$html, "</figure>" if defined $caption;
Branch: 1 branch
BranchTrueFalseTotal
if defined $caption 0 0 0
438 } else {
439 0 push @$html, "<a href=\"";
440 0 push @$html, $link;
441 0 push @$html, "\">";
442 0 ▶ if ($elem->description) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->description) { } 0 0 0
443 0 push @$html, $self->export_elements($elem->description);
444 } else {
445 0 push @$html, $link;
446 }
447 0 push @$html, "</a>";
448 }
449  
450 0 join "", @$html;
451 }
452  
453 3 1;
454 # ABSTRACT: Export Org document to HTML
455  
456 __END__
457  
458 =pod
459  
460 =encoding UTF-8
461  
462 =head1 NAME
463  
464 Org::To::HTML - Export Org document to HTML
465  
466 =head1 VERSION
467  
468 This document describes version 0.235 of Org::To::HTML (from Perl distribution Org-To-HTML), released on 2022-02-12.
469  
470 =head1 SYNOPSIS
471  
472 use Org::To::HTML qw(org_to_html);
473  
474 # non-OO interface
475 my $res = org_to_html(
476 source_file => 'todo.org', # or source_str
477 #target_file => 'todo.html', # defaults return the HTML in $res->[2]
478 #html_title => 'My Todo List', # defaults to file name
479 #include_tags => [...], # default exports all tags.
480 #exclude_tags => [...], # behavior mimics emacs's include/exclude rule
481 #css_url => '/path/to/my/style.css', # default none
482 #naked => 0, # if set to 1, no HTML/HEAD/BODY will be output.
483 );
484 die "Failed" unless $res->[0] == 200;
485  
486 # OO interface
487 my $oeh = Org::To::HTML->new();
488 my $html = $oeh->export($doc); # $doc is Org::Document object
489  
490 =head1 DESCRIPTION
491  
492 Export Org format to HTML. To customize, you can subclass this module.
493  
494 A command-line utility L<org-to-html> is available in the distribution
495 L<App::OrgUtils>.
496  
497 Note that this module is just a simple exporter, for "serious" work you'll
498 probably want to use the exporting features or L<org-mode|http://orgmode.org>.
499  
500 =head1 new(%args)
501  
502 =head2 $exp->export_document($doc) => HTML
503  
504 Export document to HTML.
505  
506 =head1 FUNCTIONS
507  
508  
509 =head2 org_to_html
510  
511 Usage:
512  
513 org_to_html(%args) -> [$status_code, $reason, $payload, \%result_meta]
514  
515 Export Org document to HTML.
516  
517 This is the non-OO interface. For more customization, consider subclassing
518 Org::To::HTML.
519  
520 This function is not exported by default, but exportable.
521  
522 Arguments ('*' denotes required arguments):
523  
524 =over 4
525  
526 =item * B<css_url> => I<str>
527  
528 Add a link to CSS document.
529  
530 =item * B<exclude_tags> => I<array[str]>
531  
532 Exclude trees that carry one of these tags.
533  
534 If the whole document doesn't have any of these tags, then the whole document
535 will be exported. Otherwise, trees that do not carry one of these tags will be
536 excluded. If a selected tree is a subtree, the heading hierarchy above it will
537 also be selected for export, but not the text below those headings.
538  
539 exclude_tags is evaluated after include_tags.
540  
541 =item * B<html_title> => I<str>
542  
543 HTML document title, defaults to source_file.
544  
545 =item * B<ignore_unknown_settings> => I<bool>
546  
547 =item * B<include_tags> => I<array[str]>
548  
549 Include trees that carry one of these tags.
550  
551 Works like Org's 'org-export-select-tags' variable. If the whole document
552 doesn't have any of these tags, then the whole document will be exported.
553 Otherwise, trees that do not carry one of these tags will be excluded. If a
554 selected tree is a subtree, the heading hierarchy above it will also be selected
555 for export, but not the text below those headings.
556  
557 =item * B<inline_images> => I<bool> (default: 1)
558  
559 If set to true, will make link to an image filename into an <imgE<gt> element instead of <aE<gt>.
560  
561 =item * B<naked> => I<bool>
562  
563 Don't wrap exported HTML with HTMLE<sol>HEADE<sol>BODY elements.
564  
565 =item * B<source_file> => I<str>
566  
567 Source Org file to export.
568  
569 =item * B<source_str> => I<str>
570  
571 Alternatively you can specify Org string directly.
572  
573 =item * B<target_file> => I<str>
574  
575 HTML file to write to.
576  
577 If not specified, HTML string will be returned.
578  
579  
580 =back
581  
582 Returns an enveloped result (an array).
583  
584 First element ($status_code) is an integer containing HTTP-like status code
585 (200 means OK, 4xx caller error, 5xx function error). Second element
586 ($reason) is a string containing error message, or something like "OK" if status is
587 200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth
588 element (%result_meta) is called result metadata and is optional, a hash
589 that contains extra information, much like how HTTP response headers provide additional metadata.
590  
591 Return value: (any)
592  
593 =for Pod::Coverage ^(export_.+|before_.+|after_.+)$
594  
595 =head1 ATTRIBUTES
596  
597 =head2 naked => BOOL
598  
599 If set to true, export_document() will not output HTML/HEAD/BODY wrapping
600 element. Default is false.
601  
602 =head2 html_title => STR
603  
604 Title to use in TITLE HTML element, to override C<#+TITLE> setting in the Org
605 document. If unset and document does not have C<#+TITLE> setting, will default
606 to the name of the source file, or C<(source string)>.
607  
608 =head2 css_url => STR
609  
610 If set, export_document() will output a LINK element pointing to this CSS.
611  
612 =head1 METHODS
613  
614 =head1 FAQ
615  
616 =head2 Why would one want to use this instead of org-mode's built-in exporting features?
617  
618 This module might come in handy if you want to customize the Org-to-HTML
619 translation with Perl, for example when you want to customize the default HTML
620 title when there's no C<#+TITLE> setting, change translation of table element to
621 an ASCII table, etc.
622  
623 =head1 HOMEPAGE
624  
625 Please visit the project's homepage at L<https://metacpan.org/release/Org-To-HTML>.
626  
627 =head1 SOURCE
628  
629 Source repository is at L<https://github.com/perlancar/perl-Org-To-HTML>.
630  
631 =head1 SEE ALSO
632  
633 For more information about Org document format, visit http://orgmode.org/
634  
635 L<Org::Parser>
636  
637 L<org-to-html>
638  
639 =head1 AUTHOR
640  
641 perlancar <perlancar@cpan.org>
642  
643 =head1 CONTRIBUTORS
644  
645 =for stopwords Harald Jörg Steven Haryanto
646  
647 =over 4
648  
649 =item *
650  
651 Harald Jörg <Harald.Joerg@arcor.de>
652  
653 =item *
654  
655 Steven Haryanto <stevenharyanto@gmail.com>
656  
657 =back
658  
659 =head1 CONTRIBUTING
660  
661  
662 To contribute, you can send patches by email/via RT, or send pull requests on
663 GitHub.
664  
665 Most of the time, you don't need to build the distribution yourself. You can
666 simply modify the code, then test via:
667  
668 % prove -l
669  
670 If you want to build the distribution (e.g. to try to install it locally on your
671 system), you can install L<Dist::Zilla>,
672 L<Dist::Zilla::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
673 Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required
674 beyond that are considered a bug and can be reported to me.
675  
676 =head1 COPYRIGHT AND LICENSE
677  
678 This software is copyright (c) 2022, 2020, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011 by perlancar <perlancar@cpan.org>.
679  
680 This is free software; you can redistribute it and/or modify it under
681 the same terms as the Perl 5 programming language system itself.
682  
683 =head1 BUGS
684  
685 Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Org-To-HTML>
686  
687 When submitting a bug or request, please include a test-file or a
688 patch to an existing test-file that illustrates the bug or desired
689 feature.
690  
691 =cut
↑ Summary Base.pm »