blib/lib/App/Greple/md.pm

filter: Statementstmt 93.4% 454 / 486 Branchbran 86.5% 161 / 186 Conditioncond 71.7% 61 / 85 MC/DCmcdc 53.1% 25 / 47 Subroutinesub 97.1% 67 / 69 Podpod 0.0% 0 / 35 totaltotal 84.5% 768 / 908 CC 142 SCAR 20.5
CC
142
Cov
89%
default_theme
30.0
format_table
19.0
setup_colors
18.1
CRAP 7.8
« blib/lib/App/Greple/md/Blocks.pm ↑ Summary blib/lib/App/Greple/md/widen.pm »
1 # -*- mode: perl; coding: utf-8 -*-
2 # vim: set fileencoding=utf-8 filetype=perl :
3 package App::Greple::md;
4  
5 184 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 184 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 184 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 184 our $VERSION = "1.23";
10  
11 =encoding utf-8
12  
13 =head1 NAME
14  
15 App::Greple::md - Greple module for Markdown syntax highlighting
16  
17 =head1 SYNOPSIS
18  
19 greple -Mmd file.md
20  
21 greple -Mmd --mode=dark -- file.md
22  
23 greple -Mmd --base-color=Crimson -- file.md
24  
25 greple -Mmd --cm h1=RD -- file.md
26  
27 greple -Mmd --no-table -- file.md
28  
29 greple -Mmd --fold -- file.md
30  
31 greple -Mmd -- --fold file.md
32  
33 greple -Mmd --refill -- file.md
34  
35 =head1 DESCRIPTION
36  
37 B<App::Greple::md> is a L<greple|App::Greple> module for viewing
38 Markdown files in the terminal with syntax highlighting.
39  
40 It colorizes headings, bold, italic, strikethrough, inline code,
41 fenced code blocks, front matter, HTML comments, blockquotes, horizontal rules,
42 links, and images. Tables are formatted with aligned columns and
43 optional Unicode box-drawing borders. Long lines in list items can
44 be folded with proper indentation. Links become clickable via OSC 8
45 terminal hyperlinks in supported terminals.
46  
47 Nested elements are handled with cumulative coloring: for example,
48 a link inside a heading retains both its link color and the heading
49 background color.
50  
51 For a complete Markdown viewing experience with line folding,
52 multi-column output, and themes, see L<App::mdee>, which uses this
53 module as its highlighting engine.
54  
55 Control characters and escape sequences in the file, which could change
56 the title or the clipboard or clear the screen, are removed before
57 anything else, and so are not shown. Colors stay.
58  
59 =head1 COMMAND OPTIONS
60  
61 The following options are defined as greple command options
62 (specified after C<-->).
63  
64 =head2 B<--fold>
65  
66 Enable text folding for list items, definition lists, block quotes
67 and the paragraphs joined by C<--refill>. Long lines are wrapped
68 with proper indentation using L<ansifold(1)|App::ansifold> via
69 L<App::Greple::tee>; the continuation lines of a block quote start with
70 the quote marker again.
71 Code blocks, front matter, HTML comments, and tables are
72 excluded from folding. The fold width is controlled by the
73 C<fold_width> config parameter (default: 80), and the run-in and
74 run-out widths for line breaking (kinsoku) by C<fold_runin> and
75 C<fold_runout> (default: 4, as in ansifold).
76  
77 greple -Mmd -- --fold file.md
78 greple -Mmd::config(fold_width=60) -- --fold file.md
79  
80 Supported list markers: C<*>, C<+>, C<->, C<1.>, C<1)>, C<#.>, C<#)>.
81  
82 The module option C<--fold> (before C<-->) enables the same folding
83 through the config.
84  
85 =head1 MODULE OPTIONS
86  
87 Module options are specified before C<--> to separate them from
88 greple's own options:
89  
90 greple -Mmd --mode=dark --cm h1=RD -- file.md
91  
92 =head2 B<-m> I<MODE>, B<--mode>=I<MODE>
93  
94 Set color mode. Available modes are C<light> (default) and C<dark>.
95  
96 greple -Mmd -m dark -- file.md
97  
98 =head2 B<-B> I<COLOR>, B<--base-color>=I<COLOR>
99  
100 Override the base color used for headings, bold, links, and other
101 elements. Accepts a named color (e.g., C<Crimson>, C<DarkCyan>) or a
102 L<Term::ANSIColor::Concise> color spec.
103  
104 greple -Mmd -B Crimson -- file.md
105  
106 =head2 B<--[no-]colorize>
107  
108 Enable or disable syntax highlighting. Enabled by default.
109 When disabled, no color is applied to Markdown elements.
110  
111 greple -Mmd --no-colorize -- file.md
112  
113 =head2 B<--[no-]fold>
114  
115 Enable or disable text folding (config C<fold>). Disabled by default.
116 When enabled, long lines in list items, definition lists, block
117 quotes and the paragraphs joined by C<--refill> are wrapped with
118 proper indentation.
119 The fold width is controlled by the C<fold_width> config parameter
120 (default: 80).
121  
122 greple -Mmd --fold -- file.md
123 greple -Mmd::config(fold=1,fold_width=60) file.md
124  
125 The greple command option of the same name, C<--fold> after C<-->,
126 folds the same lines once without setting the config.
127  
128 =head2 B<--[no-]refill>
129  
130 Join the lines of paragraphs into one line each (see L</Paragraph
131 Refill>). Disabled by default.
132  
133 =head2 B<--[no-]table>
134  
135 Enable or disable table formatting. When enabled (default),
136 Markdown tables (3 or more consecutive pipe-delimited rows) are
137 formatted with aligned columns using L<App::ansicolumn>. A C<< <br> >>
138 (or C<< <br/> >>, C<< <br /> >>) in a cell breaks the cell into lines
139 after it, and is colored with C<br_mark> (see L</Line Breaks in
140 Tables>); one in a code span, or escaped as C<< \<br> >>, does not. This needs an L<App::ansicolumn> which takes
141 a LINE SEPARATOR (U+2028) as a line break in a cell.
142  
143 greple -Mmd --no-table -- file.md
144  
145 =head2 B<--[no-]rule>
146  
147 Enable or disable Unicode box-drawing characters for table borders.
148 When enabled (default), ASCII pipe characters (C<|>) are replaced
149 with vertical lines (C<E<0x2502>>), and separator row dashes become
150 horizontal rules (C<E<0x2500>>) with corner pieces (C<E<0x251C>>,
151 C<E<0x2524>>, C<E<0x253C>>).
152  
153 greple -Mmd --no-rule -- file.md
154  
155 =head2 B<--colormap> I<LABEL>=I<SPEC>, B<--cm> I<LABEL>=I<SPEC>
156  
157 Override the color for a specific element. I<LABEL> is one of
158 the color labels listed in L</COLOR LABELS>. I<SPEC> follows
159 L<Term::ANSIColor::Concise> format and supports C<sub{...}>
160 function specs via L<Getopt::EX::Colormap>.
161  
162 greple -Mmd --cm h1=RD -- file.md
163 greple -Mmd --cm bold='${base}D' -- file.md
164  
165 B<greple> has an option of the same name. Before C<-->, C<--cm> belongs
166 to this module and takes the labels above. After C<-->, or when there
167 is no C<-->, it is the option of B<greple>, which colors the matched
168 text and its own labels such as C<FILE>, and complains about the labels
169 of this module:
170  
171 greple -Mmd --cm h1=RD -- --cm B -E foo file.md # h1 is red, foo blue
172 greple -Mmd --cm h1=RD file.md # h1: Unknown label
173  
174 =head2 B<--begin> I<FUNCTION>
175  
176 Call I<FUNCTION> on the whole text of each file, in C<$_>, before this
177 module does anything to it, so that the tables and the folded lines
178 follow the change. I<FUNCTION> is a C<sub{...}> or the name of a
179 function, as C<&func(key=value)> or C<&Package::func(key=value)>. A
180 name without a package is looked for in C<main>, and then in the module
181 C<App::Greple::md::func>, named after the function. The package is
182 loaded if the function is not defined yet. A function that is not found
183 is reported with a warning and skipped. The option can be repeated.
184  
185 greple -Mmd --begin 'sub{ s/\(C\)/\x{A9}/g }' -- file.md
186  
187 Unlike the C<--begin> of B<greple> after C<-->, which runs after this
188 module, this one runs before it. In a configuration file or a theme of
189 L<mdee>, it is given as C<pass_md+=(--begin '...')>.
190  
191 L<App::Greple::md::widen> has such a function, C<&widen>.
192  
193 =head2 B<--heading-markup>[=I<STEPS>], B<--hm>[=I<STEPS>]
194  
195 Control inline markup processing inside headings. By default,
196 headings are rendered with uniform heading color without processing
197 bold, italic, strikethrough, or inline code inside them. Links
198 are always processed as OSC 8 hyperlinks regardless of this option.
199  
200 Without an argument, all inline formatting becomes visible within
201 headings using cumulative coloring. With an argument, only the
202 specified steps are processed inside headings. Steps are separated
203 by colons.
204  
205 Available steps: C<inline_code>, C<horizontal_rules>, C<bold>,
206 C<italic>, C<strike>.
207  
208 greple -Mmd --hm -- file.md # all markup
209 greple -Mmd --hm=bold -- file.md # bold only
210 greple -Mmd --hm=bold:italic -- file.md # bold and italic
211  
212 =head2 B<--hashed> I<LEVEL>=I<VALUE>
213  
214 Append closing hashes to headings. For example, C<### Title>
215 becomes C<### Title ###>. Set per heading level:
216  
217 greple -Mmd --hashed h3=1 --hashed h4=1 -- file.md
218  
219 =head2 B<--show> I<LABEL>[=I<VALUE>]
220  
221 Control which elements are highlighted. This is useful for
222 focusing on specific elements or disabling unwanted highlighting.
223  
224 greple -Mmd --show bold=0 -- file.md # disable bold
225 greple -Mmd --show all= --show h1 -- file.md # only h1
226  
227 C<--show LABEL=0> or C<--show LABEL=> disables the label.
228 C<--show LABEL> or C<--show LABEL=1> enables it.
229 C<all> is a special key that sets all labels at once.
230  
231 Controllable labels: C<bold>, C<italic>, C<strike>, C<code_inline>,
232 C<header> (h1-h6), C<horizontal_rule>, C<blockquote>.
233  
234 The following elements are always processed and cannot be disabled:
235 C<comment>, C<code_block> (C<code_mark>, C<code_info>),
236 C<link>, C<image>, C<image_link>.
237 Use C<--cm LABEL=> to remove their color without disabling processing.
238  
239 =head1 CONFIGURATION
240  
241 Module parameters can also be set using the C<config()> function
242 in the C<-M> declaration:
243  
244 greple -Mmd::config(mode=dark,base_color=Crimson) file.md
245  
246 Nested hash parameters use dot notation:
247  
248 greple -Mmd::config(hashed.h3=1,hashed.h4=1) file.md
249  
250 Available parameters:
251  
252 mode light or dark (default: light)
253 base_color base color override
254 colorize syntax highlighting (default: 1)
255 fold text folding (default: 0)
256 fold_width fold width in columns (default: 80)
257 fold_runin run-in width in folded lines (default: 4)
258 fold_runout run-out width in folded lines (default: 4)
259 refill join the lines of paragraphs (default: 0)
260 refill_parser cmark (the CommonMark module), mdperl (Markdown::Perl)
261 or pandoc (default: the first that is there, in
262 that order)
263 front_matter show TOML and YAML front matter as a code block
264 and leave it alone with refill (default: 1)
265 table table formatting (default: 1)
266 table_trim trim cell whitespace (default: 1)
267 table_width table width in columns; cells of over-wide
268 columns are wrapped (default: 0 = off)
269 table_cell per-column widths passed to ansicolumn --cell,
270 colon separated (e.g. 0:10:<20)
271 table_wrap_mark string put at the start of continuation lines
272 of wrapped cells (default: none)
273 table_cell_mark string put at the start of the first line of
274 every body cell in a column that has a wrapped
275 cell (default: none)
276 table_runin run-in width in wrapped cells (default: 2)
277 table_runout run-out width in wrapped cells (default: 2)
278 table_boundary fold boundary in wrapped cells (default: word)
279 table_linebreak line break mode in wrapped cells: none, runin,
280 runout or all (default: all)
281 intact parts sent to ansicolumn --intact to be kept as
282 they are, colon separated; only "table" for now:
283 the lines of a formatted table are joined into
284 one (default: none)
285 table_style lattice: column separators (default; pipe is
286 another name); 3line: columns separated by spaces,
287 a rule above and below the table and per column
288 below the header; simple: columns separated by
289 spaces, a rule per column below the header and
290 below the table
291 rule box-drawing characters (default: 1)
292 osc8 OSC 8 hyperlinks (default: 1)
293 heading_markup inline markup in headings (default: 0)
294 0=off, 1/all=all, or colon-separated steps
295 cjk_friendly markdown-cjk-friendly rules for * and **
296 emphasis (default: 0)
297 tick_open inline code open marker (default: `)
298 tick_close inline code close marker (default: ´)
299 nofork nofork+raw mode for code ref calls (default: 1)
300 highlight syntax highlight of fenced code: 1, the name
301 of a highlighter, or 0 (default: 1)
302 hashed.h1-h6 closing hashes per level (default: 0)
303  
304 =head2 Paragraph Refill
305  
306 With C<refill=1>, the lines of every paragraph, block quote, list
307 item and definition are joined into one line before the text is
308 colorized. Which lines belong together is decided by a Markdown
309 parser (L<App::Greple::md::Blocks>): the L<CommonMark> module, an
310 interface to libcmark, when it is installed, otherwise
311 L<Markdown::Perl>, which is pure Perl and always installed with this
312 module, or L<pandoc|https://pandoc.org> (2.11.3 or later) run with the
313 C<sourcepos> extension; C<refill_parser> chooses one. The module
314 joins the lines of the original text and changes nothing else. Hard
315 line breaks are kept, and so is the break before a list marker, a
316 horizontal rule, a table row or a definition (C<: >). When folding
317 is enabled, the joined lines are folded too.
318  
319 Front matter (see L</Front Matter>) is left as it is.
320  
321 greple -Mmd::config(refill=1) file.md
322 greple -Mmd::config(refill=1,refill_parser=pandoc) file.md
323  
324 =head2 Front Matter
325  
326 Front matter at the top of the file is shown as a code block: the
327 body in C<meta_block> and the delimiter lines in C<meta_mark>, with
328 no other markup, and it is not folded or joined.
329 TOML front matter is between C<+++> lines, and YAML front matter goes
330 from a C<---> line to a C<---> or C<...> line, as pandoc takes it (the
331 opening line is not followed by a blank line).
332  
333 Front matter is not part of CommonMark. pandoc and L<Markdown::Perl>
334 take YAML front matter as metadata, while libcmark reads it as a rule,
335 paragraphs and list items. C<front_matter=0> takes the lines as
336 Markdown, as libcmark does.
337  
338 =head2 Syntax Highlight of Code
339  
340 The code in a fenced code block is shown in the colors of its
341 language, the first word after the opening fence, inside the
342 C<code_block> color. The colors are those of a highlighter of
343 L<Text::ANSI::Highlight>: C<highlight=1> is its default, C<pandoc>,
344 and C<highlight=sourcehighlight> (C<gnu>) or C<highlight=skylighting>
345 (C<sky>) is the one of the name. All the blocks of a file are given
346 to the highlighter at once, and each is colored by itself.
347  
348 C<highlight=1> is C<pandoc> in its own style in the light mode, and in
349 the style C<breezedark> in the dark mode. A style of the highlighter
350 can be named after a colon (C<highlight=pandoc:haddock>), and a
351 highlighter which is named is as it is named in both modes. The
352 background colors of a style are not used: the background is that of
353 the code block.
354  
355 The language is also the first class in braces, as pandoc takes it
356 (C<```{.perl .numberLines}>).
357  
358 A block which has parts is colored part by part, when
359 L<Text::ANSI::Highlight> has a module for it. A block of
360 L<pandoc-embedz|https://github.com/tecolicom/pandoc-embedz>
361 (C<```embedz>, C<```{.embedz data=file.csv}>) has a configuration in
362 YAML, a template of Jinja2 and data: they are colored by their
363 languages, in the backgrounds of C<code_config>, C<code_block> and
364 C<code_data>, and the lines of C<---> between them have no color.
365 The lines of a part labelled I<NAME> by the module are in the color
366 C<code_I<NAME>>, and in C<code_block> when it is not defined.
367  
368 A block is shown as it is when it has no language, when the
369 highlighter does not know the language, and when the module, the
370 highlighter or its command (C<pandoc>, C<source-highlight>,
371 C<skylighting>) is not installed. C<highlight=0> turns it off.
372  
373 =head2 OSC 8 Hyperlinks
374  
375 Links are converted to clickable OSC 8 terminal hyperlinks in
376 supported terminals (iTerm2, Kitty, WezTerm, Ghostty, etc.).
377 Disable with:
378  
379 greple -Mmd::config(osc8=0) file.md
380  
381 =head2 Settings of the User
382  
383 The module runs B<ansifold> and B<ansicolumn> for folding and tables.
384 They are run as functions in the greple process, or as separate
385 processes with C<nofork=0>, and either way they do not read the rc
386 files of the user, F<~/.ansifoldrc> and F<~/.ansicolumnrc>: the
387 module puts their names in the environment variable
388 C<GETOPTEX_NO_USER_SETTINGS> (see L<Getopt::EX::Long>), so that the
389 output does not change by the settings made for them on the command
390 line.
391  
392 =head1 COLOR LABELS
393  
394 The following labels identify colorizable elements. Use them
395 with C<--colormap> (C<--cm>) to customize colors or C<--show> to control
396 visibility. Default values are shown as C<light / dark>.
397 Colors follow L<Term::ANSIColor::Concise> format.
398  
399 =head2 Headings
400  
401 LABEL LIGHT DARK
402 h1 L25D/${base};E L00D/${base};E
403 h2 L25D/${base}+y20;E L00D/${base}-y15;E
404 h3 L25DN/${base}+y20 L00DN/${base}-y15
405 h4 ${base}UD ${base}UD
406 h5 ${base}UD ${base}UD
407 h6 ${base}D ${base}D
408  
409 =head2 Inline Formatting
410  
411 LABEL LIGHT DARK
412 bold D
413 italic I
414 strike X
415 emphasis_mark L18 L10
416 bold_mark - -
417 italic_mark - -
418 strike_mark - -
419  
420 Emphasis markers (C<**>, C<*>, C<__>, C<_>, C<~~>) are colored with
421 C<emphasis_mark>, separately from the content text. C<bold_mark>,
422 C<italic_mark>, C<strike_mark> are undefined by default and fall back
423 to C<emphasis_mark>. Define them via C<--cm> to override per type:
424  
425 greple -Mmd --cm emphasis_mark=R -- file.md # all markers red
426 greple -Mmd --cm bold_mark=G -- file.md # bold markers green
427  
428 C<*> and C<**> start and end emphasis as in CommonMark: a delimiter
429 next to a letter on one side and punctuation on the other does not
430 count, so C<L*=50, a*=68> is not italic. In Japanese text this also
431 rejects C<日本語の*「強調」*です> and C<日本語の**強調。**です>, as the
432 commonmark and gfm readers of pandoc do. With C<cjk_friendly=1>,
433 the rules of
434 L<markdown-cjk-friendly|https://github.com/tats-u/markdown-cjk-friendly>
435 are used instead, and these are emphasis.
436  
437 =head2 Code
438  
439 LABEL LIGHT DARK
440 code_mark L20 L10
441 code_tick L15/L23 L15/L05
442 code_info ${base_name}=y70 L10
443 code_block /L24;E /L03;E
444 code_config /#ECE9F7;E /#1C1B24;E
445 code_data /#E3F1EA;E /#181E1A;E
446 code_inline L00/L23 L25/L05
447 meta_mark L15/L10E L10/L15E
448 meta_block L25/L12E L00/L13E
449  
450 Inline code backticks are displayed as C<`contentE<0xb4>> using
451 C<code_tick> color. Multi-backtick delimiters are collapsed to a
452 single pair with optional surrounding spaces stripped (per CommonMark).
453 The open/close markers can be customized via C<tick_open>/C<tick_close>
454 config parameters.
455  
456 C<code_config> and C<code_data> are the backgrounds of the
457 configuration and the data in a block of pandoc-embedz (see
458 L</Syntax Highlight of Code>).
459  
460 C<meta_mark> and C<meta_block> color the delimiter lines and the
461 body of front matter (see L</Front Matter>), the delimiter lines in
462 the background of the file name and the body a little apart from it.
463 When they are set to empty, C<meta_block> falls back to C<code_block>,
464 and the delimiter lines are in C<code_mark> inside the color of the
465 body.
466  
467 =head2 Block Elements
468  
469 LABEL LIGHT / DARK
470 blockquote ${base}
471 horizontal_rule L15
472 comment ${base}+r60
473  
474 =head2 Links
475  
476 LABEL LIGHT / DARK
477 link I
478 image I
479 image_link I
480 link_mark - -
481  
482 C<link_mark> colors the brackets (C<[>, C<]>) around link and image
483 text. Undefined by default, falls back to C<emphasis_mark>.
484 The C<!> prefix for images uses the C<image> or C<image_link>
485 color. Hide brackets with C<--cm 'link_mark=sub{""}'> (used by
486 the C<nomark> theme).
487  
488 =head2 Line Breaks in Tables
489  
490 LABEL LIGHT / DARK
491 br_mark - -
492  
493 C<br_mark> colors a C<< <br> >> which breaks a table cell into lines
494 (see B<--[no-]table>). Undefined by default, falls back to
495 C<emphasis_mark>. Hide it with C<--cm 'br_mark=sub{""}'> (used by
496 the C<nomark> theme): the line break is left, and the tag is not
497 shown.
498  
499 =head1 SEE ALSO
500  
501 =over 4
502  
503 =item L<App::mdee>
504  
505 Markdown viewer command with line folding, table formatting,
506 multi-column layout, and themes. Uses this module for syntax
507 highlighting.
508  
509 =item L<App::Greple>
510  
511 General-purpose extensible grep tool that hosts this module.
512  
513 =item L<Term::ANSIColor::Concise>
514  
515 Concise ANSI color specification format used for color labels.
516  
517 =item L<App::ansicolumn>
518  
519 ANSI-aware column formatting used for table alignment.
520  
521 =item L<App::ansifold>
522  
523 ANSI-aware text folding used for line wrapping in list items.
524  
525 =item L<App::Greple::md::widen>
526  
527 A function for B<--begin>, for characters that terminals draw over two
528 columns.
529  
530 =item L<App::Greple::md::Blocks>
531  
532 Finds the paragraphs for C<refill=1> with L<CommonMark>,
533 L<Markdown::Perl> or L<pandoc|https://pandoc.org>.
534  
535 =item L<CommonMark Spec 0.31.2, Emphasis and strong emphasis|https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis>
536  
537 The rules that C<*> and C<**> follow by default.
538  
539 =item L<CommonMark CJK-friendly Amendments|https://github.com/tats-u/markdown-cjk-friendly/blob/main/specification.md>
540  
541 The rules that C<cjk_friendly=1> follows. The proposal comes from
542 L<commonmark-spec issue #650, Emphasis with CJK punctuation|https://github.com/commonmark/commonmark-spec/issues/650>,
543 open since 2020.
544  
545 =back
546  
547 =head1 AUTHOR
548  
549 Kazumasa Utashiro
550  
551 =head1 LICENSE
552  
553 Copyright © 2025-2026 Kazumasa Utashiro.
554  
555 This library is free software; you can redistribute it and/or modify
556 it under the same terms as Perl itself.
557  
558 =cut
559  
560 184 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
561 184 ▶use Text::ANSI::Fold qw($csi_re $osc_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
562 184 ▶use Text::ANSI::Fold::Util qw(ansi_width);
Subroutine BEGIN: called CC=1 SCAR=0.0
563 184 ▶use Getopt::EX::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
564 184 ▶use Getopt::EX::Colormap;
Subroutine BEGIN: called CC=1 SCAR=0.0
565 184 ▶use Getopt::EX::Func qw(parse_func);
Subroutine BEGIN: called CC=1 SCAR=0.0
566 184 ▶use App::Greple::Common qw(FILELABEL);
Subroutine BEGIN: called CC=1 SCAR=0.0
567 184 ▶use App::Greple::Regions qw(match_regions);
Subroutine BEGIN: called CC=1 SCAR=0.0
568 184 ▶use App::Greple::md::Blocks qw(blocks);
Subroutine BEGIN: called CC=1 SCAR=0.0
569  
570 184 my @color_labels = qw(
571 code_mark code_tick code_info code_block code_inline
572 code_config code_template code_data
573 meta_mark meta_block
574 comment link image image_link
575 h1 h2 h3 h4 h5 h6
576 bold italic strike emphasis_mark
577 bold_mark italic_mark strike_mark link_mark br_mark
578 blockquote horizontal_rule
579 );
580  
581 my $config = Getopt::EX::Config->new(
582 mode => '', # light / dark
583 osc8 => 1, # OSC 8 hyperlinks
584 base_color => '', # override base color
585 colorize => 1, # syntax highlighting
586 fold => 0, # text folding
587 refill => 0, # join the lines of paragraphs
588 refill_parser => '', # cmark (CommonMark module), mdperl (Markdown::Perl) or pandoc; '' = whichever is there
589 front_matter => 1, # front matter (+++ TOML, --- YAML) at the top is a code block
590 fold_width => 80, # fold width
591 fold_runin => 4, # run-in width in folded lines (ansifold default)
592 fold_runout => 4, # run-out width in folded lines (ansifold default)
593 table => 1, # table formatting
594 table_trim => 1, # trim cell content whitespace
595 table_width => 0, # table width; wrap over-wide cells (0 = off)
596 table_cell => '', # per-column widths for ansicolumn --cell (colon separated)
597 table_wrap_mark => '', # mark for continuation lines of wrapped cells
598 table_cell_mark => '', # mark for the first line of cells in wrapped columns
599 table_runin => 2, # run-in width in wrapped cells
600 table_runout => 2, # run-out width in wrapped cells
601 table_boundary => 'word', # fold boundary in wrapped cells
602 table_linebreak => 'all', # line break (kinsoku) mode in wrapped cells
603 intact => '', # parts kept intact by ansicolumn --intact (table)
604 rule => 1, # box-drawing characters for tables
605 table_style => 'lattice', # lattice (or pipe): column separators; 3line, simple: spaces and rules only
606 nofork => 1, # use nofork+raw for code ref calls
607 highlight => 1, # syntax highlight of fenced code: 1, the name of a highlighter, or 0
608 heading_markup => 0, # inline formatting in headings
609 cjk_friendly => 0, # markdown-cjk-friendly rules for * and ** emphasis
610 tick_open => '`', # inline code open marker
611 tick_close => "\x{b4}", # inline code close marker (´)
612 hashed => { h1 => 0, h2 => 0, h3 => 0, h4 => 0, h5 => 0, h6 => 0 },
613 184 (map { $_ => undef } @color_labels), # color labels (undef = not set)
614 );
615  
616 #
617 # Color definitions
618 #
619  
620 184 my %base_color = (
621 light => '<RoyalBlue>=y25',
622 dark => '<RoyalBlue>=y80',
623 );
624  
625 184 my %default_colors = (
626 code_mark => 'L20',
627 code_tick => 'L15/L23',
628 code_info => '${base_name}=y70',
629 code_block => '/L24;E',
630 code_config => '/#ECE9F7;E',
631 code_data => '/#E3F1EA;E',
632 code_inline => 'L00/L23',
633 meta_mark => 'L15/L10E',
634 meta_block => 'L25/L12E',
635 comment => '${base}+r60',
636 link => 'I',
637 image => 'I',
638 image_link => 'I',
639 h1 => 'L25D/${base};E',
640 h2 => 'L25D/${base}+y20;E',
641 h3 => 'L25DN/${base}+y20',
642 h4 => '${base}UD',
643 h5 => '${base}UD',
644 h6 => '${base}D',
645 bold => 'D',
646 italic => 'I',
647 strike => 'X',
648 emphasis_mark => 'L18',
649 blockquote => '${base}',
650 horizontal_rule => 'L15',
651 );
652  
653 184 my %dark_overrides = (
654 code_mark => 'L10',
655 code_tick => 'L15/L05',
656 code_info => 'L10',
657 code_block => '/L03;E',
658 code_config => '/#1C1B24;E',
659 code_data => '/#181E1A;E',
660 code_inline => 'L25/L05',
661 meta_mark => 'L10/L15E',
662 meta_block => 'L00/L13E',
663 h1 => 'L00D/${base};E',
664 h2 => 'L00D/${base}-y15;E',
665 h3 => 'L00DN/${base}-y15',
666 h4 => '${base}UD',
667 h5 => '${base}UD',
668 h6 => '${base}D',
669 emphasis_mark => 'L10',
670 );
671  
672 0 ▶sub default_theme {
Subroutine default_theme: not called CC=5 SCAR=34.0
Pod undocumented
673 0 ▶ my $mode = shift // 'light';
Condition0%0/2
shift() // "light"
l!l
00
MC/DC0%0/1
shift() // "light"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
674 0 my %colors = %default_colors;
675 0 ▶ if ($mode eq 'dark') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "dark") 0 0 0
676 0 @colors{keys %dark_overrides} = values %dark_overrides;
677 }
678 0 $colors{base} = $base_color{$mode};
679 0 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 0 0 0
680 0 %colors;
681 } else {
682 # Print as bash array assignments: theme_MODE[key]='value'
683 0 for my $key (sort keys %colors) {
684 0 (my $val = $colors{$key}) =~ s/'/'\\''/g;
685 0 printf "theme_%s[%s]='%s'\n", $mode, $key, $val;
686 }
687 }
688 }
689  
690 184 my $cm;
691 my @opt_cm;
692 184 my @opt_begin;
693 184 my %show;
694  
695 177 ▶sub finalize {
Subroutine finalize: called CC=5 SCAR=16.7
Pod undocumented
696 177 my($mod, $argv) = @_;
697 177 no_user_settings(qw(ansicolumn ansifold));
698 177 $config->deal_with($argv,
699 "mode|m=s", "base_color|B=s",
700 "colorize!", "nofork!", "fold!", "refill!", "refill_parser=s", "front_matter!", "fold_width=i", "fold_runin=i", "fold_runout=i",
701 "table!", "table_trim!",
702 "table_width=i", "table_cell=s", "table_wrap_mark=s", "table_cell_mark=s", "rule!", "table_style=s",
703 "table_runin=i", "table_runout=i", "table_boundary=s", "table_linebreak=s", "intact=s",
704 "heading_markup|hm:s", "cjk_friendly!", "tick_open=s", "tick_close=s",
705 "hashed=s%",
706 "colormap|cm=s" => \@opt_cm,
707 "begin=s" => \@opt_begin,
708 "show=s%" => \%show);
709 # --hm with no argument gives "": treat as "all"
710 177 my $hm = $config->{heading_markup};
711 177 ▶ if (defined $hm && $hm eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hm and $hm eq "") 0 177 177
Condition33%1/3
defined $hm and $hm eq ""
!ll&&!rl&&r
01770
MC/DC0%0/2
defined $hm and $hm eq ""
defined $hm $hm eq ""
Truth table33%1/3
A and B
A= defined $hmB= $hm eq ""
ABresult
0X0
100
111
712 0 $config->{heading_markup} = 'all';
713 }
714 177 ▶ if (my $w = $config->{fold_width}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = $config->{"fold_width"}) 177 0 177
715 177 my $fold = join ' ', '--fold-by', $w, @{$config}{qw(fold_runin fold_runout)};
716 177 $mod->setopt('--fold', $fold);
717 177 ▶ if ($config->{fold}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"fold"}) 65 112 177
718 65 my @default = $mod->default;
719 65 $mod->setopt('default', @default, $fold);
720 }
721 }
722 }
723  
724 # ansicolumn and ansifold are called as functions in this process
725 # (Command::Run), or as separate processes by the tee module in fork
726 # mode. Either way they must not read the rc files of the user, so
727 # their names go into GETOPTEX_NO_USER_SETTINGS (Getopt::EX >= 3.05),
728 # which Getopt::EX::Long reads when the program first runs. Names
729 # already there are kept, and 1 or * (all programs) is left as it is.
730 183 ▶sub no_user_settings {
Subroutine no_user_settings: called CC=4 SCAR=13.9
Pod undocumented
731 183 my @names = @_;
732 183 ▶ my $env = $ENV{GETOPTEX_NO_USER_SETTINGS} // '';
Condition100%2/2
$ENV{"GETOPTEX_NO_USER_SETTINGS"} // ""
l!l
5178
MC/DC100%1/1
$ENV{"GETOPTEX_NO_USER_SETTINGS"} // ""
$ENV{"GETOPTEX_NO_USER_SETTINGS"}
Truth table100%2/2
A
A= $ENV{"GETOPTEX_NO_USER_SETTINGS"}
Aresult
11
00
733 183 ▶ return if $env eq '1' or $env eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $env eq 1 or $env eq "*" 2 181 183
Condition100%3/3
$env eq 1 or $env eq "*"
l!l&&r!l&&!r
11181
MC/DC100%2/2
$env eq 1 or $env eq "*"
$env eq 1 $env eq "*"
Truth table100%3/3
A or B
A= $env eq 1B= $env eq "*"
ABresult
1X1
011
000
734 181 my %have = map { $_ => 1 } split /[\s,]+/, $env;
735 $ENV{GETOPTEX_NO_USER_SETTINGS} =
736 181 join ' ', grep { length } $env, grep { !$have{$_}++ } @names;
737 }
738  
739 99 ▶sub setup_colors {
Subroutine setup_colors: called CC=13 SCAR=29.0
Pod undocumented
740 99 ▶ my $mode = $config->{mode} || 'light';
Condition100%2/2
$config->{'mode'} || 'light'
l!l
5346
MC/DC100%1/1
$config->{'mode'} || 'light'
$config->{'mode'}
Truth table100%2/2
A
A= $config->{'mode'}
Aresult
11
00
741 99 my %colors = %default_colors;
742 99 ▶ if ($mode eq 'dark') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "dark") 5 94 99
743 5 @colors{keys %dark_overrides} = values %dark_overrides;
744 }
745 # Determine base color
746 99 my $base = $config->{base_color};
747 99 ▶ if ($base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base) { } 0 99 99
748 # Color names get automatic luminance adjustment
749 0 ▶ $base = "<$base>" . ($mode eq 'dark' ? '=y80' : '=y25')
Branch: 2 branches
BranchTrueFalseTotal
if $base =~ /^[A-Za-z]\w*$/u 0 0 0
$mode eq 'dark' ? : 0 0 0
750 if $base =~ /^[A-Za-z]\w*$/;
751 } else {
752 99 ▶ $base = $base_color{$mode} || $base_color{light};
Condition33%1/3
$base_color{$mode} || $base_color{'light'}
l!l&&r!l&&!r
9900
MC/DC0%0/2
$base_color{$mode} || $base_color{'light'}
$base_color{$mode} $base_color{'light'}
Truth table33%1/3
A || B
A= $base_color{$mode}B= $base_color{'light'}
ABresult
1X1
011
000
753 }
754 # Override with config params (e.g., config(h1=RD))
755 99 for my $key (@color_labels) {
756 3069 my $val = $config->{$key};
757 3069 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 11 3058 3069
758 11 $colors{$key} = $val;
759 }
760 }
761 # ${base_name}: color without luminance (e.g., '<RoyalBlue>')
762 99 (my $base_name = $base) =~ s/=y\d+$//;
763 # Expand placeholders
764 99 for my $key (keys %colors) {
765 2475 $colors{$key} =~ s/\$\{base_name\}/$base_name/g;
766 2475 $colors{$key} =~ s/\$\{base\}/$base/g;
767 }
768 # Handle + prefix: prepend current color value before load_params
769 # (load_params' built-in + doesn't work correctly with sub{...})
770 99 my @final_cm;
771 99 for my $entry (@opt_cm) {
772 264 my $expanded = $entry =~ s/\$\{base_name\}/$base_name/gr
773 =~ s/\$\{base\}/$base/gr;
774 264 ▶ if ($expanded =~ /^(\w+)=\+(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expanded =~ /^(\w+)=\+(.*)/u) { } 0 264 264
775 0 my ($label, $append) = ($1, $2);
776 0 ▶ my $current = $colors{$label} // '';
Condition0%0/2
$colors{$label} // ""
l!l
00
MC/DC0%0/1
$colors{$label} // ""
$colors{$label}
Truth table0%0/2
A
A= $colors{$label}
Aresult
11
00
777 0 push @final_cm, "$label=$current$append";
778 } else {
779 264 push @final_cm, $expanded;
780 }
781 }
782  
783 99 $cm = Getopt::EX::Colormap->new(
784 HASH => \%colors,
785 NEWLABEL => 1,
786 );
787 99 $cm->load_params(@final_cm);
788 }
789  
790 1374 ▶sub active {
Subroutine active: called CC=4 SCAR=13.9
Pod undocumented
791 1374 my $label = shift;
792 1374 ▶ return 0 if exists $show{$label} && !$show{$label};
Branch: 1 branch
BranchTrueFalseTotal
if exists $show{$label} and not $show{$label} 17 1357 1374
Condition100%3/3
exists $show{$label} and not $show{$label}
!ll&&!rl&&r
1355217
MC/DC100%2/2
exists $show{$label} and not $show{$label}
exists $show{$label} not $show{$label}
Truth table100%3/3
A and not(B)
A= exists $show{$label}B= not $show{$label}
ABresult
0X0
100
111
793 1357 ▶ return 1 unless exists $cm->{HASH}{$label};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cm->{"HASH"}{$label} 97 1260 1357
794 1260 $cm->{HASH}{$label} ne '';
795 }
796  
797 #
798 # Apply color by label
799 #
800  
801 7369 ▶sub md_color {
Subroutine md_color: called CC=1 SCAR=0.0
Pod undocumented
802 7369 my($label, $text) = @_;
803 7369 $cm->color($label, $text);
804 }
805  
806 23 ▶sub defined_color {
Subroutine defined_color: called CC=2 SCAR=6.9
Pod undocumented
807 23 my $label = shift;
808 23 ▶ exists $cm->{HASH}{$label} && $cm->{HASH}{$label} ne '';
Branch: 1 branch
BranchTrueFalseTotal
exists $cm->{"HASH"}{$label} and $cm->{"HASH"}{$label} ne "" 21 2 23
809 }
810  
811 # The color of $label, or of $fallback when $label is not defined
812 17 ▶sub fallback_color {
Subroutine fallback_color: called CC=2 SCAR=6.9
Pod undocumented
813 17 my($label, $fallback, $text) = @_;
814 17 ▶ $label = $fallback unless defined_color($label);
Branch: 1 branch
BranchTrueFalseTotal
unless defined_color($label) 5 12 17
815 17 md_color($label, $text);
816 }
817  
818 1766 ▶sub mark_color {
Subroutine mark_color: called CC=3 SCAR=11.0
Pod undocumented
819 1766 my($type, $text) = @_;
820 1766 my $label = "${type}_mark";
821 $label = 'emphasis_mark' unless exists $cm->{HASH}{$label}
822 1766 ▶ && $cm->{HASH}{$label} ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cm->{"HASH"}{$label} and $cm->{"HASH"}{$label} ne "" 1394 372 1766
Condition66%2/3
exists $cm->{"HASH"}{$label} and $cm->{"HASH"}{$label} ne ""
!ll&&!rl&&r
13940372
MC/DC50%1/2
exists $cm->{"HASH"}{$label} and $cm->{"HASH"}{$label} ne ""
exists $cm->{"HASH"}{$label} $cm->{"HASH"}{$label} ne ""
Truth table66%2/3
A and B
A= exists $cm->{"HASH"}{$label}B= $cm->{"HASH"}{$label} ne ""
ABresult
0X0
100
111
823 1766 md_color($label, $text);
824 }
825  
826 #
827 # Protection mechanism
828 #
829 # SGR 256 placeholders protect processed regions (inline code,
830 # comments, links) from being matched by later patterns.
831 #
832  
833 184 my @protected;
834 184 my($PS, $PE) = ("\e[256m", "\e[m"); # protect start/end markers
835 184 my $PR = qr/\e\[256m(\d+)\e\[m/; # protect restore pattern
836 184 my($OS, $OE) = ("\e]8;;", "\e\\"); # OSC 8 start/end markers
837  
838 2206 ▶sub protect {
Subroutine protect: called CC=1 SCAR=0.0
Pod undocumented
839 2206 my $text = shift;
840 2206 push @protected, $text;
841 2206 $PS . $#protected . $PE;
842 }
843  
844 958 ▶sub restore {
Subroutine restore: called CC=3 SCAR=11.0
Pod undocumented
845 958 my $s = shift;
846 958 ▶ 1 while $s =~ s{$PR}{$protected[$1] // die "restore failed: index $1"}ge;
Branch: 1 branch
BranchTrueFalseTotal
$s =~ s[$PR][$protected[$1] // die("restore failed: index $1");]egu and do { '???' } 172 958 1130
Condition50%1/2
$protected[$1] // die("restore failed: index $1")
l!l
22060
MC/DC0%0/1
$protected[$1] // die("restore failed: index $1")
$protected[$1]
Truth table50%1/2
A
A= $protected[$1]
Aresult
11
00
847 958 $s;
848 }
849  
850 #
851 # OSC 8 hyperlink generation
852 #
853  
854 340 ▶sub osc8 {
Subroutine osc8: called CC=2 SCAR=7.0
Pod undocumented
855 340 ▶ return $_[1] unless $config->{osc8};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"osc8"} 0 340 340
856 340 my($url, $text) = @_;
857 340 my $escaped = uri_escape_utf8($url, "^\\x20-\\x7e");
858 340 "${OS}${escaped}${OE}${text}${OS}${OE}";
859 }
860  
861 #
862 # Link text inner pattern: backtick spans, backslash escapes, normal chars
863 #
864  
865 184 my $LT = qr/(?:`[^`\n]*+`|\\.|[^`\\\n\]]++)+/;
866  
867 # Code span pattern (both single and multi-backtick).
868 # Captures: _bt (backtick delimiter), _content (code body).
869 # Used directly in inline_code step and as basis for $SKIP_CODE.
870 184 my $CODE = qr{(?x)
871 (?<_bt> `++ ) # opening backtick(s)
872 (?<_content>
873 (?: (?! \g{_bt} ) . )+? # content (not containing same-length backticks)
874 )
875 \g{_bt} # closing backtick(s) matching opener
876 };
877  
878 # Skip code spans in link/image patterns.
879 # Used as the first alternative in s{$SKIP_CODE|<link pattern>}{...}ge
880 # so that code spans are matched and skipped, preventing link/image
881 # patterns from matching inside them.
882 184 my $SKIP_CODE = qr{$CODE (*SKIP)(*FAIL)}x;
883  
884 # Delimiters of * and ** emphasis (CommonMark 0.31.2, 6.2): an opener
885 # must be left-flanking and a closer right-flanking, so that
886 # "L*=50, a*=68" is not italic. Punctuation is Unicode punctuation and
887 # symbols. An opener after $tight (neither a space nor punctuation)
888 # cannot have punctuation after it, and a closer before $tight cannot
889 # have punctuation before it.
890 #
891 # With cjk_friendly=1 the amendments of markdown-cjk-friendly apply:
892 # CJK punctuation does not count, and a CJK character is as good as a
893 # space on the other side, so that "日本語の*「強調」*です" is italic.
894 # See CLAUDE.md.
895 184 ▶my $EMOJI = eval q{ qr/\p{Emoji_Presentation}/ } // qr/(?!)/; # not in old perl
Condition50%1/2
eval " qr/\p{Emoji_Presentation}/ " // qr/(?!)/u
l!l
1840
MC/DC0%0/1
eval " qr/\p{Emoji_Presentation}/ " // qr/(?!)/u
eval " qr/\p{Emoji_Presentation}/ "
Truth table50%1/2
A
A= eval " qr/\p{Emoji_Presentation}/ "
Aresult
11
00
896 184 my $CJK = qr/(?:(?!$EMOJI)[\p{EA=W}\p{EA=F}\p{EA=H}]|\p{Hangul})/;
897  
898 193 ▶sub flanking {
Subroutine flanking: called CC=2 SCAR=6.9
Pod undocumented
899 193 my $d = quotemeta shift;
900 my($punct, $tight) = $config->{cjk_friendly}
901 193 ▶ ? (qr/(?!$CJK)[\p{P}\p{S}]/, qr/(?!$CJK)[^\s\p{P}\p{S}]/)
Branch: 1 branch
BranchTrueFalseTotal
$config->{'cjk_friendly'} ? : 2 191 193
902 : (qr/[\p{P}\p{S}]/, qr/[^\s\p{P}\p{S}]/);
903 # A link or a code span is a protect() placeholder by now; its
904 # ends stand for the punctuation that delimits it in the text.
905 193 my($ps, $pe) = (qr/\e\[256m/, qr/\e\[m/);
906 193 (qr/(?:(?<!$tight)|(?<=$pe)|(?!$d(?:$punct|$ps)))$d(?=\S)/,
907 qr/(?<=\S)(?:(?<!$punct)(?<!$pe)|(?!$d(?!$ps)$tight))$d/);
908 }
909  
910 #
911 # colorize() - the main function
912 #
913 # Receives entire file content in $_ (--begin with -G --filter).
914 # Processes all patterns with multiline regexes.
915 #
916  
917 #
918 # Pipeline step class
919 #
920  
921 184 package App::Greple::md::Step {
922 2576 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
923 2576 my($class, %args) = @_;
924 2576 bless \%args, $class;
925 }
926 0 ▶ sub label { $_[0]->{label} }
Subroutine label: not called CC=1 SCAR=6.9
927 1386 ▶ sub active { !$_[0]->{label} || App::Greple::md::active($_[0]->{label}) }
Subroutine active: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
not $_[0]{"label"} or App::Greple::md::active($_[0]{"label"}) 594 792 1386
928 1369 ▶ sub run { $_[0]->{code}->() }
Subroutine run: called CC=1 SCAR=0.0
929 }
930  
931 2576 ▶sub Step {
Subroutine Step: called CC=1 SCAR=0.0
Pod undocumented
932 2576 my $code = pop;
933 2576 my $label = shift;
934 2576 App::Greple::md::Step->new(label => $label, code => $code);
935 }
936  
937 #
938 # Pipeline steps: Step(sub{}) = always active, Step(label => sub{}) = controllable
939 #
940  
941 my %colorize = (
942  
943 99 ▶ front_matter => Step(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
944 99 ▶ my $front = front_matter($_) // return;
Condition100%2/2
front_matter($_) // (return)
l!l
693
MC/DC100%1/1
front_matter($_) // (return)
front_matter($_)
Truth table100%2/2
A
A= front_matter($_)
Aresult
11
00
945 6 my($open, @body) = split /(?<=\n)/, $front;
946 6 my $close = pop @body;
947 6 chomp $open;
948 6 ▶ my $block = sub { fallback_color('meta_block', 'code_block', $_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
949 # A delimiter line is in meta_mark alone when it is defined, and
950 # otherwise in code_mark inside the block color, so that the
951 # background of the block runs over it
952 my $mark = defined_color('meta_mark')
953 10 ▶ ? sub { md_color('meta_mark', $_[0]) }
Subroutine __ANON__: called CC=1 SCAR=0.0
954 6 ▶ : sub { $block->(md_color('code_mark', $_[0])) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
defined_color('meta_mark') ? : 5 1 6
955 substr($_, 0, length $front) = protect(join '',
956 $mark->($open), "\n",
957 6 (map { $block->($_) } @body),
958 $mark->($close));
959 }),
960  
961 99 ▶ code_blocks => Step(sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
962 # closing fence: the same character, at least as long (CommonMark)
963 99 my $block = qr{^( {0,3})((`|~)\3{2,})(.*)\n((?s:.*?))^( {0,3})(\2\3*)(\h*)$}m;
964 # the code of all the blocks, in the colors of their languages
965 99 my @highlight;
966 99 ▶ if ($config->{highlight}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"highlight"}) 97 2 99
967 97 my @code;
968 97 ▶ while (/$block/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$block/gu) 237 97 334
969 237 my($info, $body) = ($4, $5);
970 237 push @code, [ $info, $body ];
971 }
972 97 @highlight = highlight_code(@code);
973 }
974 s{$block}{
975 239 my($oi, $fence, $lang, $body, $ci, $close, $trail) = ($1, $2, $4, $5, $6, $7, $8);
976 239 ▶ my @label = $config->{highlight} ? code_labels($lang, $body) : ();
Branch: 1 branch
BranchTrueFalseTotal
$config->{'highlight'} ? : 237 2 239
977 239 ▶ $body = shift(@highlight) // $body;
Condition66%2/3
shift @highlight // $body
l!l&&r!l&&!r
152240
MC/DC0%0/2
shift @highlight // $body
shift @highlight $body
Truth table66%2/3
A // B
A= shift @highlightB= $body
ABresult
1X1
011
000
978 239 my $result = md_color('code_mark', "$oi$fence");
979 239 ▶ $result .= md_color('code_info', $lang) if length($lang);
Branch: 1 branch
BranchTrueFalseTotal
if length $lang 233 6 239
980 239 $result .= "\n";
981 239 ▶ if (length($body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body) 238 1 239
982 $result .= join '', map {
983 238 my $label = shift @label;
984 733 ▶ !defined $label ? md_color('code_block', $_)
Branch: 2 branches
BranchTrueFalseTotal
!defined($label) ? : 724 9 733
$label eq '' ? : 3 6 9
985 : $label eq '' ? $_
986 : fallback_color("code_$label", 'code_block', $_);
987 } split /(?<=\n)/, $body;
988 }
989 239 $result .= md_color('code_mark', "$ci$close") . $trail;
990 239 protect($result)
991 99 }mge;
992 }),
993  
994 99 ▶ comments => Step(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
995 99 s/(^<!--(?![->])(?s:.*?)-->)/protect(md_color('comment', $1))/mge;
996 }),
997  
998 99 ▶ image_links => Step(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
999 99 s{$SKIP_CODE|\[!\[(?<text>$LT)\]\((?<img>[^)\n]+)\)\]\(<?(?<url>[^>)\s\n]+)>?\)}{
1000 protect(
1001 osc8($+{img}, md_color('image_link', "!"))
1002 42 . osc8($+{url}, mark_color('link', "[") . md_color('image_link', $+{text}) . mark_color('link', "]"))
1003 )
1004 }ge;
1005 }),
1006  
1007 99 ▶ images => Step(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1008 99 s{$SKIP_CODE|!\[(?<text>$LT)\]\(<?(?<url>[^>)\s\n]+)>?\)}{
1009 42 protect(osc8($+{url}, md_color('image', "!") . mark_color('link', "[") . md_color('image', $+{text}) . mark_color('link', "]")))
1010 }ge;
1011 }),
1012  
1013 99 ▶ links => Step(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1014 99 s{$SKIP_CODE|(?<![!\e])\[(?<text>$LT)\]\(<?(?<url>[^>)\s\n]+)>?\)}{
1015 214 protect(osc8($+{url}, mark_color('link', "[") . md_color('link', $+{text}) . mark_color('link', "]")))
1016 }ge;
1017 }),
1018  
1019 97 ▶ inline_code => Step(code_inline => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1020 97 state $to = $config->{tick_open};
1021 97 state $tc = $config->{tick_close};
1022 97 s{$CODE}{
1023 554 my $content = $+{_content};
1024 # Strip optional leading/trailing space for multi-backtick (CommonMark)
1025 554 ▶ $content =~ s/^ (.+) $/$1/ if length($+{_bt}) >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $+{"_bt"} >= 2 80 474 554
1026 554 protect(md_color('code_tick', $to) . md_color('code_inline', $content) . md_color('code_tick', $tc))
1027 }ge;
1028 }),
1029  
1030 97 ▶ headings => Step(header => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
1031 97 my $hashed = $config->{hashed};
1032 97 for my $n (reverse 1..6) {
1033 582 ▶ next unless active("h$n");
Branch: 1 branch
BranchTrueFalseTotal
unless active("h$n") 0 582 582
1034 582 my $hdr = '#' x $n;
1035 # up to 3 spaces of indentation; empty headings ("#") too (CommonMark)
1036 582 s{^( {0,3}$hdr(?:\h+.*)?)$}{
1037 859 my $line = $1;
1038 $line .= " $hdr"
1039 859 ▶ if $hashed->{"h$n"} && $line !~ /\#$/;
Branch: 1 branch
BranchTrueFalseTotal
if $$hashed{"h$n"} and not $line =~ /\#$/u 130 729 859
Condition100%3/3
$$hashed{"h$n"} and not $line =~ /\#$/u
!ll&&!rl&&r
70326130
MC/DC100%2/2
$$hashed{"h$n"} and not $line =~ /\#$/u
$$hashed{"h$n"} not $line =~ /\#$/u
Truth table100%3/3
A and not(B)
A= $$hashed{"h$n"}B= not $line =~ /\#$/u
ABresult
0X0
100
111
1040 859 protect(md_color("h$n", restore($line)));
1041 }mge;
1042 }
1043 }),
1044  
1045 97 ▶ horizontal_rules => Step(horizontal_rule => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1046 97 s/^([ ]{0,3}(?:[-*_][ ]*){3,})$/protect(md_color('horizontal_rule', $1))/mge;
1047 }),
1048  
1049 97 ▶ bold_italic => Step(bold => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1050 97 s{$SKIP_CODE|(?<!\\)(?<m>\*\*\*)(?<t>.*?)(?<!\\)\g{m}}{
1051 40 protect(mark_color('bold', $+{m}) . md_color('bold', md_color('italic', $+{t})) . mark_color('bold', $+{m}))
1052 }gep;
1053 97 s{$SKIP_CODE|(?<![\\\w])(?<m>___)(?<t>.*?)(?<!\\)\g{m}(?!\w)}{
1054 40 protect(mark_color('bold', $+{m}) . md_color('bold', md_color('italic', $+{t})) . mark_color('bold', $+{m}))
1055 }gep;
1056 }),
1057  
1058 97 ▶ bold => Step(bold => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1059 97 my($open, $close) = flanking('**');
1060 # no opener inside, so that a closer pairs with the nearest opener
1061 97 s{$SKIP_CODE|(?<!\\)(?<m>$open)(?<t>(?:(?!$open).)*?)(?<!\\)$close}{
1062 mark_color('bold', $+{m}) . md_color('bold', $+{t}) . mark_color('bold', $+{m})
1063 292 }gep;
1064 97 s{$SKIP_CODE|(?<![\\\w])(?<m>__)(?<t>.*?)(?<!\\)\g{m}(?!\w)}{
1065 mark_color('bold', $+{m}) . md_color('bold', $+{t}) . mark_color('bold', $+{m})
1066 40 }gep;
1067 }),
1068  
1069 96 ▶ italic => Step(italic => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1070 96 my($open, $close) = flanking('*');
1071 96 s{$SKIP_CODE|(?<![\\\w])(?<m>_)(?<t>(?:(?!_).)+)(?<!\\)\g{m}(?!\w)}{
1072 mark_color('italic', $+{m}) . md_color('italic', $+{t}) . mark_color('italic', $+{m})
1073 78 }gep;
1074 96 s{$SKIP_CODE|(?<![\\*])(?<m>$open)(?<t>(?:(?!\*).)+)(?<!\\)$close(?!\*)}{
1075 mark_color('italic', $+{m}) . md_color('italic', $+{t}) . mark_color('italic', $+{m})
1076 49 }gep;
1077 }),
1078  
1079 97 ▶ strike => Step(strike => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1080 97 s{$SKIP_CODE|(?<!\\)(?<m>~~)(?<t>.+?)(?<!\\)\g{m}}{
1081 mark_color('strike', $+{m}) . md_color('strike', $+{t}) . mark_color('strike', $+{m})
1082 40 }gep;
1083 }),
1084  
1085 97 ▶ blockquotes => Step(blockquote => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1086 97 s/^( {0,3})((?:>\h?)+)(.*)$/$1 . md_color('blockquote', $2) . $3/mge;
1087 184 }),
1088 );
1089  
1090 #
1091 # Pipeline configuration
1092 #
1093  
1094 # Always before headings (protection + links)
1095 184 my @protect_steps = qw(front_matter code_blocks comments image_links images links);
1096  
1097 # Inline steps controlled by heading_markup
1098 184 my @inline_steps = qw(inline_code horizontal_rules bold_italic bold italic strike);
1099  
1100 # Always last
1101 184 my @final_steps = qw(blockquotes);
1102  
1103 99 ▶sub build_pipeline {
Subroutine build_pipeline: called CC=4 SCAR=25.8
Pod undocumented
1104 99 my $hm = $config->{heading_markup};
1105  
1106 # heading_markup disabled: headings before all inline steps
1107 99 ▶ if (!$hm) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hm) 99 0 99
1108 99 return (@protect_steps, 'headings', @inline_steps, @final_steps);
1109 }
1110  
1111 # "all" or "1": all inline steps before headings
1112 0 my %before;
1113 0 ▶ if ($hm eq '1' || $hm =~ /^all$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hm eq '1' or $hm =~ /^all$/iu) { } 0 0 0
Condition0%0/3
$hm eq 1 or $hm =~ /^all$/iu
l!l&&r!l&&!r
000
MC/DC0%0/2
$hm eq 1 or $hm =~ /^all$/iu
$hm eq 1 $hm =~ /^all$/iu
Truth table0%0/3
A or B
A= $hm eq 1B= $hm =~ /^all$/iu
ABresult
1X1
011
000
1114 0 %before = map { $_ => 1 } @inline_steps;
1115 } else {
1116 # "bold:italic" → collect word tokens, filter to valid inline steps
1117 0 my %valid = map { $_ => 1 } @inline_steps;
1118 0 %before = map { $_ => 1 } grep { $valid{$_} } ($hm =~ /(\w+)/g);
1119 }
1120  
1121 0 my @before_h = grep { $before{$_} } @inline_steps;
1122 0 my @after_h = grep { !$before{$_} } @inline_steps;
1123  
1124 0 return (@protect_steps, @before_h, 'headings', @after_h, @final_steps);
1125 }
1126  
1127 99 ▶sub colorize {
Subroutine colorize: called CC=3 SCAR=11.0
Pod undocumented
1128 99 setup_colors();
1129 99 @protected = ();
1130  
1131 99 for my $name (build_pipeline()) {
1132 1386 my $step = $colorize{$name};
1133 1386 ▶ $step->run if $step->active;
Branch: 1 branch
BranchTrueFalseTotal
if $step->active 1369 17 1386
1134 }
1135  
1136 99 $_ = restore($_);
1137 99 $_;
1138 }
1139  
1140 #
1141 # Table formatting
1142 #
1143  
1144 # What joins the lines of a part kept intact (config intact) into one
1145 # line, as ansicolumn --intact takes it
1146 184 my $INTACT_SEP = "\x1F";
1147  
1148 # Whether the part ("table") is kept intact
1149 317 ▶sub intact {
Subroutine intact: called CC=2 SCAR=7.1
Pod undocumented
1150 317 my $part = shift;
1151 317 ▶ grep { $_ eq $part } split /:/, $config->{intact} // '';
Condition50%1/2
$config->{"intact"} // ""
l!l
3170
MC/DC0%0/1
$config->{"intact"} // ""
$config->{"intact"}
Truth table50%1/2
A
A= $config->{"intact"}
Aresult
11
00
1152 }
1153  
1154 # Regions of formatted tables, used by --fold-by as --exclude so that
1155 # ansifold does not fold them again: three or more lines that contain a
1156 # column separator, in a block that has a separator row. A line may
1157 # start with a border or rule character, a dash, whitespace, or one of
1158 # the configured marks (a wrapped first column's continuation line starts
1159 # with the wrap mark, and its body rows with the cell mark), so the
1160 # pattern is built from the config at run time rather than in __DATA__.
1161 66 ▶sub table_regions {
Subroutine table_regions: called CC=2 SCAR=6.9
Pod undocumented
1162 66 my %arg = @_;
1163 66 delete $arg{&FILELABEL};
1164 39 my $marks = join '', map { quotemeta substr $_, 0, 1 }
1165 66 grep { length } @{$config}{qw(table_wrap_mark table_cell_mark)};
1166 66 my $bar = "\x{2502}|"; # column separator │ |
1167 66 my $cross = "\x{253C}"; # rule crossing ┼
1168 66 my $rule = "\x{2500}\x{251C}\x{2524}"; # rule line ─ ├ ┤
1169 66 my $row = qr{
1170 ^ [ $bar \x{251C} \x{2500} $marks \h - ] # border, rule, mark, pad, or dash
1171 [^\n]* [ $bar $cross ] [^\n]* \n # ... with a separator somewhere
1172 }xxm;
1173 66 my $sep = qr{ (?= [^\n]* [ $cross | ] ) [ $rule $cross | - ]+ \n }xx;
1174 # a table kept intact is a line with UNIT SEPARATORs, which come
1175 # from nowhere else: remove_controls() takes them out of the text
1176 66 ▶ return match_regions(pattern => qr/^[^\n]*$INTACT_SEP[^\n]*\n/m) if intact('table');
Branch: 1 branch
BranchTrueFalseTotal
if intact("table") 1 65 66
1177 65 match_regions(pattern => qr/ (?= (?:$row)*? $sep ) (?:$row){3,} /x);
1178 }
1179  
1180 #
1181 # Paragraph refill
1182 #
1183  
1184 # Join the lines of one paragraph (or list item, quote, definition)
1185 # into a single line. The rules are those of ansifold's fill_up: the
1186 # line break after a fullwidth punctuation character and between two
1187 # characters of a script written without spaces (CJK) is simply
1188 # removed, any other becomes one space. A line is left as it is, as
1189 # the start of a new line, when the line before it ends in a hard
1190 # break (two or more spaces, or a backslash), or when it starts with
1191 # a list marker, is a horizontal rule, which pandoc may report as
1192 # part of the paragraph (a "#." list, "Text\n***"), or when either
1193 # line looks like a table row: GFM does not let a table interrupt a
1194 # paragraph, but format_table takes three such lines as a table.
1195 # From the second line on, up to $depth quote markers and the
1196 # indentation are removed before joining. A CR before the line
1197 # break (CRLF file) goes with the line break.
1198 #
1199 # The character properties are defined as subroutines in App::ansifold
1200 # and looked up when the pattern is compiled, so the patterns are
1201 # built here, after the module is loaded, not at compile time.
1202 73 ▶sub refill_block {
Subroutine refill_block: called CC=13 SCAR=25.7
Pod undocumented
1203 73 my($lines, $depth) = @_;
1204 73 state $punct = do {
1205 18 require App::ansifold;
1206 18 my $p = "App::ansifold::InFullwidthPunctuation";
1207 18 qr/\p{$p}/;
1208 };
1209 73 state $concat = do {
1210 18 my $p = "App::ansifold::InConcatScript";
1211 18 qr/\p{$p}/;
1212 };
1213 73 my $prefix = qr/^\h*(?:>\h?){0,$depth}\h*/;
1214 73 my $marker = qr/^\h*(?:[*+-]|(?:\d+|#)[.)])\h+/;
1215 73 my $hr = qr/^ {0,3}(?:[-*_][ ]*){3,}\r?$/;
1216 73 my $row = qr/^ {0,3}\|.+\|\h*\r?$/;
1217 73 my $def = qr/^\h*:\h/;
1218 73 my $note = qr/$prefix\[\^[^\]\s]+\]:/;
1219 73 my $hard = qr/(?:(?<!\\)(?:\\\\)*\\|(?<! ) {2,})\r?$/; # odd number of backslashes, or two spaces (the lookbehinds keep it linear)
1220 73 my @out;
1221 73 for my $line (@$lines) {
1222 187 ▶ if (!@out or $out[-1] =~ $hard or $out[-1] =~ $row
Branch: 1 branch
BranchTrueFalseTotal
if (not @out or $out[-1] =~ /$hard/u or $out[-1] =~ /$row/u or $line =~ /$marker/u or $line =~ /$hr/u or $line =~ /$row/u or $line =~ /$def/u or $line =~ /$note/u) 124 63 187
Condition100%21/21
not @out or $out[-1] =~ /$hard/u or $out[-1] =~ /$row/u or $line =~ /$marker/u or $line =~ /$hr/u or $line =~ /$row/u or $line =~ /$def/u or $line =~ /$note/u
l!l&&r!l&&!r
121363
not @out or $out[-1] =~ /$hard/u or $out[-1] =~ /$row/u or $line =~ /$marker/u or $line =~ /$hr/u or $line =~ /$row/u or $line =~ /$def/u
l!l&&r!l&&!r
118366
not @out or $out[-1] =~ /$hard/u or $out[-1] =~ /$row/u or $line =~ /$marker/u or $line =~ /$hr/u or $line =~ /$row/u
l!l&&r!l&&!r
113569
not @out or $out[-1] =~ /$hard/u or $out[-1] =~ /$row/u or $line =~ /$marker/u or $line =~ /$hr/u
l!l&&r!l&&!r
112174
not @out or $out[-1] =~ /$hard/u or $out[-1] =~ /$row/u or $line =~ /$marker/u
l!l&&r!l&&!r
111175
not @out or $out[-1] =~ /$hard/u or $out[-1] =~ /$row/u
l!l&&r!l&&!r
773476
not @out or $out[-1] =~ /$hard/u
l!l&&r!l&&!r
734110
MC/DC100%8/8
not @out or $out[-1] =~ /$hard/u or $out[-1] =~ /$row/u or $line =~ /$marker/u or $line =~ /$hr/u or $line =~ /$row/u or $line =~ /$def/u or $line =~ /$note/u
not @out $out[-1] =~ /$hard/u $out[-1] =~ /$row/u $line =~ /$marker/u $line =~ /$hr/u $line =~ /$row/u $line =~ /$def/u $line =~ /$note/u
Truth table100%9/9
not(A) or B or C or D or E or F or G or H
A= not @outB= $out[-1] =~ /$hard/uC= $out[-1] =~ /$row/uD= $line =~ /$marker/uE= $line =~ /$hr/uF= $line =~ /$row/uG= $line =~ /$def/uH= $line =~ /$note/u
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
1223 or $line =~ $marker or $line =~ $hr or $line =~ $row
1224 or $line =~ $def or $line =~ $note) {
1225 124 push @out, $line;
1226 124 next;
1227 }
1228 63 my $text = $line =~ s/$prefix//r;
1229 63 $out[-1] =~ s/\r\z//;
1230 63 ▶ if ($out[-1] =~ /$punct$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($out[-1] =~ /$punct$/u) { } 1 62 63
elsif ($out[-1] =~ /$concat$/u and $text =~ /^$concat/u) { } 2 60 62
1231 1 $out[-1] .= $text;
1232 ▶ } elsif ($out[-1] =~ /$concat$/ and $text =~ /^$concat/) {
Condition66%2/3
$out[-1] =~ /$concat$/u and $text =~ /^$concat/u
!ll&&!rl&&r
6002
MC/DC50%1/2
$out[-1] =~ /$concat$/u and $text =~ /^$concat/u
$out[-1] =~ /$concat$/u $text =~ /^$concat/u
Truth table66%2/3
A and B
A= $out[-1] =~ /$concat$/uB= $text =~ /^$concat/u
ABresult
0X0
100
111
1233 2 $out[-1] .= $text;
1234 } else {
1235 60 $out[-1] =~ s/ +$//;
1236 60 $text =~ s/^ +//;
1237 60 $out[-1] .= " $text";
1238 }
1239 }
1240 73 @out;
1241 }
1242  
1243 # The line ranges that refill() joined, as [first, last] in the text
1244 # after joining, and the formatted tables, as [first line, lines added],
1245 # both for the current file. refill_regions() turns them into the
1246 # regions that --fold-by folds.
1247 184 our @refill_lines;
1248 184 our @table_shift;
1249  
1250 # Front matter at the top of the text: TOML (+++ ... +++) or YAML
1251 # (--- ... --- or ...). It is not part of CommonMark: pandoc takes TOML
1252 # as a paragraph and YAML as metadata, Markdown::Perl takes YAML as
1253 # metadata, and libcmark takes YAML as paragraphs and list items. As
1254 # in pandoc, a blank line right after the opening --- makes it a rule.
1255 # Returns the front matter without the line break after its last line,
1256 # or an empty list. $front_lines is its number of lines, set by
1257 # begin() before the text is changed, for refill and the fold regions.
1258 184 our $front_lines = 0;
1259 275 ▶sub front_matter {
Subroutine front_matter: called CC=3 SCAR=11.0
Pod undocumented
1260 275 ▶ $config->{front_matter} or return;
Branch: 1 branch
BranchTrueFalseTotal
$config->{"front_matter"} or return 272 3 275
1261 272 ▶ $_[0] =~ m{
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ / \A (?: \+\+\+ \r?\n (?:[^\n]*\n)*? \+\+\+ | --- \r?\n (?!\r?\n) (?:[^\n]*\n)*? (?:---|\.\.\.) ) \h* \r? (?=\n|\z) /pux ? : 17 255 272
1262 \A (?: \+\+\+ \r?\n (?:[^\n]*\n)*? \+\+\+
1263 | --- \r?\n (?!\r?\n) (?:[^\n]*\n)*? (?:---|\.\.\.) )
1264 \h* \r? (?=\n|\z)
1265 }px ? ${^MATCH} : ();
1266 }
1267  
1268 # The region of the front matter, used by --fold-by as --exclude. The
1269 # text is colored by then, so the region is found by its line count.
1270 66 ▶sub front_matter_region {
Subroutine front_matter_region: called CC=4 SCAR=14.3
Pod undocumented
1271 66 my %arg = @_;
1272 66 delete $arg{&FILELABEL};
1273 66 ▶ $front_lines or return ();
Branch: 1 branch
BranchTrueFalseTotal
$App::Greple::md::front_lines or return () 1 65 66
1274 1 my $pos = 0;
1275 1 for (1 .. $front_lines) {
1276 1 $pos = index($_, "\n", $pos);
1277 1 ▶ $pos < 0 and return [ 0, length ];
Branch: 1 branch
BranchTrueFalseTotal
$pos < 0 and return [0, length $_] 1 0 1
1278 0 $pos++;
1279 }
1280 0 [ 0, $pos ];
1281 }
1282  
1283 # Join the lines of every paragraph (Para) and tight list item or
1284 # definition (Plain) in $_, from the bottom up so that the line numbers
1285 # of the blocks above stay valid, and remember where the joined lines
1286 # are in the result.
1287 18 ▶sub refill {
Subroutine refill: called CC=7 SCAR=19.5
Pod undocumented
1288 18 @refill_lines = ();
1289 18 my @lines = split /\n/, $_, -1;
1290 18 my @done;
1291 18 my $limit = @lines + 1;
1292 18 my $front = $front_lines;
1293 18 for my $b (sort { $b->{first} <=> $a->{first} }
1294 49 ▶ grep { $_->{type} eq 'Para' or $_->{type} eq 'Plain' }
Condition33%1/3
$_->{'type'} eq 'Para' || $_->{'type'} eq 'Plain'
l!l&&r!l&&!r
4900
MC/DC0%0/2
$_->{'type'} eq 'Para' || $_->{'type'} eq 'Plain'
$_->{'type'} eq 'Para' $_->{'type'} eq 'Plain'
Truth table33%1/3
A || B
A= $_->{'type'} eq 'Para'B= $_->{'type'} eq 'Plain'
ABresult
1X1
011
000
1295 blocks($_, parser => $config->{refill_parser})) {
1296 49 my($first, $last) = @{$b}{qw(first last)};
1297 # libcmark runs a paragraph on past a closing "..."
1298 49 ▶ $first = $front + 1 if $first <= $front;
Branch: 1 branch
BranchTrueFalseTotal
if $first <= $front 1 48 49
1299 49 ▶ next if $last < $first or $last >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $last < $first or $last >= $limit 1 48 49
Condition66%2/3
$last < $first or $last >= $limit
l!l&&r!l&&!r
1048
MC/DC50%1/2
$last < $first or $last >= $limit
$last < $first $last >= $limit
Truth table66%2/3
A or B
A= $last < $firstB= $last >= $limit
ABresult
1X1
011
000
1300 48 $limit = $first;
1301 48 my @joined = refill_block([ @lines[$first-1 .. $last-1] ], $b->{depth});
1302 48 splice @lines, $first-1, $last-$first+1, @joined;
1303 48 unshift @done, [ $first, scalar @joined, $last - $first + 1 - @joined ];
1304 }
1305 18 my $shift = 0;
1306 18 for my $d (@done) {
1307 48 my($first, $count, $removed) = @$d;
1308 48 push @refill_lines, [ $first - $shift, $first - $shift + $count - 1 ];
1309 48 $shift += $removed;
1310 }
1311 18 $_ = join "\n", @lines;
1312 }
1313  
1314 # The lines that refill() joined, one region per line including its
1315 # newline, for --fold-by. The text has been through colorize(), which
1316 # keeps the number of lines, and format_table(), which does not: the
1317 # tables above a range have added the lines in @table_shift.
1318 66 ▶sub refill_regions {
Subroutine refill_regions: called CC=9 SCAR=22.1
Pod undocumented
1319 66 my %arg = @_;
1320 66 delete $arg{&FILELABEL};
1321 66 ▶ return () unless @refill_lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @App::Greple::md::refill_lines 55 11 66
1322 11 my @start = (0);
1323 11 my $pos = 0;
1324 11 ▶ while (($pos = index($_, "\n", $pos)) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($pos = index($_, " ", $pos)) >= 0) 99 11 110
1325 99 push @start, ++$pos;
1326 }
1327 11 my $end = length $_;
1328 11 my @regions;
1329 11 for my $range (@refill_lines) {
1330 29 for my $line ($range->[0] .. $range->[1]) {
1331 53 my $shift = 0;
1332 53 for my $t (@table_shift) {
1333 33 ▶ $shift += $t->[1] if $t->[0] < $line;
Branch: 1 branch
BranchTrueFalseTotal
if $t->[0] < $line 24 9 33
1334 }
1335 53 my $n = $line + $shift;
1336 53 ▶ last if $n > @start;
Branch: 1 branch
BranchTrueFalseTotal
if $n > @start 0 53 53
1337 53 ▶ push @regions, [ $start[$n-1], $start[$n] // $end ];
Condition33%1/3
$start[$n] // $end
l!l&&r!l&&!r
5300
MC/DC0%0/2
$start[$n] // $end
$start[$n] $end
Truth table33%1/3
A // B
A= $start[$n]B= $end
ABresult
1X1
011
000
1338 }
1339 }
1340 11 @regions;
1341 }
1342  
1343  
1344 # What a file must not send to the terminal: everything but colors, and the
1345 # mark of protect(), which would bring back or look for protected text.
1346 184 my $STRING = qr{
1347 (?: \e [PX^_] | [\x90\x98\x9e\x9f] ) # DCS, SOS, PM, APC
1348 (?: [^\x00-\x07\x0e-\x1f\x7f-\x9f]*+ (?: \e\\ | \x9c | \a ) )?
1349 }x;
1350 184 my $CONTROLS = qr{
1351 \e \[ (?! 256m ) [0-9:;]* m (*SKIP)(*FAIL)
1352 |
1353 $osc_re
1354 |
1355 $STRING
1356 |
1357 $csi_re
1358 |
1359 \e [\x20-\x2f]* [\x30-\x7e]
1360 |
1361 [\x00-\x08\x0b\x0e-\x1f\x7f-\x9f]
1362 }x;
1363  
1364 205 ▶sub remove_controls {
Subroutine remove_controls: called CC=1 SCAR=0.0
Pod undocumented
1365 205 s/$CONTROLS//g;
1366 }
1367  
1368 # Find the function of a --begin option and load its package, so that a
1369 # module does not have to be given with -M ahead of this one. A name without
1370 # a package is looked for in main, and then as the function name of the
1371 # module App::Greple::md::name. Returns the option with the full name. A
1372 # function that does not exist is only reported, and the text is shown
1373 # without it; a module that exists and does not compile is an error.
1374 21 ▶sub resolve_function {
Subroutine resolve_function: called CC=6 SCAR=17.9
Pod undocumented
1375 21 my $spec = shift;
1376 21 ▶ $spec =~ m{ ^ &? (?<name> \w+ (?: :: \w+ )* ) (?<arg> (?= [(=] | $ ) .* ) }xs or return $spec;
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ / ^ &? (?<name> \w+ (?: :: \w+ )* ) (?<arg> (?= [(=] | $ ) .* ) /sux or return $spec 15 6 21
1377 15 my($name, $arg) = @+{qw(name arg)};
1378 15 ▶ my @candidate = $name =~ /::/ ? $name : ("main::$name", "App::Greple::md::${name}::$name");
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /::/u ? : 8 7 15
1379 184 ▶ no strict q(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
1380 15 for my $func (@candidate) {
1381 21 ▶ load_package($func =~ s/::\w+$//r) unless defined &$func;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$func 19 2 21
1382 18 ▶ return "&$func$arg" if defined &$func;
Branch: 1 branch
BranchTrueFalseTotal
if defined &$func 6 12 18
1383 }
1384 6 warn "--begin: $name: function not found\n";
1385 6 return;
1386 }
1387  
1388 19 ▶sub load_package {
Subroutine load_package: called CC=4 SCAR=13.9
Pod undocumented
1389 19 my $package = shift;
1390 19 ▶ return if $package eq q(main);
Branch: 1 branch
BranchTrueFalseTotal
if $package eq "main" 7 12 19
1391 12 my $file = $package =~ s{::}{/}gr . q(.pm);
1392 12 ▶ return if eval { require $file; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require $file; 1 } } 5 7 12
1393 7 ▶ die $@ unless $@ =~ /^Can't locate \Q$file\E /;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /^Can't locate \Q$file\E /u 3 4 7
1394 }
1395  
1396 # The functions of --begin, parsed once. The names belong to main, as
1397 # with the --begin of greple.
1398 180 ▶sub begin_functions {
Subroutine begin_functions: called CC=3 SCAR=11.0
Pod undocumented
1399 state $func = [ map {
1400 180 ▶ parse_func({ PACKAGE => 'main' }, $_) or die "$_: function format error\n";
Branch: 1 branch
BranchTrueFalseTotal
parse_func({"PACKAGE", "main"}, $_) or die "${_}: function format error\n" 21 0 21
1401 21 my $spec = resolve_function($_);
1402 18 ▶ defined $spec ? parse_func({ PACKAGE => 'main' }, $spec) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $spec ? : 12 6 18
1403 } @opt_begin ];
1404 176 @$func;
1405 }
1406  
1407 180 ▶sub begin {
Subroutine begin: called CC=7 SCAR=19.5
Pod undocumented
1408 180 remove_controls();
1409 180 @refill_lines = @table_shift = ();
1410 180 for my $func (begin_functions()) {
1411 14 $func->call(@_);
1412 }
1413 176 ▶ $front_lines = do { my $f = front_matter($_); defined $f ? ($f =~ tr/\n//) + 1 : 0 };
Branch: 1 branch
BranchTrueFalseTotal
defined $f ? : 11 165 176
1414 176 ▶ refill() if $config->{refill};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"refill"} 18 158 176
1415 176 ▶ mark_line_breaks() if $config->{table};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"table"} 159 17 176
1416 176 ▶ colorize() if $config->{colorize};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"colorize"} 99 77 176
1417 176 ▶ format_table() if $config->{table};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"table"} 159 17 176
1418 }
1419  
1420 # A <br> in a table row breaks the cell: a LINE SEPARATOR (U+2028)
1421 # is put after it, which ansicolumn takes as a line break in a cell.
1422 # The <br> stays, as the text is shown as written. It is done before
1423 # colorize(), while a code span still has its backticks: a <br> in a
1424 # code span, and an escaped one (\<br>), break nothing, nor does one at
1425 # the end of a cell, which would leave an empty line. The lines are
1426 # those format_table() takes as a table; a separator left in lines it
1427 # does not format after all (in a code block, say) is taken out there.
1428 159 ▶sub mark_line_breaks {
Subroutine mark_line_breaks: called CC=1 SCAR=0.0
Pod undocumented
1429 159 s{(^ {0,3}\|.+\|\h*\n){3,}}{
1430 251 $& =~ s{$CODE(*SKIP)(*FAIL)|(?<!\\)<br\h*/?>\K(?!\h*\|)}{\x{2028}}gir
1431 }mge;
1432 }
1433  
1434 159 ▶sub format_table {
Subroutine format_table: called CC=19 SCAR=29.4
Pod undocumented
1435 # lattice (or pipe) has column separators. 3line and simple have
1436 # none and a header rule per column: 3line adds a full-width rule
1437 # above and below, simple the header rule below. They are formatted
1438 # as in rule mode and the separators are blanked.
1439 159 my $style = $config->{table_style};
1440 159 ▶ my $open = $style eq '3line' || $style eq 'simple';
Condition100%3/3
$style eq '3line' || $style eq 'simple'
l!l&&r!l&&!r
44151
MC/DC100%2/2
$style eq '3line' || $style eq 'simple'
$style eq '3line' $style eq 'simple'
Truth table100%3/3
A || B
A= $style eq '3line'B= $style eq 'simple'
ABresult
1X1
011
000
1441 159 ▶ my $sep = ($config->{rule} || $open) ? "\x{2502}" : '|'; # │ or |
Branch: 1 branch
BranchTrueFalseTotal
$config->{'rule'} || $open ? : 158 1 159
Condition100%3/3
$config->{"rule"} or $open
l!l&&r!l&&!r
15621
MC/DC100%2/2
$config->{"rule"} or $open
$config->{"rule"} $open
Truth table100%3/3
A or B
A= $config->{"rule"}B= $open
ABresult
1X1
011
000
1442  
1443 # A row may have spaces after the last pipe (GFM ignores them); they
1444 # are allowed in the block and removed, so that every row ends in
1445 # the same column for ansicolumn.
1446 159 s{(^ {0,3}\|.+\|\h*\n){3,}}{
1447 251 my $block = $& =~ s/\h+$//mgr;
1448 # a <br> which breaks a cell (see mark_line_breaks) has the
1449 # color of br_mark, which the nomark theme makes empty
1450 12 $block =~ s{<br\h*/?>(?=\x{2028})}{mark_color('br', $&)}gie
1451 251 ▶ if $config->{colorize};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"colorize"} 165 86 251
1452 251 my $line = 1 + (substr($_, 0, $-[0]) =~ tr/\n//);
1453 251 my $lines = $& =~ tr/\n//;
1454 251 my($right, $center) = parse_separator(\$block);
1455 251 my @sep_opt;
1456 # An escaped pipe (\|) is not a column separator (GFM). It is
1457 # left as written: backslash escapes are not processed yet.
1458 251 ▶ if ($config->{table_trim}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'table_trim'}) { } 246 5 251
1459 246 @sep_opt = ('-rs', '\s*(?<!\\\\)\|\s*', '--item-format= %s ', '--table-remove=1,-0', '--padding');
1460 } else {
1461 5 $_++ for @$right, @$center;
1462 5 @sep_opt = ('-rs', '(?<!\\\\)\|');
1463 }
1464 251 ▶ my @align = (
Branch: 2 branches
BranchTrueFalseTotal
@$right ? : 47 204 251
@$center ? : 47 204 251
1465 @$right ? ('--table-right=' . join(',', @$right)) : (),
1466 @$center ? ('--table-center=' . join(',', @$center)) : (),
1467 );
1468 # Fit table into table_width and/or per-column widths (table_cell).
1469 # table_cell entries are colon separated because comma separates
1470 # config parameters; non-trim mode has a phantom first column.
1471 251 my @fit;
1472 251 ▶ if ($config->{table_width} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"table_width"} > 0) 123 128 251
1473 123 push @fit, '-w', $config->{table_width};
1474 }
1475 251 ▶ if ((my $cell = $config->{table_cell}) ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $cell = $config->{"table_cell"}) ne "") 3 248 251
1476 3 $cell =~ tr/:/,/;
1477 3 ▶ $cell = "0,$cell" unless $config->{table_trim};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"table_trim"} 1 2 3
1478 3 push @fit, '--cell', $cell;
1479 }
1480 251 ▶ if ((my $mark = $config->{table_wrap_mark}) ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $mark = $config->{"table_wrap_mark"}) ne "") 4 247 251
1481 4 push @fit, '--table-wrap-mark', $mark;
1482 }
1483 251 ▶ if ((my $mark = $config->{table_cell_mark}) ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $mark = $config->{"table_cell_mark"}) ne "") 107 144 251
1484 # the header row and the separator row (to ansicolumn a row of
1485 # "-" cells) are the two header rows and get no mark
1486 107 push @fit, '--table-cell-mark', $mark, '--table-head', 2;
1487 }
1488 # How cells are wrapped is given explicitly, so that it does not
1489 # change with the user's ~/.ansicolumnrc
1490 251 my @wrap = map { "--$_=" . $config->{"table_$_"} } qw(runin runout boundary linebreak);
1491 251 my $formatted = call_ansicolumn($block, @sep_opt, '-o', $sep, '-t', '--cu=1', @align, @fit, @wrap);
1492 251 my $table = fix_separator($formatted, $sep);
1493 251 ▶ if ($open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open) 20 231 251
1494 # blank the column separators and add the rules
1495 20 $table =~ tr/\x{2502}\x{253C}\x{251C}\x{2524}/ /;
1496 20 ▶ if ($style eq '3line') {
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq '3line') { } 10 10 20
1497 10 my($first) = $table =~ /^(.*)$/m;
1498 10 my $rule = "\x{2500}" x ansi_width($first);
1499 10 $table = "$rule\n$table$rule\n";
1500 } else {
1501 10 my($rule) = $table =~ /^([\x{2500} ]+)$/m;
1502 10 ▶ $table .= "$rule\n" if defined $rule;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule 10 0 10
1503 }
1504 20 ▶ $table =~ s{^[\x{2500} ]+$}{ $& =~ tr/\x{2500}/-/r }mge unless $config->{rule};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"rule"} 8 12 20
1505 }
1506 # A table is kept intact: its lines joined into one by UNIT
1507 # SEPARATOR, the last newline kept, for ansicolumn --intact,
1508 # which lays them out without folding them. Nothing after this
1509 # takes a table by its lines but table_regions().
1510 251 ▶ $table =~ s/\n(?=.)/$INTACT_SEP/gs if intact('table');
Branch: 1 branch
BranchTrueFalseTotal
if intact("table") 1 250 251
1511 251 push @table_shift, [ $line, ($table =~ tr/\n//) - $lines ];
1512 251 $table;
1513 }mge;
1514 # the separators of mark_line_breaks() left out of the tables
1515 159 s{<br\h*/?>\K\x{2028}}{}gi;
1516 }
1517  
1518 258 ▶sub parse_separator {
Subroutine parse_separator: called CC=3 SCAR=11.0
Pod undocumented
1519 258 my $blockref = shift;
1520 258 my $SEP = qr/^\h*+\|(\h*+:?+-++:?+\h*+\|)++\h*+$/mn;
1521 258 my ($sep_line) = $$blockref =~ /($SEP)/;
1522 258 ▶ return ([], []) unless defined $sep_line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep_line 1 257 258
1523 257 my @cells = split /\|/, $sep_line, -1;
1524 257 shift @cells; pop @cells;
1525 257 s/^\h+|\h+$//g for @cells;
1526 257 my @right = grep { $cells[$_-1] =~ /^-+:$/ } 1..@cells;
1527 257 my @center = grep { $cells[$_-1] =~ /^:-+:$/ } 1..@cells;
1528 # Minimize dashes so separator width doesn't inflate column widths
1529 257 $$blockref =~ s{$SEP}{ ${^MATCH} =~ s/:?-+:?/-/gr }mpe;
1530 257 (\@right, \@center);
1531 }
1532  
1533 # Color the code of fenced code blocks by a highlighter of
1534 # Text::ANSI::Highlight: the one of the name in the config highlight,
1535 # or its default for 1. Takes [ language, code ] of each block, and
1536 # returns as many: the code in colors, or undef for a block which is
1537 # not colored, as one with no language or with one the highlighter
1538 # does not know. All are undef when the module, the highlighter or
1539 # its command is not there, and the blocks are shown as they are.
1540 #
1541 # All the blocks are given at once, and each is colored by itself.
1542 # What comes back is the code which was given, with colors, so the
1543 # lines are those of the file.
1544 # The labels of the lines of a code block, when it has parts: a block
1545 # of pandoc-embedz has config, template and data. A line of a part
1546 # labelled NAME is in the color code_NAME, when it is defined.
1547 237 ▶sub code_labels {
Subroutine code_labels: called CC=4 SCAR=13.9
Pod undocumented
1548 237 my($info, $body) = @_;
1549 237 ▶ my @part = eval {
Branch: 1 branch
BranchTrueFalseTotal
my(@part) = eval { do { require Text::ANSI::Highlight; "Text::ANSI::Highlight"->parts($info, $body) } } or return () 237 0 237
1550 237 require Text::ANSI::Highlight;
1551 237 Text::ANSI::Highlight->parts($info, $body);
1552 } or return ();
1553 # in a block with labelled parts, the lines of the others, which
1554 # are between them, have no color
1555 237 ▶ (grep { defined $_->[2] } @part) or return ();
Branch: 1 branch
BranchTrueFalseTotal
grep {defined $_->[2];} @part or return () 3 234 237
1556 3 ▶ map { my @line = split /(?<=\n)/, $_->[1]; ($_->[2] // '') x @line } @part;
Condition100%2/2
$_->[2] // ""
l!l
53
MC/DC100%1/1
$_->[2] // ""
$_->[2]
Truth table100%2/2
A
A= $_->[2]
Aresult
11
00
1557 }
1558  
1559 # the highlighter in the dark mode, when none is named
1560 184 our $DARK_HIGHLIGHT = 'pandoc:breezedark';
1561  
1562 97 ▶sub highlight_code {
Subroutine highlight_code: called CC=7 SCAR=19.5
Pod undocumented
1563 97 my @code = @_;
1564 97 my @none = (undef) x @code;
1565 97 ▶ @code or return @none;
Branch: 1 branch
BranchTrueFalseTotal
@code or return @none 65 32 97
1566 65 my $name = $config->{highlight};
1567 # the default is pandoc, in a style for a dark screen in the dark
1568 # mode
1569 my @with = $name ne '1' ? $name
1570 65 ▶ : ($config->{mode} // '') eq 'dark' ? $DARK_HIGHLIGHT : ();
Branch: 2 branches
BranchTrueFalseTotal
$name ne '1' ? : 6 59 65
($config->{'mode'} // '') eq 'dark' ? : 3 56 59
Condition50%1/2
$config->{"mode"} // ""
l!l
590
MC/DC0%0/1
$config->{"mode"} // ""
$config->{"mode"}
Truth table50%1/2
A
A= $config->{"mode"}
Aresult
11
00
1571 65 ▶ my $hl = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $hl = eval { do { require Text::ANSI::Highlight; "Text::ANSI::Highlight"->new(@with, "background", 0) } } or return @none 64 1 65
1572 65 require Text::ANSI::Highlight;
1573 # the background is that of the code block: a style for a
1574 # dark screen has its own
1575 65 Text::ANSI::Highlight->new(@with, background => 0);
1576 } or return @none;
1577 64 my @color = eval { $hl->highlight(@code) };
1578 64 ▶ @color == @code ? @color : @none;
Branch: 1 branch
BranchTrueFalseTotal
@color == @code ? : 64 0 64
1579 }
1580  
1581 252 ▶sub call_ansicolumn {
Subroutine call_ansicolumn: called CC=3 SCAR=11.0
Pod undocumented
1582 252 my ($text, @args) = @_;
1583 252 require Command::Run;
1584 252 require App::ansicolumn;
1585 Command::Run->new
1586 ->command(\&App::ansicolumn::ansicolumn, @args)
1587 ->with(stdin => $text,
1588 252 ▶ $config->{nofork} ? (nofork => 1, raw => 1) : ())
Branch: 1 branch
BranchTrueFalseTotal
$config->{'nofork'} ? : 248 4 252
Condition50%1/2
"Command::Run"->new->command(\&App::ansicolumn::ansicolumn, @args)->with("stdin", $text, $config->{"nofork"} ? ("nofork", 1, "raw", 1) : ())->update->data // ""
l!l
2520
MC/DC0%0/1
"Command::Run"->new->command(\&App::ansicolumn::ansicolumn, @args)->with("stdin", $text, $config->{"nofork"} ? ("nofork", 1, "raw", 1) : ())->update->data // ""
"Command::Run"->new->command(\&App::ansicolumn::ansicolumn, @args)->with("stdin", $text, $config->{"nofork"} ? ("nofork", 1, "raw", 1) : ())->update->data
Truth table50%1/2
A
A= "Command::Run"->new->command(\&App::ansicolumn::ansicolumn, @args)->with("stdin", $text, $config->{"nofork"} ? ("nofork", 1, "raw", 1) : ())->update->data
Aresult
11
00
1589 ->update
1590 ->data // '';
1591 }
1592  
1593 251 ▶sub fix_separator {
Subroutine fix_separator: called CC=7 SCAR=19.5
Pod undocumented
1594 251 my ($text, $sep) = @_;
1595 251 ▶ my $sep_re = $sep eq "\x{2502}" ? "\x{2502}" : '\\|';
Branch: 1 branch
BranchTrueFalseTotal
$sep eq "\x{2502}" ? : 247 4 251
1596 251 $text =~ s{^(\h*?)($sep_re)?((?:\h*-+\h*$sep_re)*\h*-+\h*)($sep_re)?(\h*?)$}{
1597 251 my($pre, $left, $mid, $right, $post) = ($1, $2, $3, $4, $5);
1598 251 ▶ if ($sep eq "\x{2502}") {
Branch: 1 branch
BranchTrueFalseTotal
if ($sep eq "\x{2502}") { } 247 4 251
1599 247 ▶ ($pre =~ tr[ ][\x{2500}]r)
Branch: 2 branches
BranchTrueFalseTotal
defined $left ? : 5 242 247
defined $right ? : 5 242 247
1600 . (defined $left ? "\x{251C}" : '')
1601 . ($mid =~ tr[\x{2502} -][\x{253C}\x{2500}\x{2500}]r)
1602 . (defined $right ? "\x{2524}" : '')
1603 . ($post =~ tr[ ][\x{2500}]r)
1604 } else {
1605 4 ▶ ($pre =~ tr[ ][-]r)
Branch: 2 branches
BranchTrueFalseTotal
defined $left ? : 0 4 4
defined $right ? : 0 4 4
1606 . (defined $left ? '|' : '')
1607 . ($mid =~ tr[ ][-]r)
1608 . (defined $right ? '|' : '')
1609 . ($post =~ tr[ ][-]r)
1610 }
1611 }xmeg;
1612 251 $text;
1613 }
1614  
1615 184 1;
1616  
1617 __DATA__
« blib/lib/App/Greple/md/Blocks.pm ↑ Summary blib/lib/App/Greple/md/widen.pm »