@@ -7,7 +7,7 @@ my @indexes;
7
7
my @ body ;
8
8
9
9
sub pod2html ($ pod ) is export {
10
- @ body . push : whatever2html ($ pod );
10
+ @ body . push : node2html ($ pod );
11
11
12
12
my $ title_html = $ title // ' Pod document' ;
13
13
@@ -30,24 +30,9 @@ sub pod2html($pod) is export {
30
30
~ " </body>\n </html>" ;
31
31
}
32
32
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
-
48
33
sub metadata {
49
34
@ 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 ]
51
36
}). join ;
52
37
}
53
38
@@ -84,87 +69,77 @@ sub buildindexes {
84
69
return $ r ~ " </nav>\n " ;
85
70
}
86
71
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
+ }
91
75
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 ) {
98
97
}
99
98
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 ]) }
106
105
when ' config' { }
107
106
when ' nested' { }
108
107
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 ]);
111
110
}
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
+ );
115
117
}
116
118
117
119
' <section>'
118
- ~ " <h1>{ $ pod . name } </h1>\n "
119
- ~ whatever2html( $ pod . content)
120
+ ~ " <h1>{ $ node . name } </h1>\n "
121
+ ~ node2html( $ node . content)
120
122
~ " </section>\n "
121
123
}
122
124
}
123
125
}
124
126
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 "
131
129
}
132
130
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>' ;
158
133
159
134
if $ pod . caption {
160
- @ r . push (" <caption>{ escape ($ pod . caption, ' html ' )} </caption>" );
135
+ @ r . push (" <caption>{ escape_html ($ pod . caption)} </caption>" );
161
136
}
162
137
163
138
if $ pod . headers {
164
139
@ r . push (
165
140
' <thead><tr>' ,
166
141
$ pod . headers. map (-> $ cell {
167
- " <th>{ escape ($ cell , ' html ' )} </th>"
142
+ " <th>{ node2html ($ cell )} </th>"
168
143
}),
169
144
' </tr></thead>'
170
145
);
@@ -175,12 +150,43 @@ sub table2html($pod) {
175
150
$ pod . content. map (-> $ line {
176
151
' <tr>' ,
177
152
$ line . list. map (-> $ cell {
178
- " <td>{ escape ($ cell , ' html ' )} </td>"
153
+ " <td>{ node2html ($ cell )} </td>"
179
154
}),
180
155
' </tr>'
181
156
}),
182
- ' </tbody>'
157
+ ' </tbody>' ,
158
+ ' </table>'
183
159
);
184
160
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 );
186
192
}
0 commit comments