File Coverage

blib/lib/String/Tagged/Markdown/HFM.pm
Criterion Covered Total %
statement 36 46 78.2
branch 7 14 50.0
condition 2 6 33.3
subroutine 12 14 85.7
pod 2 5 40.0
total 59 85 69.4


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2023 -- leonerd@leonerd.org.uk
5              
6             package String::Tagged::Markdown::HFM 0.05;
7              
8 2     2   231239 use v5.26;
  2         12  
9 2     2   10 use warnings;
  2         4  
  2         54  
10 2     2   519 use experimental 'signatures';
  2         3582  
  2         45  
11 2     2   282 use base qw( String::Tagged::Markdown );
  2         5  
  2         1196  
12              
13             =head1 NAME
14              
15             C - parse and emit text with F
16              
17             =head1 SYNOPSIS
18              
19             use String::Tagged::Markdown::HFM;
20              
21             my $st = String::Tagged::Markdown::HFM->parse_markdown( $markdown );
22              
23             # Conforms to the String::Tagged::Formatting API
24             String::Tagged::Terminal->new_from_formatting(
25             $st->as_formatting
26             )->say_to_terminal;
27              
28             =head1 DESCRIPTION
29              
30             This subclass of L handles all of the Markdown
31             syntax recognised by the base class, and in addition the inline span marker
32             extensions that are recognised by F, the version
33             of Markdown used on L.
34              
35             =head1 TAGS
36              
37             This module adds the following extra tags.
38              
39             =head2 superscript, subscript, underline, highlight
40              
41             Boolean values indicating superscript, subscript, underline or highlight.
42             These are parsed from
43              
44             ^superscript^
45             ~subscript~
46             ++underline++
47             ==highlight==
48              
49             =cut
50              
51             sub markdown_markers
52             {
53             shift->SUPER::markdown_markers,
54 1     1 0 8 "++" => "underline",
55             "^" => "superscript",
56             "~" => "subscript",
57             "==" => "highlight";
58             }
59              
60             our $HIGHLIGHT_COLOUR;
61              
62             =head1 METHODS
63              
64             =head2 as_formatting
65              
66             $fmt = $st->as_formatting( %args );
67              
68             Returns a new C instance tagged with
69             L standard tags.
70              
71             By default the C tag is not handled, but optionally the caller can
72             specify how to handle it by setting a callback in the C
73             argument.
74              
75             $st->as_formatting(
76             convert_tags => { highlight => sub { ... } }
77             );
78              
79             Alternatively, this can be handled automatically by providing a colour to be
80             set as the value of the C tag - either by passing the C
81             named argument, or setting the value of the package-global
82             C<$HIGHLIGHT_COLOUR>. Remember that this should be an instance of
83             L.
84              
85             $st->as_formatting(
86             highlight_colour => Convert::Color->new( "vga:yellow" )
87             );
88              
89             =cut
90              
91             sub tags_to_formatting
92             {
93             shift->SUPER::tags_to_formatting,
94             underline => "under",
95 1     1   128 superscript => sub { sizepos => "super" },
96 1     1   116 subscript => sub { sizepos => "sub" },
97 1     1 0 6 }
98              
99 1         4 sub as_formatting ( $self, %args )
100 1     1 1 2 {
  1         2  
  1         2  
101 1 50       7 my %convert_tags = $args{convert_tags} ? $args{convert_tags}->%* : ();
102              
103 1 50 33     21 if( my $highlight_colour = delete $args{highlight_colour} // $HIGHLIGHT_COLOUR ) {
104 0     0   0 $convert_tags{highlight} = sub { bg => $highlight_colour };
  0         0  
105             }
106              
107 1         12 return $self->SUPER::as_formatting(
108             %args,
109             convert_tags => \%convert_tags,
110             );
111             }
112              
113             =head2 new_from_formatting
114              
115             $st = String::Tagged::Markdown::HFM->new_from_formatting( $fmt, %args );
116              
117             Returns a new instance by converting L standard
118             tags.
119              
120             By default the C tag is not generated, but optionally the caller
121             can specify how to generate it by setting a callback in the C
122             argument, perhaps by inspecting the background colour.
123              
124             String::Tagged::Markdown::HFM->new_from_formatting( $orig,
125             convert_tags => { bg => sub ($k, $v) { ... } }
126             );
127              
128             Alternatively, this can be handled automatically by providing a colour to be
129             matched against the C tag - either by passing the C
130             named argument, or setting the value of the package-global
131             C<$HIGHLIGHT_COLOUR>. Remember that this should be an instance of
132             L. If the value of the C is within 5% of this colour, the
133             C tag will be applied.
134              
135             String::Tagged::Markdown::HFM->new_from_formatting( $orig,
136             highlight_colour => Convert::Color->new( "vga:yellow" )
137             );
138              
139             =cut
140              
141             sub tags_from_formatting
142             {
143             shift->SUPER::tags_from_formatting,
144 2         5 under => "underline",
145 2     2   144 sizepos => sub ($k, $v) {
  2         3  
  2         5  
146 2 50       12 $v eq "super" ? ( superscript => 1 ) :
    100          
147             $v eq "sub" ? ( subscript => 1 ) :
148             ()
149             },
150 1     1 0 9 }
151              
152 1         3 sub new_from_formatting ( $class, $orig, %args )
  1         2  
153 1     1 1 2461 {
  1         2  
  1         3  
154 1 50       21 my %convert_tags = $args{convert_tags} ? $args{convert_tags}->%* : ();
155              
156 1 50 33     9 if( my $highlight_colour = delete $args{highlight_colour} // $HIGHLIGHT_COLOUR ) {
157 0         0 $highlight_colour = $highlight_colour->as_rgb;
158              
159 0     0   0 $convert_tags{bg} = sub ($k, $v) {
  0            
  0            
  0            
160 0 0       0 return highlight => 1 if $highlight_colour->dst_rgb( $v ) <= 0.05;
161 0         0 return ();
162 0         0 };
163             }
164              
165 1         34 return $class->SUPER::new_from_formatting( $orig,
166             %args,
167             convert_tags => \%convert_tags,
168             );
169             }
170              
171             =head1 AUTHOR
172              
173             Paul Evans
174              
175             =cut
176              
177             0x55AA;