blib/lib/CommonMark/Massage.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 #! perl
2  
3 package CommonMark::Massage;
4  
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use CommonMark qw( :node :event );
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 our $VERSION = '0.02';
10  
11 =head1 NAME
12  
13 CommonMark::Massage - Manipulate CommonMark AST
14  
15 =head1 SYNOPSIS
16  
17 use CommonMark qw(:node :event);
18 use CommonMark::Massage;
19  
20 my $parser = CommonMark::Parser->new;
21 $parser->feed("Hello world");
22 my $doc = $parser->finish;
23  
24 # Apply function to text nodes.
25 my $doc->massage ( { NODE_TEXT => sub { ... } } } );
26 $doc->render_html;
27  
28 =head1 DESCRIPTION
29  
30 The massage function can be used to manipulate the AST as produced by
31 the CommonMark parsers.
32  
33 =head1 METHODS
34  
35 The methods are defined in the CommonMark::Node namespace, so they can
36 be applied to the result of parsing.
37  
38 =head2 massage
39  
40 One argument: a hash ref of node names, to a subroutine. For example:
41  
42 { NODE_TEXT => \&fixit }
43  
44 The subroutine is called with three arguments, the doc tree, the node,
45 and a boolean indicating whether the call is upon an EVENT_ENTER (true)
46 or EVENT_EXIT (false).
47  
48 It is free to do whatever it wants, but caveat emptor.
49  
50 See L<EXAMPLES> and the example directory for some example routines.
51  
52 =cut
53  
54 sub CommonMark::Node::massage {
55 my ( $doc, $ctl ) = @_;
56  
57 # Turn mnemonics into values.
58 for ( keys(%$ctl) ) {
59 next if /^\d+$/; # already numeric
60 no strict 'refs';
61 $ctl->{$_->()} = delete $ctl->{$_};
62 }
63  
64 my $iter = $doc->iterator;
65  
66 while (my ($ev_type, $node) = $iter->next) {
67 my $node_type = $node->get_type;
68  
69 if ( $ctl->{$node_type} ) {
70 next unless my $code = $ctl->{$node_type};
71 $code->( $doc, $node, $ev_type == EVENT_ENTER );
72 }
73 }
74 }
75  
76 =head2 reveal
77  
78 This method dumps the AST nodes/events for entertainment and debugging.
79  
80 The result is returned as a string.
81  
82 =cut
83  
84 my $node_names;
85 my $event_types;
86  
87 sub CommonMark::Node::reveal {
88 my ( $doc, $fh ) = @_;
89  
90 _constants() unless $node_names;
91  
92 my $iter = $doc->iterator;
93 my $res = "";
94 while (my ($ev_type, $node) = $iter->next) {
95 my $node_type = $node->get_type;
96 $res .= "$event_types->[$ev_type] $node_names->[$node_type]\n";
97 }
98 $res;
99 }
100  
101 sub _constants {
102 my @names = qw(
103 NODE_NONE
104 NODE_DOCUMENT
105 NODE_BLOCK_QUOTE
106 NODE_LIST
107 NODE_ITEM
108 NODE_CODE_BLOCK
109 NODE_HTML
110 NODE_PARAGRAPH
111 NODE_HEADER
112 NODE_HRULE
113 NODE_TEXT
114 NODE_SOFTBREAK
115 NODE_LINEBREAK
116 NODE_CODE
117 NODE_INLINE_HTML
118 NODE_EMPH
119 NODE_STRONG
120 NODE_LINK
121 NODE_IMAGE
122 NODE_CUSTOM_BLOCK
123 NODE_CUSTOM_INLINE
124 NODE_HTML_BLOCK
125 NODE_HEADING
126 NODE_THEMATIC_BREAK
127 NODE_HTML_INLINE
128 );
129 my @values = (
130 NODE_NONE,
131 NODE_DOCUMENT,
132 NODE_BLOCK_QUOTE,
133 NODE_LIST,
134 NODE_ITEM,
135 NODE_CODE_BLOCK,
136 NODE_HTML,
137 NODE_PARAGRAPH,
138 NODE_HEADER,
139 NODE_HRULE,
140 NODE_TEXT,
141 NODE_SOFTBREAK,
142 NODE_LINEBREAK,
143 NODE_CODE,
144 NODE_INLINE_HTML,
145 NODE_EMPH,
146 NODE_STRONG,
147 NODE_LINK,
148 NODE_IMAGE,
149 NODE_CUSTOM_BLOCK,
150 NODE_CUSTOM_INLINE,
151 NODE_HTML_BLOCK,
152 NODE_HEADING,
153 NODE_THEMATIC_BREAK,
154 NODE_HTML_INLINE,
155 );
156 for ( @names ) {
157 $node_names->[shift(@values)] = $_;
158 }
159 $event_types = [ qw( NONE DONE ENTR EXIT ) ];
160 }
161  
162 =head1 EXAMPLES
163  
164 This example manipulates links. Normally a link is rendered as
165  
166 <a href="uri">text</a>
167  
168 After massaging with
169  
170 $doc->massage( { NODE_LINK => { EVENT_EXIT => \&fixlink } } )
171  
172 this will become for non-local links:
173  
174 <a href="http://www.example.com" target="_blank">text</a>
175  
176 This is the subroutine
177  
178 sub fixlink {
179 my ( $doc, $node ) = @_;
180 # Get the link and title.
181 my $link = $node->get_url // "";
182 my $title = $node->get_title // "";
183  
184 # Create a new custom node.
185 my $n = CommonMark::Node->new(NODE_CUSTOM_INLINE);
186  
187 # The replacement 'enter' text.
188 my $enter = "<a href=\"$link\"";
189 $enter .= " title=\"$title\"" if $title;
190 $enter .= " target=\"_blank\"" if $link =~ /^\w+:\/\//;
191 $enter .= ">";
192 $n->set_on_enter($enter);
193  
194 # The 'exit' text.
195 $n->set_on_exit("</a>" );
196  
197 # NODE_LINK has a single child, copy it to the new node.
198 my $t = $node->first_child;
199 $n->append_child($t);
200 $t->unlink;
201  
202 # Replace the LINK node by the CUSTOM node.
203 $node->replace($n);
204 }
205  
206 =head1 AUTHOR
207  
208 Johan Vromans, C<< <JV at cpan.org> >>
209  
210 =head1 SUPPORT AND DOCUMENTATION
211  
212 Development of this module takes place on GitHub:
213 https://github.com/sciurius/perl-CommonMark-Massage.
214  
215 You can find documentation for this module with the perldoc command.
216  
217 perldoc CommonMark::Massage
218  
219 Please report any bugs or feature requests using the issue tracker on
220 GitHub.
221  
222 =head1 SEE ALSO
223  
224 L<CommonMark>
225  
226 =head1 COPYRIGHT & LICENSE
227  
228 Copyright 2020 Johan Vromans, all rights reserved.
229  
230 This program is free software; you can redistribute it and/or modify it
231 under the same terms as Perl itself.
232  
233 =cut
234  
235 1;
↑ Summary