.htmlgen/FunctionalPerl/Htmlgen/Mediawiki.pm

filter: Statementstmt 92.8% 116 / 125 Branchbran 84.6% 22 / 26 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 96.4% 27 / 28 Podpod 0.0% 0 / 8 totaltotal 85.7% 168 / 196 CC 16 SCAR 17.1
CC
16
Cov
90%
mediawiki_expand
14.4
possibly_unescape_brackets
2.7
uri_unescape
2.0
CRAP 5.5
« lib/FP/Lazy.pm ↑ Summary lib/FP/Div.pm »
1 #
2 # Copyright (c) 2014-2023 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::Mediawiki
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 Expand `[[ ]]` in markdown source text into standard markdown format.
19  
20 =head1 NOTE
21  
22 This is alpha software! Read the status section in the package README
23 or on the L<website|http://functional-perl.org/>.
24  
25 =cut
26  
27 package FunctionalPerl::Htmlgen::Mediawiki;
28 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use experimental "signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 3 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 3 our @EXPORT = qw();
36 3 our @EXPORT_OK = qw(mediawiki_prepare mediawiki_replace mediawiki_rexpand
37 mediawiki_expand);
38 3 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
39  
40 3 ▶use FP::Docstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use Chj::chompspace;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use Chj::TEST ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use PXML::XHTML ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 # and for text display we need to *decode* URIs..
48 # COPY from chj-bin's `urldecode`, now modified by having our own
49 # uri_escape
50 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # Adapted copy from URI::Escape
53 5 ▶sub uri_unescape($str) {
Subroutine uri_unescape: called CC=2 SCAR=6.9
Pod undocumented
54  
55 # Note from RFC1630: "Sequences which start with a percent sign
56 # but are not followed by two hexadecimal characters are reserved
57 # for future extension"
58 5 ▶ $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg if defined $str;
Branch: 1 branch
BranchTrueFalseTotal
if defined $str 2 3 5
59 5 $str;
60 }
61  
62 5 ▶sub url_decode {
Subroutine url_decode: called CC=1 SCAR=0.0
Pod undocumented
63 5 my ($str) = @_;
64 5 my $str2 = uri_unescape($str);
65 5 decode("utf-8", $str2, Encode::FB_CROAK)
66 }
67  
68 # Hack to avoid failing tests because of changed URI behaviour. The
69 # new URI behaviour is probably the correct one but don't want to fail
70 # with older versions.
71 6 ▶sub possibly_unescape_brackets {
Subroutine possibly_unescape_brackets: called CC=2 SCAR=10.1
Pod undocumented
72 6 my ($str) = @_;
73  
74 # warn "URI $URI::VERSION";
75 $URI::VERSION < 5.11
76 6 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
$URI::VERSION < 5.11 ? : 0 6 6
77 0 $str =~ s{\%5B}{\[}sg;
78 0 $str =~ s{\%5D}{\]}sg;
79 0 $str
80 }
81 : $str
82 }
83  
84 # escape [ ] for markdown; XX is this correct?
85 0 ▶sub squarebracked_escape($str) {
Subroutine squarebracked_escape: not called CC=1 SCAR=6.9
Pod undocumented
86 0 $str =~ s%([\[\]])%\\$1%sg;
87 0 $str
88 }
89  
90 # meant to preprocess the whole markdown document; keep the returned
91 # table and the used token for mediawiki_expand.
92  
93 2 ▶sub mediawiki_prepare ($str, $token) {
Subroutine mediawiki_prepare: called CC=1 SCAR=0.0
Pod undocumented
94 2 __ '($str, $tokenstr) -> ($str, $hashtable) '
95 . '-- replace "[[...]]" syntax in $str with replacement tokens '
96 . 'which use $tokenstr as prefix';
97 2 my $table = {};
98 2 my $n = 0;
99 2 $str =~ s%(^|.)\[\[(.*?[^\\])\]\]%
100 2 my ($pre,$cont) = ($1,$2);
101 2 $n++;
102 2 $$table{$n} = $cont;
103 2 $pre.$token."-".$n."-" # must be inert to markdown parser
104 %sge;
105 2 ($str, $table)
106 }
107  
108 1 ▶TEST { [mediawiki_prepare 'foo [[bar]] [[baz]].', 'LO'] }
Subroutine __ANON__: called CC=1 SCAR=0.0
109 3 ['foo LO-1- LO-2-.', { '1' => 'bar', '2' => 'baz' }];
110  
111 # possibly should build PXML directly from here instead of string
112 # replace, but I'm lazy right now.
113  
114 18 ▶sub mediawiki_replace ($str, $token, $table) {
Subroutine mediawiki_replace: called CC=1 SCAR=0.2
Pod undocumented
115 18 __ '($str, $token, {tokenargument => $value,..}) -> $str '
116 . '-- re-insert hidden parts';
117 18 $str =~ s%\Q$token\E-(\d+)-%
118 0 '[['.$$table{$1}.']]'
119 %sge;
120 18 $str
121 }
122  
123 # here's the version that returns PXML, through the indirection of a
124 # string, which XXX may very well be unsafe.
125  
126 18 ▶sub mediawiki_rexpand ($str, $token, $table) {
Subroutine mediawiki_rexpand: called CC=1 SCAR=0.0
Pod undocumented
127 18 __ '($str, $token, $table) -> [PXML::Element] '
128 . '-- mediawiki_replace then _expand';
129 18 mediawiki_expand(mediawiki_replace($str, $token, $table))
130 }
131  
132 25 ▶sub mediawiki_expand($str) {
Subroutine mediawiki_expand: called CC=14 SCAR=26.7
Pod undocumented
133 25 __ '($text_segment_str) -> [string|PXML::Element] '
134 . '-- expand "[[...]]" in $text_segment_str to link elements';
135 25 my $res = [];
136 25 my $lastpos = 0;
137 25 ▶ while ($str =~ m%(^|.)\[\[(.*?[^\\])\]\]%sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /(^|.)\[\[(.*?[^\\])\]\]/gs) 7 25 32
138 7 ▶ next if $1 eq '\\';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "\\" 1 6 7
139 6 my $cont = $2;
140 6 my $pos = pos $str;
141  
142 6 my $matchlen = 2 + length($cont) + 2;
143 6 my $prelen = $pos - $matchlen - $lastpos;
144 6 ▶ push @$res, substr $str, $lastpos, $prelen if $prelen > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $prelen > 0 5 1 6
145 6 $lastpos = $pos;
146  
147 6 $cont =~ s|(?<=[^\\])\\(.)|$1|sg; # remove quoting
148 6 my @parts = map { chompspace $_ } split /(?<=[^\\])\|/, $cont;
149 6 ▶ if (@parts == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@parts == 1) { } 5 1 6
elsif (@parts == 2) { } 1 0 1
150 5 my ($docname_and_perhaps_fragment) = @parts;
151 5 my $uri = URI->new($docname_and_perhaps_fragment);
152  
153 5 my $fragment = url_decode $uri->fragment;
154 5 my $fragmenttext = do {
155 5 ▶ if (length $fragment) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $fragment) { } 2 3 5
156 2 my @f = split /,/, $fragment;
157 2 my $f = shift @f;
158 2 ▶ while (@f and length $f < 20 and length $f[0] < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if (@f and length $f < 20 and length $f[0] < 20) 0 2 2
Condition50%3/6
@f and length $f < 20 and length $f[0] < 20
!ll&&!rl&&r
200
@f and length $f < 20
!ll&&!rl&&r
110
MC/DC0%0/3
@f and length $f < 20 and length $f[0] < 20
@f length $f < 20 length $f[0] < 20
Truth table50%2/4
A and B and C
A= @fB= length $f < 20C= length $f[0] < 20
ABCresult
0XX0
10X0
1100
1111
159 0 $f .= "," . shift @f;
160 }
161 2 ▶ $f .= ".." if @f;
Branch: 1 branch
BranchTrueFalseTotal
if @f 1 1 2
162 2 ▶ if (length $f > 40) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $f > 40) 0 2 2
163 0 $f = substr($f, 0, 28) . ".."
164 }
165  
166 # convert underscores back to spaces (XX
167 # well.. that's lossy!)
168 2 $f =~ s/_/ /sg;
169 2 " ($f)";
170 } else {
171 3 ""
172 }
173 };
174  
175 # (Get title of document at path? But may be too long,
176 # probably not a good idea.)
177  
178 # XX use 'opaque' instead of 'path' for the url? for
179 # locations with protocol or so? Or croak about those? Use
180 # opaque for the text, though, ok?
181 5 my $text = $uri->opaque;
182 5 $text =~ tr/_/ /;
183 push @$res, A(
184 {
185 5 href => "//" . $uri->path . ".md" . do {
186 5 my $f = $uri->fragment;
187 5 ▶ length $f ? "#" . $f : ""
Branch: 1 branch
BranchTrueFalseTotal
length $f ? : 2 3 5
188 }
189 },
190 $text . $fragmenttext
191 );
192 } elsif (@parts == 2) {
193 1 my ($loc, $text) = @parts;
194 1 push @$res, A({ href => $loc }, $text)
195 } else {
196  
197 # XX location?...
198 0 die "more than 2 parts in a wiki style link: " . show($cont);
199 }
200 }
201  
202 25 my $postlen = length($str) - $lastpos;
203 25 ▶ push @$res, substr $str, $lastpos, $postlen if $postlen > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $postlen > 0 23 2 25
204  
205 25 $res
206 }
207  
208 1 ▶TEST { mediawiki_expand "<foo>[[bar]] baz</foo>" }
Subroutine __ANON__: called CC=1 SCAR=0.0
209 3 ['<foo>', A({ href => "//bar.md" }, "bar"), ' baz</foo>'];
210  
211 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
212 1 mediawiki_expand
213 ' [[howto#References (and "mutation"), "variables" versus "bindings"]] '
214 }
215 [
216 3 ' ',
217 A(
218 {
219 href =>
220 '//howto.md#References%20(and%20%22mutation%22),%20%22variables%22%20versus%20%22bindings%22'
221 },
222 'howto (References (and "mutation")..)'
223 ),
224 ' '
225 ];
226  
227 1 ▶TEST { mediawiki_expand ' [[Foo#yah_Hey\\[1\\]]] ' }
Subroutine __ANON__: called CC=1 SCAR=0.0
228 [
229 3 ' ',
230 A(
231 { href => possibly_unescape_brackets '//Foo.md#yah_Hey%5B1%5D' },
232 'Foo (yah Hey[1])'
233 ),
234 ' '
235 ];
236  
237 1 ▶TEST { mediawiki_expand ' [[Foo#(yah_Hey)\\[1\\]|Some \\[text\\]]] ' }
Subroutine __ANON__: called CC=1 SCAR=0.0
238 [
239 3 ' ',
240 A({ href => possibly_unescape_brackets 'Foo#(yah_Hey)[1]' }, 'Some [text]'),
241 ' '
242 ]; # note: no // and .md added to Foo!
243  
244 1 ▶TEST { mediawiki_expand 'foo [[bar]]' }
Subroutine __ANON__: called CC=1 SCAR=0.0
245 3 ['foo ', A { href => "//bar.md" }, "bar"];
246 1 ▶TEST { mediawiki_expand '[[bar]]' }
Subroutine __ANON__: called CC=1 SCAR=0.0
247 3 [A { href => "//bar.md" }, "bar"];
248 1 ▶TEST { mediawiki_expand ' \[[bar]]' }
Subroutine __ANON__: called CC=1 SCAR=0.0
249 3 [' \[[bar]]'];
250  
251 3 1
« lib/FP/Lazy.pm ↑ Summary lib/FP/Div.pm »