File Coverage

blib/lib/Text/HTML/Turndown/HighlightedCodeBlock.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 18 19 94.7


line stmt bran cond sub pod time code
1             package Text::HTML::Turndown::HighlightedCodeBlock 0.12;
2 1     1   774 use 5.020;
  1         4  
3 1     1   7 use experimental 'signatures';
  1         2  
  1         7  
4 1     1   150 use List::MoreUtils 'all';
  1         3  
  1         8  
5              
6             our $highlightRegExp = qr/highlight-(?:text|source)-([a-z0-9]+)/;
7             our %RULES = (
8              
9             highlightedCodeBlock => {
10             filter => sub ($rule, $node, $options) {
11             my $firstChild = $node->firstChild;
12             return (
13             uc $node->nodeName eq 'DIV' &&
14             $node->className =~ /$highlightRegExp/ &&
15             $firstChild &&
16             uc $firstChild->nodeName eq 'PRE'
17             )
18             },
19             replacement => sub( $content, $node, $options, $context ) {
20             my $className = $node->className || '';
21             my $language = '';
22             if( $className =~ /$highlightRegExp/) {
23             $language = $1;
24             };
25              
26             return (
27             "\n\n" . $options->{fence} . $language . "\n" .
28             $node->firstChild->textContent .
29             "\n" . $options->{fence} . "\n\n"
30             )
31             }
32             },
33             );
34              
35 22     22 0 49 sub install ($class, $target) {
  22         46  
  22         43  
  22         57  
36 22         86 for my $key (keys %RULES) {
37 22         81 $target->addRule($key, $RULES{$key})
38             }
39             }
40              
41             1;
42              
43             =head1 REPOSITORY
44              
45             The public repository of this module is
46             L.
47              
48             =head1 SUPPORT
49              
50             The public support forum of this module is L.
51              
52             =head1 BUG TRACKER
53              
54             Please report bugs in this module via the Github bug queue at
55             L
56              
57             =head1 AUTHOR
58              
59             Max Maischein C
60              
61             =head1 COPYRIGHT (c)
62              
63             Copyright 2025- by Max Maischein C.
64              
65             =head1 LICENSE
66              
67             This module is released under the Artistic License 2.0.
68              
69             =cut