blib/lib/App/Greple/md.pm

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