File Coverage

blib/lib/String/Tagged/Markdown/HFM.pm
Criterion Covered Total %
statement 45 46 97.8
branch 8 14 57.1
condition 2 6 33.3
subroutine 14 14 100.0
pod 2 5 40.0
total 71 85 83.5


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.03;
7              
8 2     2   230507 use v5.26;
  2         16  
9 2     2   12 use warnings;
  2         4  
  2         54  
10 2     2   544 use experimental 'signatures';
  2         3581  
  2         17  
11 2     2   350 use base qw( String::Tagged::Markdown );
  2         6  
  2         1199  
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 9 "++" => "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   136 superscript => sub { sizepos => "super" },
96 1     1   121 subscript => sub { sizepos => "sub" },
97 1     1 0 7 }
98              
99 1         2 sub as_formatting ( $self, %args )
100 1     1 1 4 {
  1         2  
  1         3  
101 1 50       5 my %convert_tags = $args{convert_tags} ? $args{convert_tags}->%* : ();
102              
103 1 50 33     15 if( my $highlight_colour = delete $args{highlight_colour} // $HIGHLIGHT_COLOUR ) {
104 1     1   8 $convert_tags{highlight} = sub { bg => $highlight_colour };
  1         142  
105             }
106              
107 1         14 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         4 under => "underline",
145 2     2   148 sizepos => sub ($k, $v) {
  2         12  
  2         3  
146 2 50       36 $v eq "super" ? ( superscript => 1 ) :
    100          
147             $v eq "sub" ? ( subscript => 1 ) :
148             ()
149             },
150 1     1 0 6 }
151              
152 1         4 sub new_from_formatting ( $class, $orig, %args )
  1         3  
153 1     1 1 2500 {
  1         2  
  1         2  
154 1 50       7 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 1         35 $highlight_colour = $highlight_colour->as_rgb;
158              
159 1     1   3 $convert_tags{bg} = sub ($k, $v) {
  1         139  
  1         2  
  1         3  
160 1 50       8 return highlight => 1 if $highlight_colour->dst_rgb( $v ) <= 0.05;
161 0         0 return ();
162 1         36 };
163             }
164              
165 1         11 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;