blib/lib/Markdown/ODF.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Markdown::ODF;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Markdown::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Markdown::Parser::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use ODF::lpOD;
Subroutine BEGIN: called CC=1 SCAR=2.6
12  
13 our $VERSION = '0.02';
14  
15 {
16 # Silence warnings from ODF package
17 package ODF::lpOD::Element;
18 sub DESTROY {}
19 }
20  
21 =head1 NAME
22  
23 Markdown::ODF - Create ODF documents from Markdown
24  
25 =head1 SYNOPSIS
26  
27 use Markdown::ODF;
28  
29 my $convert = Markdown::ODF->new;
30  
31 # Optionally use PDF document directly
32 my $odf = $convert->odf;
33 my $meta = $odf->meta;
34 $meta->set_title("Title for converted document");
35  
36 # Optionally set default paragraph style for document
37 my $default = odf_create_style(
38 'paragraph',
39 area => 'text',
40 language => 'en',
41 country => 'GB',
42 size => '11pt',
43 font => 'Arial',
44 );
45 $odf->insert_style($default, default => TRUE);
46  
47 # Add content
48 $convert->add_markdown("My markdown with some **bold text**");
49  
50 =head1 DESCRIPTION
51  
52 This module converts Markdown to ODF text documents. The ODF document is
53 accessed using the L</"odf"> method which returns a L<ODF::lpOD> object
54 allowing further manipulation of the document.
55  
56 =head1 METHODS
57  
58 =cut
59  
60 =head2 odf
61  
62 Returns the L<ODF::lpOD> object used for the ODF document.
63  
64 =cut
65  
66 has odf => (
67 is => 'lazy',
68 );
69  
70 sub _build_odf
71 { my $self = shift;
72  
73 my $odf = odf_new_document('text');
74 my $meta = $odf->meta;
75  
76 $meta->set_title("New Title");
77 $meta->set_modification_date;
78 $meta->set_creator('Andy');
79  
80 $odf->insert_style(
81 odf_create_style('text', name => "bold", weight => 'bold')
82 );
83 $odf->insert_style(
84 odf_create_style('text', name => "italic", style => 'italic')
85 );
86  
87 $odf->insert_style(
88 odf_create_style(
89 'paragraph',
90 name => 'Activ Paragraph',
91 parent => 'Standard',
92 'margin bottom' => '0.5cm',
93 )
94 );
95 $odf->insert_style(
96 odf_create_style(
97 'paragraph',
98 name => 'Activ Paragraph List',
99 parent => 'Standard',
100 'margin bottom' => '0.5cm',
101 'style:contextual-spacing' => "true", # Only spacing after list, not between items
102 )
103 );
104  
105 my $ls = odf_create_style('list', name => "ordered");
106 $ls->set_level_style(
107 1,
108 type => 'number',
109 format => '1',
110 suffix => '. '
111 );
112 $odf->insert_style($ls);
113  
114 $ls = odf_create_style('list', name => "unordered");
115 my $c = '•';
116 utf8::encode($c);
117 $ls->set_level_style(
118 1, type => 'bullet', character => $c
119 );
120 $odf->insert_style($ls);
121  
122 $odf;
123 }
124  
125 has _parser => (
126 is => 'lazy',
127 );
128  
129 sub _build__parser
130 { my $self = shift;
131 Markdown::Parser->new;
132 }
133  
134 =head2 add_markdown($markdown)
135  
136 Add markdown content as a paragraph to the current ODF page.
137  
138 =cut
139  
140 sub add_markdown
141 { my ($self, $md) = @_;
142  
143 # Markdown::Parser is slow, therefore try and shortcut simple text.
144 #
145 # Plain text, no Markdown formatting (format characters and numbered
146 # lists):
147 if ($md !~ /[-#=*_>+]+/ && $md !~ /[0-9]+\./)
148 {
149 $self->_print($md);
150 }
151 elsif ($md =~ /^\h*(#{1,3})\h*(.*)/) # Headings
152 {
153 my $level = $1 eq '#' ? 1 : $1 eq '##' ? 2 : 3;
154 my $text = $2;
155 my $header = odf_heading->create(
156 level => $level,
157 style => "Heading_20_$level",
158 );
159 $self->_print($text, header => $header);
160 }
161 else {
162 # Anything else, parse as Markdown:
163 my $doc = $self->_parser->parse($md);
164 $self->_print($doc);
165 }
166 }
167  
168 has _stack => (
169 is => 'ro',
170 default => sub { [] },
171 );
172  
173 has _all_text => (
174 is => 'ro',
175 lazy => 1,
176 builder => sub { [] },
177 clearer => 1,
178 );
179  
180 =head2 current_element
181  
182 Returns the most recent L<ODF::lpOD::Element> that has been written to the document.
183  
184 =cut
185  
186 has current_element => (
187 is => 'rwp',
188 );
189  
190 sub _para
191 { my ($self, %options) = @_;
192 my @s; my $pos = 0;
193 my $text = '';
194 my $to_add;
195 my $list = $options{list};
196 my $header = $options{header};
197 foreach my $item (@{$self->_all_text})
198 {
199 $text .= $item->{text};
200 push @s, {
201 offset => $pos,
202 length => length $item->{text},
203 style => $item->{styles},
204 link => $item->{link},
205 };
206 $pos += length $item->{text};
207 }
208 return if $text =~ /^\s*$/; # Do not print empty paragraphs
209 utf8::encode($text);
210 my $e;
211 if ($header)
212 {
213 $header->set_text($text);
214 $e = $header;
215 }
216 else {
217 $e = odf_paragraph->create(
218 text => $text,
219 style => $list ? "Activ Paragraph List" : "Activ Paragraph",
220 );
221 }
222 foreach my $s (@s)
223 {
224 foreach my $ss (@{$s->{style}})
225 {
226 if ($ss eq 'bold')
227 {
228 $e->set_span(
229 offset => $s->{offset},
230 length => $s->{length},
231 style => 'bold',
232 );
233 }
234 elsif ($ss eq 'emphasis')
235 {
236 $e->set_span(
237 offset => $s->{offset},
238 length => $s->{length},
239 style => 'italic',
240 );
241 }
242 elsif ($ss eq 'list')
243 {
244 }
245 elsif ($ss eq 'listnum')
246 {
247 }
248 elsif ($ss eq 'link')
249 {
250 $e->set_hyperlink(
251 offset => $s->{offset},
252 length => $s->{length},
253 url => $s->{link},
254 );
255 }
256 elsif ($ss eq 'listitem')
257 {
258 my $item = $list->add_item;
259 $item->append_element($e);
260 $to_add = $list;
261 return;
262 }
263 else {
264 confess "Unknown style: $ss";
265 }
266 }
267 }
268 $to_add ||= $e;
269 $self->append_element($to_add);
270 }
271  
272 =head2 append_element($element)
273  
274 Add a ODF::lpOD::Element to the current document and update L</"current_element">.
275  
276 =cut
277  
278 sub append_element
279 { my ($self, $element) = @_;
280 $self->current_element ? $self->current_element->insert_element($element, position => NEXT_SIBLING) : $self->odf->body->insert_element($element);
281 $self->_set_current_element($element);
282 $self;
283 }
284  
285 sub _print
286 { my ($self, $item, %options) = @_;
287  
288 if (!ref $item) # Plain text, see comments above to shortcut parsing
289 {
290 push @{$self->_all_text}, {
291 text => $item,
292 styles => [],
293 };
294 # Print para
295 $self->_para(%options);
296 $self->_clear_all_text;
297 }
298 elsif (!$item->isa('Markdown::Parser::Text'))
299 {
300 my $ref = ref $item;
301 $ref =~ s/^Markdown::Parser:://;
302 my $has_style; my $list = $options{list};
303 my $header = $options{header}; my $link;
304 if ($ref eq 'List')
305 {
306 $list = odf_list->create(style => $item->order ? 'ordered' : 'unordered');
307 $self->append_element($list);
308 }
309 elsif ($ref eq 'Header')
310 {
311 $header = odf_heading->create(
312 level => $item->level,
313 style => "Heading_20_".$item->level,
314 );
315 }
316 elsif ($ref =~ /^(Emphasis|Bold|List|ListItem|Link)$/)
317 {
318 push @{$self->_stack}, lc $ref;
319 $has_style = 1;
320 $link = $item->url if $ref eq 'Link';
321 }
322 $item->children->for(sub{
323 my ($i, $item2) = @_;
324 $self->_print($item2, list => $list, header => $header, last => $i == $item->children->size && ($ref eq 'Paragraph' || $ref eq 'Header' || $ref eq 'ListItem'), link => $link);
325 return 1;
326 });
327 pop @{$self->_stack} if $has_style;
328 }
329 else {
330 my $text = $item->text or return;
331  
332 $text =~ s/\s+$// if $options{list} && $options{last};
333 if ($text !~ /^\s+$/)
334 {
335 push @{$self->_all_text}, {
336 text => $text,
337 styles => [@{$self->_stack}],
338 link => $options{link},
339 };
340 }
341 if ($text =~ s/\n$// || $options{last})
342 {
343 # Print para
344 $self->_para(%options);
345 $self->_clear_all_text;
346 }
347 }
348  
349 }
350  
351 =head1 LICENSE AND COPYRIGHT
352  
353 Copyright (c) 2022 Amtivo Group
354  
355 This program is free software, licensed under the MIT licence.
356  
357 =cut
358  
359 1;
↑ Summary