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