| 1 | package Markdown::Render; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use Data::Dumper; | ||
| 8 | use English qw(-no_match_vars); | ||
| 9 | use HTTP::Tiny; | ||
| 10 | use IO::Scalar; | ||
| 11 | use JSON; | ||
| 12 | use List::Util qw(none); | ||
| 13 | |||
| 14 | our $VERSION = '2.0.4'; | ||
| 15 | |||
| 16 | use parent qw(Class::Accessor::Fast); | ||
| 17 | |||
| 18 | __PACKAGE__->follow_best_practice; | ||
| 19 | |||
| 20 | __PACKAGE__->mk_accessors( | ||
| 21 | qw( | ||
| 22 | body | ||
| 23 | css | ||
| 24 | engine | ||
| 25 | git_email | ||
| 26 | git_user | ||
| 27 | html | ||
| 28 | infile | ||
| 29 | markdown | ||
| 30 | mode | ||
| 31 | no_title | ||
| 32 | raw | ||
| 33 | render | ||
| 34 | title | ||
| 35 | ) | ||
| 36 | ); | ||
| 37 | |||
| 38 | use Readonly; | ||
| 39 | |||
| 40 | Readonly::Scalar our $GITHUB_API => 'https://api.github.com/markdown'; | ||
| 41 | Readonly::Scalar our $EMPTY => q{}; | ||
| 42 | Readonly::Scalar our $SPACE => q{ }; | ||
| 43 | Readonly::Scalar our $TOC_TITLE => 'Table of Contents'; | ||
| 44 | Readonly::Scalar our $TOC_BACK => 'Back to Table of Contents'; | ||
| 45 | Readonly::Scalar our $DEFAULT_CSS => 'https://cdn.simplecss.org/simple-v1.css'; | ||
| 46 | Readonly::Scalar our $TRUE => 1; | ||
| 47 | Readonly::Scalar our $FALSE => 0; | ||
| 48 | |||
| 49 | caller or __PACKAGE__->main; | ||
| 50 | |||
| 51 | ######################################################################## | ||
| 52 | sub new { | ||
| 53 | ######################################################################## | ||
| 54 | my ( $class, @args ) = @_; | ||
| 55 | |||
| 56 | my %options = ref $args[0] ? %{ $args[0] } : @args; | ||
| 57 | |||
| 58 | $options{title} //= $TOC_TITLE; | ||
| 59 | $options{css} //= $options{nocss} ? $EMPTY : $DEFAULT_CSS; | ||
| 60 | $options{mode} //= 'markdown'; | ||
| 61 | $options{engine} //= 'github'; | ||
| 62 | |||
| 63 | my $self = $class->SUPER::new( \%options ); | ||
| 64 | |||
| 65 | if ( $self->get_infile ) { | ||
| 66 | open my $fh, '<', $self->get_infile | ||
| 67 | or die 'could not open ' . $self->get_infile; | ||
| 68 | |||
| 69 | local $RS = undef; | ||
| 70 | |||
| 71 | $self->set_markdown(<$fh>); | ||
| 72 | |||
| 73 | close $fh; | ||
| 74 | } | ||
| 75 | |||
| 76 | return $self; | ||
| 77 | } | ||
| 78 | |||
| 79 | ######################################################################## | ||
| 80 | sub toc_back { | ||
| 81 | ######################################################################## | ||
| 82 | my ($self) = @_; | ||
| 83 | |||
| 84 | my $back_link = lc $self->get_title; | ||
| 85 | $back_link =~ s/\s/-/gxsm; | ||
| 86 | |||
| 87 | return $back_link; | ||
| 88 | } | ||
| 89 | |||
| 90 | ######################################################################## | ||
| 91 | sub back_to_toc { | ||
| 92 | ######################################################################## | ||
| 93 | my ( $self, $message ) = @_; | ||
| 94 | |||
| 95 | $message ||= $TOC_BACK; | ||
| 96 | |||
| 97 | $message =~ s/[(]\"?(.*?)\"?[)]/$1/xsm; | ||
| 98 | |||
| 99 | return sprintf '[%s](#%s)', $message, $self->toc_back; | ||
| 100 | } | ||
| 101 | |||
| 102 | ######################################################################## | ||
| 103 | sub finalize_markdown { | ||
| 104 | ######################################################################## | ||
| 105 | my ($self) = @_; | ||
| 106 | |||
| 107 | my $markdown = $self->get_markdown; | ||
| 108 | |||
| 109 | die "no markdown yet\n" | ||
| 110 | if !$markdown; | ||
| 111 | |||
| 112 | my $fh = IO::Scalar->new( \$markdown ); | ||
| 113 | |||
| 114 | my $final_markdown; | ||
| 115 | my $in_code_block; | ||
| 116 | |||
| 117 | while ( my $line = <$fh> ) { | ||
| 118 | |||
| 119 | if ( $line =~ /\A\s*[`]{3}\s*\z/xsm ) { | ||
| 120 | $in_code_block ^= 1; | ||
| 121 | } | ||
| 122 | |||
| 123 | if ($in_code_block) { | ||
| 124 | $final_markdown .= $line; | ||
| 125 | next; | ||
| 126 | } | ||
| 127 | |||
| 128 | $line =~ s/^\!\#/\#/xsm; # ! used to prevent including header in TOC | ||
| 129 | |||
| 130 | if ( $line =~ /^\@TOC\@/xsm ) { | ||
| 131 | my $toc = $self->create_toc; | ||
| 132 | |||
| 133 | chomp $toc; | ||
| 134 | |||
| 135 | $line =~ s/\@TOC\@/$toc/xsm; | ||
| 136 | } | ||
| 137 | |||
| 138 | my $title = $self->get_title; | ||
| 139 | |||
| 140 | if ( $line =~ /\@TOC_TITLE\@/xsm ) { | ||
| 141 | $line =~ s/\@TOC_TITLE\@/$title/xsm; | ||
| 142 | } | ||
| 143 | |||
| 144 | my $git_user = $self->get_git_user // 'anonymouse'; | ||
| 145 | my $git_email = $self->get_git_email // 'anonymouse@example.com'; | ||
| 146 | |||
| 147 | if ( $line =~ /\@GIT_(USER|EMAIL)\@/xsm ) { | ||
| 148 | $line =~ s/\@GIT_USER\@/$git_user/xsm; | ||
| 149 | |||
| 150 | $line =~ s/\@GIT_EMAIL\@/$git_email/xsm; | ||
| 151 | } | ||
| 152 | |||
| 153 | my $date; | ||
| 154 | |||
| 155 | while ( $line =~ /\@DATE[(]?(.*?)[)]?\@/xsm ) { | ||
| 156 | my $format = $1 ? $1 : '%Y-%m-%d'; | ||
| 157 | |||
| 158 | $date = $self->format_date($format); | ||
| 159 | |||
| 160 | $line =~ s /\@DATE[(]?(.*?)[)]?\@/$date/xsm; | ||
| 161 | } | ||
| 162 | |||
| 163 | if ( $line =~ /\@TOC_BACK[(]?(.*?)[)]?\@/xsm ) { | ||
| 164 | my $back = $self->back_to_toc($1); | ||
| 165 | |||
| 166 | $line =~ s/\@TOC_BACK[(]?(.*?)[)]?\@/$back/xsm; | ||
| 167 | } | ||
| 168 | |||
| 169 | $final_markdown .= $line; | ||
| 170 | } | ||
| 171 | |||
| 172 | close $fh; | ||
| 173 | |||
| 174 | $self->set_markdown($final_markdown); | ||
| 175 | |||
| 176 | return $self; | ||
| 177 | |||
| 178 | } | ||
| 179 | |||
| 180 | ######################################################################## | ||
| 181 | sub _render_with_text_markdown { | ||
| 182 | ######################################################################## | ||
| 183 | my ($self) = @_; | ||
| 184 | |||
| 185 | eval { require Text::Markdown::Discount; }; | ||
| 186 | |||
| 187 | if ($EVAL_ERROR) { | ||
| 188 | carp "no Text::Markdown::Discount available...using GitHub API.\n$EVAL_ERROR"; | ||
| 189 | return $self->_render_with_github; | ||
| 190 | } | ||
| 191 | |||
| 192 | my $markdown = $self->get_markdown; | ||
| 193 | my $html = Text::Markdown::Discount::markdown($markdown); | ||
| 194 | |||
| 195 | if ( $self->get_raw ) { | ||
| 196 | $self->set_html($html); | ||
| 197 | } | ||
| 198 | else { | ||
| 199 | $self->fix_anchors( Text::Markdown::Discount::markdown($markdown) ); | ||
| 200 | } | ||
| 201 | |||
| 202 | return $self; | ||
| 203 | } | ||
| 204 | |||
| 205 | ######################################################################## | ||
| 206 | sub fix_anchors { | ||
| 207 | ######################################################################## | ||
| 208 | my ( $self, $html_raw ) = @_; | ||
| 209 | |||
| 210 | my $fh = IO::Scalar->new( \$html_raw ); | ||
| 211 | |||
| 212 | my $html; | ||
| 213 | |||
| 214 | # sample: | ||
| 215 | # <div class="markdown-heading"><h1 class="heading-element">Table of Contents</h1><a id="table-of-contents" class="anchor" aria-label="Permalink: Table of Contents" href="#table-of-contents"><span aria-hidden="true" class="octicon octicon-link"></span></a></div> | ||
| 216 | |||
| 217 | while ( my $line = <$fh> ) { | ||
| 218 | # remove garbage if there... | ||
| 219 | |||
| 220 | if ( $line =~ m{<h(\d+)([^>]*?)>([^<]+?)</h\d+><a(.*?)/a>(.*?)}xsm ) { | ||
| 221 | $line = "<a$4$5/a><h$1$2>$3</h$1>\n"; | ||
| 222 | } | ||
| 223 | |||
| 224 | $html .= _fix_header($line); | ||
| 225 | } | ||
| 226 | |||
| 227 | close $fh; | ||
| 228 | |||
| 229 | $self->set_html($html); | ||
| 230 | |||
| 231 | return $self; | ||
| 232 | } | ||
| 233 | |||
| 234 | ######################################################################## | ||
| 235 | sub _fix_header { | ||
| 236 | ######################################################################## | ||
| 237 | my ($line) = @_; | ||
| 238 | |||
| 239 | return $line if $line !~ /^\s*<h(\d+)>(.*?)<\/h\d>$/xsm; | ||
| 240 | |||
| 241 | my ( $hn, $anchor, $header ) = ( $1, $2, $2 ); ## no critic (ProhibitCaptureWithoutTest) | ||
| 242 | |||
| 243 | $anchor = lc $anchor; | ||
| 244 | $anchor =~ s/\s+/-/gxsm; # spaces become '-' | ||
| 245 | $anchor =~ s/[.:\?_.\@'(),\`\*]//xsmg; # known weird characters, but expect more | ||
| 246 | $anchor =~ s/\///xsmg; | ||
| 247 | |||
| 248 | $line | ||
| 249 | = sprintf qq{<a id="%s" class="anchor" href="#%s"></a><h%s>%s</h%d>\n}, | ||
| 250 | $anchor, $anchor, $hn, $header, $hn; | ||
| 251 | |||
| 252 | return $line; | ||
| 253 | } | ||
| 254 | |||
| 255 | ######################################################################## | ||
| 256 | sub render_markdown { | ||
| 257 | ######################################################################## | ||
| 258 | my ($self) = @_; | ||
| 259 | |||
| 260 | if ( $self->get_engine eq 'github' ) { | ||
| 261 | return $self->_render_with_github; | ||
| 262 | } | ||
| 263 | elsif ( $self->get_engine eq 'text_markdown' ) { | ||
| 264 | return $self->_render_with_text_markdown; | ||
| 265 | } | ||
| 266 | else { | ||
| 267 | croak 'invalid engine: ' . $self->get_engine; | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | ######################################################################## | ||
| 272 | sub _render_with_github { | ||
| 273 | ######################################################################## | ||
| 274 | my ($self) = @_; | ||
| 275 | |||
| 276 | my $markdown = $self->get_markdown; | ||
| 277 | |||
| 278 | my $mode = $self->get_mode; | ||
| 279 | |||
| 280 | if ( none { $mode eq $_ } qw(gfm markdown) ) { | ||
| 281 | $mode = 'markdown'; | ||
| 282 | } | ||
| 283 | |||
| 284 | my $ua = HTTP::Tiny->new; | ||
| 285 | |||
| 286 | my $rsp = $ua->post( | ||
| 287 | $GITHUB_API, | ||
| 288 | { content => to_json( { text => $markdown, mode => $mode } ), | ||
| 289 | headers => { 'Content-Type' => 'application/json' }, | ||
| 290 | } | ||
| 291 | ); | ||
| 292 | |||
| 293 | croak 'could not render using GitHub API: ' . $rsp->{status} . ' ' . $rsp->{reason} | ||
| 294 | if !$rsp->{success}; | ||
| 295 | |||
| 296 | if ( $self->get_raw ) { | ||
| 297 | $self->set_html( $rsp->{content} ); | ||
| 298 | return $self; | ||
| 299 | } | ||
| 300 | |||
| 301 | if ( $self->get_mode eq 'gfm' ) { | ||
| 302 | return $self->fix_anchors( $rsp->{content} ); | ||
| 303 | } | ||
| 304 | else { | ||
| 305 | $self->fix_anchors( $rsp->{content} ); | ||
| 306 | return $self->fix_github_html( $self->get_html ); | ||
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 | ######################################################################## | ||
| 311 | sub fix_github_html { | ||
| 312 | ######################################################################## | ||
| 313 | my ( $self, $html_raw ) = @_; | ||
| 314 | |||
| 315 | my $fh = IO::Scalar->new( \$html_raw ); | ||
| 316 | |||
| 317 | my $html = $EMPTY; | ||
| 318 | |||
| 319 | while ( my $line = <$fh> ) { | ||
| 320 | chomp $line; | ||
| 321 | |||
| 322 | $line =~ s/(href|id)=\"\#?user-content-/$1=\"/xsm; | ||
| 323 | $line =~ s/(href|id)=\"\#?\%60.*\%60/$1=\"#$2/xsm; | ||
| 324 | |||
| 325 | $html .= "$line\n"; | ||
| 326 | } | ||
| 327 | |||
| 328 | close $fh; | ||
| 329 | |||
| 330 | $self->set_html($html); | ||
| 331 | |||
| 332 | return $self; | ||
| 333 | } | ||
| 334 | |||
| 335 | ######################################################################## | ||
| 336 | sub format_date { | ||
| 337 | ######################################################################## | ||
| 338 | my ( $self, $template ) = @_; | ||
| 339 | |||
| 340 | require Date::Format; | ||
| 341 | |||
| 342 | $template =~ s/[(]\"?(.*?)\"?[)]/$1/xsm; | ||
| 343 | |||
| 344 | my $val = eval { Date::Format::time2str( $template, time ); }; | ||
| 345 | |||
| 346 | return $EVAL_ERROR ? '<undef>' : $val; | ||
| 347 | } | ||
| 348 | |||
| 349 | ######################################################################## | ||
| 350 | sub create_toc { | ||
| 351 | ######################################################################## | ||
| 352 | my ($self) = @_; | ||
| 353 | |||
| 354 | my $markdown = $self->get_markdown; | ||
| 355 | |||
| 356 | my $fh = IO::Scalar->new( \$markdown ); | ||
| 357 | |||
| 358 | my $toc = $self->get_no_title ? $EMPTY : "# \@TOC_TITLE\@\n\n"; | ||
| 359 | |||
| 360 | my $in_code_block = $FALSE; | ||
| 361 | |||
| 362 | while (<$fh>) { | ||
| 363 | chomp; | ||
| 364 | |||
| 365 | if (/^\s*```/xsm) { | ||
| 366 | $in_code_block = $in_code_block ? $FALSE : $TRUE; | ||
| 367 | next; | ||
| 368 | } | ||
| 369 | |||
| 370 | next if $in_code_block; | ||
| 371 | |||
| 372 | /^(\#+)\s+(.*?)$/xsm && do { | ||
| 373 | my $level = $1; | ||
| 374 | |||
| 375 | my $indent = $SPACE x ( 2 * ( length($level) - 1 ) ); | ||
| 376 | |||
| 377 | my $topic = $2; | ||
| 378 | |||
| 379 | my $link = $topic; | ||
| 380 | |||
| 381 | $link =~ s/^\s*(.*)\s*$/$1/xsm; | ||
| 382 | |||
| 383 | $link =~ s/\s+/-/gxsm; # spaces become '-' | ||
| 384 | |||
| 385 | $link =~ s/[\?\_:.\@'(),\`\*]//xsmg; # known weird characters, but expect more | ||
| 386 | |||
| 387 | $link =~ s/\///xsmg; | ||
| 388 | |||
| 389 | $link = lc $link; | ||
| 390 | |||
| 391 | # remove HTML entities | ||
| 392 | $link =~ s/&\#\d+;//xsmg; | ||
| 393 | |||
| 394 | # remove escaped entities and remaining noisy characters | ||
| 395 | $link =~ s/[{}&]//xsmg; | ||
| 396 | |||
| 397 | $toc .= sprintf "%s* [%s](#%s)\n", $indent, $topic, $link; | ||
| 398 | }; | ||
| 399 | } | ||
| 400 | |||
| 401 | close $fh; | ||
| 402 | |||
| 403 | return $toc; | ||
| 404 | } | ||
| 405 | |||
| 406 | ######################################################################## | ||
| 407 | sub print_html { | ||
| 408 | ######################################################################## | ||
| 409 | my ( $self, %options ) = @_; | ||
| 410 | |||
| 411 | my $fh = $options{fh} // *STDOUT; | ||
| 412 | |||
| 413 | if ( !$options{body} ) { | ||
| 414 | print {$fh} $self->get_html; | ||
| 415 | |||
| 416 | return; | ||
| 417 | } | ||
| 418 | |||
| 419 | my $css = exists $options{css} ? $options{css} : $self->get_css; | ||
| 420 | my $title = exists $options{title} ? $options{title} : $self->get_infile; | ||
| 421 | |||
| 422 | my $title_section = $title ? "<title>$title</title>" : $EMPTY; | ||
| 423 | |||
| 424 | my $css_section | ||
| 425 | = $css | ||
| 426 | ? qq{<link href="$css" rel="stylesheet" type="text/css" />} | ||
| 427 | : $EMPTY; | ||
| 428 | |||
| 429 | my @head = grep { $_ ? $_ : () } ( $title_section, $css_section ); | ||
| 430 | |||
| 431 | my $head_section = q{}; | ||
| 432 | |||
| 433 | if (@head) { | ||
| 434 | unshift @head, '<head>'; | ||
| 435 | push @head, '</head>'; | ||
| 436 | |||
| 437 | $head_section = join "\n", @head; | ||
| 438 | } | ||
| 439 | |||
| 440 | my $body = $self->get_html; | ||
| 441 | |||
| 442 | print {$fh} <<"END_OF_TEXT"; | ||
| 443 | <html> | ||
| 444 | $head_section | ||
| 445 | <body> | ||
| 446 | $body | ||
| 447 | </body> | ||
| 448 | </html> | ||
| 449 | END_OF_TEXT | ||
| 450 | |||
| 451 | return; | ||
| 452 | } | ||
| 453 | |||
| 454 | ######################################################################## | ||
| 455 | sub main { | ||
| 456 | ######################################################################## | ||
| 457 | |||
| 458 | my $md = Markdown::Render->new( | ||
| 459 | infile => shift @ARGV, | ||
| 460 | css => $DEFAULT_CSS | ||
| 461 | ); | ||
| 462 | |||
| 463 | $md->finalize_markdown->render_markdown; | ||
| 464 | |||
| 465 | $md->print_html; | ||
| 466 | |||
| 467 | exit 0; | ||
| 468 | } | ||
| 469 | |||
| 470 | 1; | ||
| 471 | |||
| 472 | ## no critic (RequirePodSections) | ||
| 473 | |||
| 474 | __END__ |