.htmlgen/FunctionalPerl/Htmlgen/MarkdownPlus.pm

filter: Statementstmt 100% 69 / 69 Branchbran 75.0% 3 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 2 totaltotal 95.0% 95 / 100 CC 4 SCAR 2.9
CC
4
Cov
97%
pxml_body_split_h1
3.0
markdownplus_parse
2.0
BEGIN
1.0
CRAP 1.3
« examples/dbi ↑ Summary examples/fibs »
1 #
2 # Copyright (c) 2014-2019 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FunctionalPerl::Htmlgen::MarkdownPlus
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FunctionalPerl::Htmlgen::MarkdownPlus qw(markdownplus_parse);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 use PXML::XHTML qw(BODY);
18  
19 my $mediawikitoken = rand;
20 # passed to mediawiki_prepare from FunctionalPerl::Htmlgen::Mediawiki
21 my ($h1,$body1) = markdownplus_parse(
22 "# Hi\n\nHello [World](http://world).\n", # markdownplusstr
23 "Hi too", # alternative_title
24 $mediawikitoken);
25 is $h1->string, '<h1>Hi</h1>';
26 is BODY($body1)->string,
27 '<body><p>Hello <a href="http://world">World</a>.</p></body>';
28  
29  
30 =head1 DESCRIPTION
31  
32 C<markdownplus_parse> is a wrapper around L<Text::Markdown> that
33 parses the latter's html string to L<PXML::XHTML> elements
34 (C<PXML::_::XHTML> objects which are L<PXML::Element> objects). It
35 replaces mediawiki style links with tokens as a hack to make it
36 possible to implement those links without having to modify
37 L<Text::Markdown>.
38  
39 (It also contains a hack to make C<<with_toc>...</with_toc>> tags
40 possible, but the actual implementation of the toc is independent.)
41  
42 =head1 SEE ALSO
43  
44 L<FunctionalPerl::Htmlgen::PXMLMapper> -- a protocol to further
45 process the parsed L<PXML>.
46  
47 =head1 NOTE
48  
49 This is alpha software! Read the status section in the package README
50 or on the L<website|http://functional-perl.org/>.
51  
52 =cut
53  
54 package FunctionalPerl::Htmlgen::MarkdownPlus;
55 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 3 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 3 ▶use experimental "signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 3 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 3 our @EXPORT = qw();
63 3 our @EXPORT_OK = qw(markdownplus_parse);
64 3 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
65  
66 3 ▶use FP::Docstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 3 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 3 ▶use PXML qw(is_pxml_element);
Subroutine BEGIN: called CC=1 SCAR=0.0
69 3 ▶use PXML::XHTML ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
70 3 ▶use FP::Stream ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
71 3 ▶use FunctionalPerl::Htmlgen::Htmlparse 'htmlparse';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 3 ▶use Text::Markdown 'markdown';
Subroutine BEGIN: called CC=1 SCAR=0.0
73 3 ▶use FP::Lazy;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 3 ▶use FunctionalPerl::Htmlgen::Mediawiki qw(mediawiki_prepare);
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 # Return <h1> element if available, and rest.
77 5 ▶sub pxml_body_split_h1($body) {
Subroutine pxml_body_split_h1: called CC=3 SCAR=11.0
Pod undocumented
78 5 my $b = stream_mixed_flatten $body;
79 5 my ($v, $rest) = $b->first_and_rest;
80 5 ▶ if (is_pxml_element $v and $v->name eq "h1") { ($v, $rest) }
Branch: 1 branch
BranchTrueFalseTotal
if (is_pxml_element($v) and $v->name eq 'h1') { } 3 1 4
Condition66%2/3
is_pxml_element($v) and $v->name eq "h1"
!ll&&!rl&&r
103
MC/DC50%1/2
is_pxml_element($v) and $v->name eq "h1"
is_pxml_element($v) $v->name eq "h1"
Truth table66%2/3
A and B
A= is_pxml_element($v)B= $v->name eq "h1"
ABresult
0X0
100
111
81 1 else { (undef, $body) }
82 }
83  
84 1 ▶TEST { [pxml_body_split_h1 ["foo"]] }
Subroutine __ANON__: called CC=1 SCAR=0.0
85 3 [undef, ['foo']];
86  
87 1 ▶TEST { [pxml_body_split_h1 [H1("x"), "foo"]]->[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
88 3 H1("x");
89  
90 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 1 my ($h1, $rest) = pxml_body_split_h1 [H1("x", "y"), "foo", B("bar")];
92 1 [$h1->string, BODY($rest)->string]
93 }
94 3 ['<h1>xy</h1>', '<body>foo<b>bar</b></body>'];
95  
96 1 ▶sub markdownplus_parse ($str, $alternative_title, $mediawikitoken) {
Subroutine markdownplus_parse: called CC=2 SCAR=7.0
Pod undocumented
97 1 __
98 '($str, $alternative_title, $tokenstr)-> ($h1, [$body_PXML_Elements], $hashtbl) '
99 . '-- markdown parsing to PXML, extracting title, and replacing'
100 . ' mediawiki syntax with $tokenstr based replacements'
101 . ' (side channelling hack)';
102  
103 1 my ($str1, $table) = mediawiki_prepare($str, $mediawikitoken);
104  
105 1 my $htmlstr = markdown($str1);
106  
107 # XX hack: fix '<p><with_toc></p> .. <p></with_toc></p>' before
108 # parsing, to avoid losing the with_toc element. Bah.
109 1 $htmlstr =~ s|<p>\s*(</?with_toc[^<>]*>)\s*</p>|$1|sg;
110  
111 # `markdown` returns a series of <p> elements etc., not wrapped in
112 # any element. Need to wrap it before parsing or it will drop the
113 # outmost element if it's (e.g.?) <with_toc>.
114 1 my $bodyelement = htmlparse('<body>' . $htmlstr . '</body>', "body");
115  
116 1 my $body = $bodyelement->body;
117 1 my ($maybe_h1, $rest) = pxml_body_split_h1($body);
118 (
119 (
120 1 ▶ defined $maybe_h1
Branch: 1 branch
BranchTrueFalseTotal
defined $maybe_h1 ? : 1 0 1
121 ? ($maybe_h1, $rest)
122 : (H1(force($alternative_title)), $body)
123 ),
124 $table
125 )
126 }
127  
128 3 1
« examples/dbi ↑ Summary examples/fibs »