| 1 | package App::Dapper::Markdown; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | App::Dapper::Markdown - Convert Markdown to HTML, with fenced code blocks. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Dapper uses L<Text::MultiMarkdown> for Markdown. On top of it, this module | ||||||||||||||||||||||||||||
| 10 | adds fenced code blocks, which MultiMarkdown does not understand: | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | ```perl | ||||||||||||||||||||||||||||
| 13 | print "Hello, world!\n"; | ||||||||||||||||||||||||||||
| 14 | ``` | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | A fenced block (with three or more backticks or tildes) becomes | ||||||||||||||||||||||||||||
| 17 | C<< <pre><code class="language-perl"> >>, with its contents HTML-escaped and | ||||||||||||||||||||||||||||
| 18 | left untouched by Markdown. The C<language-*> class is what client-side | ||||||||||||||||||||||||||||
| 19 | highlighters such as Prism and highlight.js look for. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 32 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 32 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 32 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 32 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 32 | ▶ | use Text::MultiMarkdown (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head2 to_html | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Takes Markdown text and returns HTML. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub to_html { | ||||||||||||||||||||||||||
|
Subroutine
to_html: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 0 | my ($text) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | return '' unless defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | my @blocks; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Replace each fenced code block with a placeholder paragraph, so that | ||||||||||||||||||||||||||||
| 44 | # Markdown leaves its contents alone, and put the HTML back afterwards | ||||||||||||||||||||||||||||
| 45 | 0 | $text =~ s{ | |||||||||||||||||||||||||||
| 46 | ^([ ]{0,3})(`{3,}|~{3,})[ \t]*([\w+#.-]*)[^\n]*\n # opening fence and language | ||||||||||||||||||||||||||||
| 47 | (.*?) # code | ||||||||||||||||||||||||||||
| 48 | ^[ ]{0,3}\2[`~]*[ \t]*$ # closing fence | ||||||||||||||||||||||||||||
| 49 | }{ | ||||||||||||||||||||||||||||
| 50 | 0 | push @blocks, code_block($4, $3); | |||||||||||||||||||||||||||
| 51 | 0 | "\n\nDAPPERCODEBLOCK" . $#blocks . "\n\n"; | |||||||||||||||||||||||||||
| 52 | }gemsx; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | my $html = Text::MultiMarkdown::markdown($text); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | $html =~ s{<p>DAPPERCODEBLOCK(\d+)</p>}{$blocks[$1]}g; | |||||||||||||||||||||||||||
| 57 | 0 | $html =~ s{DAPPERCODEBLOCK(\d+)}{$blocks[$1]}g; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | return $html; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head2 code_block | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Takes the code and (optionally) its language, and returns the HTML for a code | ||||||||||||||||||||||||||||
| 65 | block. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub code_block { | ||||||||||||||||||||||||||
|
Subroutine
code_block: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 0 | my ($code, $language) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | $code = '' unless defined $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $code =~ s/&/&/g; | |||||||||||||||||||||||||||
| 74 | 0 | $code =~ s/</</g; | |||||||||||||||||||||||||||
| 75 | 0 | $code =~ s/>/>/g; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my $class = defined $language && length $language ? qq{ class="language-$language"} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $language and length $language
MC/DC0%0/2
defined $language and length $language
defined $language
length $language
Truth table0%0/3
A and B
A= defined $languageB= length $language
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | return "<pre><code$class>$code</code></pre>"; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 32 | 1; | |||||||||||||||||||||||||||