| 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.01'; | ||
| 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 | =head2 add_markdown($markdown) | ||
| 126 | |||
| 127 | Add markdown content as a paragraph to the current ODF page. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub add_markdown | ||
| 132 | { my ($self, $md) = @_; | ||
| 133 | my $parser = Markdown::Parser->new; | ||
| 134 | my $doc = $parser->parse($md); | ||
| 135 | $self->_print($doc); | ||
| 136 | } | ||
| 137 | |||
| 138 | has _stack => ( | ||
| 139 | is => 'ro', | ||
| 140 | default => sub { [] }, | ||
| 141 | ); | ||
| 142 | |||
| 143 | has _all_text => ( | ||
| 144 | is => 'ro', | ||
| 145 | lazy => 1, | ||
| 146 | builder => sub { [] }, | ||
| 147 | clearer => 1, | ||
| 148 | ); | ||
| 149 | |||
| 150 | =head2 current_element | ||
| 151 | |||
| 152 | Returns the most recent L<ODF::lpOD::Element> that has been written to the document. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | has current_element => ( | ||
| 157 | is => 'rwp', | ||
| 158 | ); | ||
| 159 | |||
| 160 | sub _para | ||
| 161 | { my ($self, %options) = @_; | ||
| 162 | my @s; my $pos = 0; | ||
| 163 | my $text = ''; | ||
| 164 | my $to_add; | ||
| 165 | my $list = $options{list}; | ||
| 166 | my $header = $options{header}; | ||
| 167 | foreach my $item (@{$self->_all_text}) | ||
| 168 | { | ||
| 169 | $text .= $item->{text}; | ||
| 170 | push @s, { | ||
| 171 | offset => $pos, | ||
| 172 | length => length $item->{text}, | ||
| 173 | style => $item->{styles}, | ||
| 174 | link => $item->{link}, | ||
| 175 | }; | ||
| 176 | $pos += length $item->{text}; | ||
| 177 | } | ||
| 178 | return if $text =~ /^\s*$/; # Do not print empty paragraphs | ||
| 179 | utf8::encode($text); | ||
| 180 | my $e; | ||
| 181 | if ($header) | ||
| 182 | { | ||
| 183 | $header->set_text($text); | ||
| 184 | $e = $header; | ||
| 185 | } | ||
| 186 | else { | ||
| 187 | $e = odf_paragraph->create( | ||
| 188 | text => $text, | ||
| 189 | style => $list ? "Activ Paragraph List" : "Activ Paragraph", | ||
| 190 | ); | ||
| 191 | } | ||
| 192 | foreach my $s (@s) | ||
| 193 | { | ||
| 194 | foreach my $ss (@{$s->{style}}) | ||
| 195 | { | ||
| 196 | if ($ss eq 'bold') | ||
| 197 | { | ||
| 198 | $e->set_span( | ||
| 199 | offset => $s->{offset}, | ||
| 200 | length => $s->{length}, | ||
| 201 | style => 'bold', | ||
| 202 | ); | ||
| 203 | } | ||
| 204 | elsif ($ss eq 'emphasis') | ||
| 205 | { | ||
| 206 | $e->set_span( | ||
| 207 | offset => $s->{offset}, | ||
| 208 | length => $s->{length}, | ||
| 209 | style => 'italic', | ||
| 210 | ); | ||
| 211 | } | ||
| 212 | elsif ($ss eq 'list') | ||
| 213 | { | ||
| 214 | } | ||
| 215 | elsif ($ss eq 'listnum') | ||
| 216 | { | ||
| 217 | } | ||
| 218 | elsif ($ss eq 'link') | ||
| 219 | { | ||
| 220 | $e->set_hyperlink( | ||
| 221 | offset => $s->{offset}, | ||
| 222 | length => $s->{length}, | ||
| 223 | url => $s->{link}, | ||
| 224 | ); | ||
| 225 | } | ||
| 226 | elsif ($ss eq 'listitem') | ||
| 227 | { | ||
| 228 | my $item = $list->add_item; | ||
| 229 | $item->append_element($e); | ||
| 230 | $to_add = $list; | ||
| 231 | return; | ||
| 232 | } | ||
| 233 | else { | ||
| 234 | confess "Unknown style: $ss"; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | $to_add ||= $e; | ||
| 239 | $self->append_element($to_add); | ||
| 240 | } | ||
| 241 | |||
| 242 | =head2 append_element($element) | ||
| 243 | |||
| 244 | Add a ODF::lpOD::Element to the current document and update L</"current_element()">. | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | sub append_element | ||
| 249 | { my ($self, $element) = @_; | ||
| 250 | $self->current_element ? $self->current_element->insert_element($element, position => NEXT_SIBLING) : $self->odf->body->insert_element($element); | ||
| 251 | $self->_set_current_element($element); | ||
| 252 | $self; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub _print | ||
| 256 | { my ($self, $item, %options) = @_; | ||
| 257 | |||
| 258 | if (!$item->isa('Markdown::Parser::Text')) | ||
| 259 | { | ||
| 260 | my $ref = ref $item; | ||
| 261 | $ref =~ s/^Markdown::Parser:://; | ||
| 262 | my $has_style; my $list = $options{list}; | ||
| 263 | my $header = $options{header}; my $link; | ||
| 264 | if ($ref eq 'List') | ||
| 265 | { | ||
| 266 | $list = odf_list->create(style => $item->order ? 'ordered' : 'unordered'); | ||
| 267 | $self->append_element($list); | ||
| 268 | } | ||
| 269 | elsif ($ref eq 'Header') | ||
| 270 | { | ||
| 271 | $header = odf_heading->create( | ||
| 272 | level => $item->level, | ||
| 273 | style => "Heading_20_".$item->level, | ||
| 274 | ); | ||
| 275 | } | ||
| 276 | elsif ($ref =~ /^(Emphasis|Bold|List|ListItem|Link)$/) | ||
| 277 | { | ||
| 278 | push @{$self->_stack}, lc $ref; | ||
| 279 | $has_style = 1; | ||
| 280 | $link = $item->url if $ref eq 'Link'; | ||
| 281 | } | ||
| 282 | $item->children->for(sub{ | ||
| 283 | my ($i, $item2) = @_; | ||
| 284 | $self->_print($item2, list => $list, header => $header, last => $i == $item->children->size && ($ref eq 'Paragraph' || $ref eq 'Header' || $ref eq 'ListItem'), link => $link); | ||
| 285 | return 1; | ||
| 286 | }); | ||
| 287 | pop @{$self->_stack} if $has_style; | ||
| 288 | } | ||
| 289 | else { | ||
| 290 | my $text = $item->text or return; | ||
| 291 | |||
| 292 | $text =~ s/\s+$// if $options{list} && $options{last}; | ||
| 293 | if ($text !~ /^\s+$/) | ||
| 294 | { | ||
| 295 | push @{$self->_all_text}, { | ||
| 296 | text => $text, | ||
| 297 | styles => [@{$self->_stack}], | ||
| 298 | link => $options{link}, | ||
| 299 | }; | ||
| 300 | } | ||
| 301 | if ($text =~ s/\n$// || $options{last}) | ||
| 302 | { | ||
| 303 | # Print para | ||
| 304 | $self->_para(%options); | ||
| 305 | $self->_clear_all_text; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | } | ||
| 310 | |||
| 311 | =head1 LICENSE AND COPYRIGHT | ||
| 312 | |||
| 313 | Copyright (c) 2022 Amtivo Group | ||
| 314 | |||
| 315 | This program is free software, licensed under the MIT licence. | ||
| 316 | |||
| 317 | =cut | ||
| 318 | |||
| 319 | 1; | ||