Skip to content

Commit 0e2cd07

Browse files
author
Anthony Parsons
committed
Replace most subs with node2html() multis
Also fixed headings/TOC links showing "FormattingCode<>" when they shouldn't.
1 parent 79d9ca8 commit 0e2cd07

File tree

1 file changed

+82
-76
lines changed

1 file changed

+82
-76
lines changed

lib/Pod/To/HTML.pm

Lines changed: 82 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ my @indexes;
77
my @body;
88

99
sub pod2html($pod) is export {
10-
@body.push: whatever2html($pod);
10+
@body.push: node2html($pod);
1111

1212
my $title_html = $title // 'Pod document';
1313

@@ -30,24 +30,9 @@ sub pod2html($pod) is export {
3030
~ "</body>\n</html>";
3131
}
3232

33-
sub whatever2html($node) {
34-
given $node {
35-
when Pod::Heading { heading2html($node) }
36-
when Pod::Block::Code { code2html($node) }
37-
when Pod::Block::Named { named2html($node) }
38-
when Pod::Block::Para { para2html($node) }
39-
when Pod::Block::Table { table2html($node) }
40-
# when Pod::Block::Declarator { declarator2html($node) }
41-
when Pod::Item { item2html($node) }
42-
when Positional { $node.map({whatever2html($_)}).join }
43-
when Pod::Block::Comment { }
44-
default { $node.Str }
45-
}
46-
}
47-
4833
sub metadata {
4934
@meta.map(-> $p {
50-
qq[<meta name="{$p.key}" value="{$p.value}" />\n]
35+
qq[<meta name="{escape_html($p.key)}" value="{escape_html($p.value)}" />\n]
5136
}).join;
5237
}
5338

@@ -84,87 +69,77 @@ sub buildindexes {
8469
return $r ~ "</nav>\n";
8570
}
8671

87-
sub heading2html($pod) {
88-
my $lvl = min($pod.level, 6);
89-
my %escaped = ($_ => escape($pod.content[0].content, $_) for <uri html>);
90-
@indexes.push: Pair.new(key => $lvl, value => %escaped);
72+
sub prose2html($pod, $sep = '') {
73+
escape_html($pod.content.join($sep));
74+
}
9175

92-
return
93-
sprintf('<h%d id="%s">', $lvl, %escaped<uri>)
94-
~ qq[<a class="u" href="#___top" title="go to top of document">]
95-
~ %escaped<html>
96-
~ qq[</a>]
97-
~ qq[</h{$lvl}>\n];
76+
sub twine2text($twine) {
77+
return '' unless $twine.elems;
78+
my $r = $twine[0];
79+
for $twine[1..*] -> $f, $s {
80+
$r ~= twine2text($f.content);
81+
$r ~= $s;
82+
}
83+
return $r;
84+
}
85+
86+
87+
multi sub node2html($node) {
88+
note "{:$node.perl} is missing a node2html multi";
89+
$node.Str;
90+
}
91+
92+
multi sub node2html(Pod::Block::Code $node) {
93+
'<pre>' ~ prose2html($node) ~ "</pre>\n"
94+
}
95+
96+
multi sub node2html(Pod::Block::Comment $node) {
9897
}
9998

100-
sub named2html($pod) {
101-
given $pod.name {
102-
when 'pod' { whatever2html($pod.content) }
103-
when 'para' { para2html($pod.content[0]) }
104-
when 'defn' { whatever2html($pod.content[0]) ~ "\n"
105-
~ whatever2html($pod.content[1..*-1]) }
99+
multi sub node2html(Pod::Block::Named $node) {
100+
given $node.name {
101+
when 'pod' { node2html($node.content) }
102+
when 'para' { node2html($node.content[0]) }
103+
when 'defn' { node2html($node.content[0]) ~ "\n"
104+
~ node2html($node.content[1..*-1]) }
106105
when 'config' { }
107106
when 'nested' { }
108107
default {
109-
if $pod.name eq 'TITLE' {
110-
$title = prose2html($pod.content[0]);
108+
if $node.name eq 'TITLE' {
109+
$title = prose2html($node.content[0]);
111110
}
112-
elsif $pod.name ~~ any(<VERSION DESCRIPTION AUTHOR COPYRIGHT SUMMARY>)
113-
and $pod.content[0] ~~ Pod::Block::Para {
114-
@meta.push: Pair.new(key => $pod.name.lc, value => prose2html($pod.content[0]));
111+
elsif $node.name ~~ any(<VERSION DESCRIPTION AUTHOR COPYRIGHT SUMMARY>)
112+
and $node.content[0] ~~ Pod::Block::Para {
113+
@meta.push: Pair.new(
114+
key => $node.name.lc,
115+
value => twine2text($node.content[0].content)
116+
);
115117
}
116118

117119
'<section>'
118-
~ "<h1>{$pod.name}</h1>\n"
119-
~ whatever2html($pod.content)
120+
~ "<h1>{$node.name}</h1>\n"
121+
~ node2html($node.content)
120122
~ "</section>\n"
121123
}
122124
}
123125
}
124126

125-
sub prose2html($pod, $sep = '') {
126-
escape($pod.content.join($sep), 'html');
127-
}
128-
129-
sub para2html($pod) {
130-
'<p>' ~ escape(twine2text($pod.content), 'html') ~ "</p>\n"
127+
multi sub node2html(Pod::Block::Para $node) {
128+
'<p>' ~ escape_html(twine2text($node.content)) ~ "</p>\n"
131129
}
132130

133-
sub code2html($pod) {
134-
'<pre>' ~ prose2html($pod) ~ "</pre>\n"
135-
}
136-
137-
sub item2html($pod) {
138-
#FIXME
139-
'<ul><li>' ~ whatever2html($pod.content) ~ "</li></ul>\n"
140-
}
141-
142-
sub formatting2text($pod) {
143-
twine2text($pod.content)
144-
}
145-
146-
sub twine2text($twine) {
147-
return '' unless $twine.elems;
148-
my $r = $twine[0];
149-
for $twine[1..*] -> $f, $s {
150-
$r ~= twine2text($f.content);
151-
$r ~= $s;
152-
}
153-
return $r;
154-
}
155-
156-
sub table2html($pod) {
157-
my @r;
131+
multi sub node2html(Pod::Block::Table $pod) {
132+
my @r = '<table>';
158133

159134
if $pod.caption {
160-
@r.push("<caption>{escape($pod.caption, 'html')}</caption>");
135+
@r.push("<caption>{escape_html($pod.caption)}</caption>");
161136
}
162137

163138
if $pod.headers {
164139
@r.push(
165140
'<thead><tr>',
166141
$pod.headers.map(-> $cell {
167-
"<th>{escape($cell, 'html')}</th>"
142+
"<th>{node2html($cell)}</th>"
168143
}),
169144
'</tr></thead>'
170145
);
@@ -175,12 +150,43 @@ sub table2html($pod) {
175150
$pod.content.map(-> $line {
176151
'<tr>',
177152
$line.list.map(-> $cell {
178-
"<td>{escape($cell, 'html')}</td>"
153+
"<td>{node2html($cell)}</td>"
179154
}),
180155
'</tr>'
181156
}),
182-
'</tbody>'
157+
'</tbody>',
158+
'</table>'
183159
);
184160

185-
return "<table>\n{@r.join("\n")}\n</table>";
161+
return @r.join("\n");
162+
}
163+
164+
multi sub node2html(Pod::Heading $node) {
165+
my $lvl = min($node.level, 6);
166+
my $plaintext = twine2text($node.content[0].content);
167+
my %escaped = (
168+
uri => escape_uri($plaintext),
169+
html => escape_html($plaintext),
170+
);
171+
@indexes.push: Pair.new(key => $lvl, value => %escaped);
172+
173+
return
174+
sprintf('<h%d id="%s">', $lvl, %escaped<uri>)
175+
~ qq[<a class="u" href="#___top" title="go to top of document">]
176+
~ %escaped<html>
177+
~ qq[</a>]
178+
~ qq[</h{$lvl}>\n];
179+
}
180+
181+
# FIXME
182+
multi sub node2html(Pod::Item $node) {
183+
'<ul><li>' ~ node2html($node.content) ~ "</li></ul>\n"
184+
}
185+
186+
multi sub node2html(Positional $node) {
187+
$node.map({ node2html($_) }).join
188+
}
189+
190+
multi sub node2html(Str $node) {
191+
escape_html($node);
186192
}

0 commit comments

Comments
 (0)