38
38
*/
39
39
public class CoreJavaDocNodeRenderer extends AbstractVisitor implements NodeRenderer {
40
40
41
+ private static final String [] html5Tags = {
42
+ "<a>" ,
43
+ "<abbr>" ,
44
+ "<address>" ,
45
+ "<area>" ,
46
+ "<article>" ,
47
+ "<aside>" ,
48
+ "<audio>" ,
49
+ "<b>" ,
50
+ "<base>" ,
51
+ "<bdi>" ,
52
+ "<bdo>" ,
53
+ "<blockquote>" ,
54
+ "<body>" ,
55
+ "<br>" ,
56
+ "<button>" ,
57
+ "<canvas>" ,
58
+ "<caption>" ,
59
+ "<cite>" ,
60
+ "<code>" ,
61
+ "<col>" ,
62
+ "<colgroup>" ,
63
+ "<data>" ,
64
+ "<datalist>" ,
65
+ "<dd>" ,
66
+ "<del>" ,
67
+ "<details>" ,
68
+ "<dfn>" ,
69
+ "<dialog>" ,
70
+ "<div>" ,
71
+ "<dl>" ,
72
+ "<dt>" ,
73
+ "<em>" ,
74
+ "<embed>" ,
75
+ "<fieldset>" ,
76
+ "<figcaption>" ,
77
+ "<figure>" ,
78
+ "<footer>" ,
79
+ "<form>" ,
80
+ "<h1>" ,
81
+ "<h2>" ,
82
+ "<h3>" ,
83
+ "<h4>" ,
84
+ "<h5>" ,
85
+ "<h6>" ,
86
+ "<head>" ,
87
+ "<header>" ,
88
+ "<hr>" ,
89
+ "<html>" ,
90
+ "<i>" ,
91
+ "<iframe>" ,
92
+ "<img>" ,
93
+ "<input>" ,
94
+ "<ins>" ,
95
+ "<kbd>" ,
96
+ "<label>" ,
97
+ "<legend>" ,
98
+ "<li>" ,
99
+ "<link>" ,
100
+ "<main>" ,
101
+ "<map>" ,
102
+ "<mark>" ,
103
+ "<meta>" ,
104
+ "<meter>" ,
105
+ "<nav>" ,
106
+ "<noscript>" ,
107
+ "<object>" ,
108
+ "<ol>" ,
109
+ "<optgroup>" ,
110
+ "<option>" ,
111
+ "<output>" ,
112
+ "<p>" ,
113
+ "<param>" ,
114
+ "<picture>" ,
115
+ "<pre>" ,
116
+ "<progress>" ,
117
+ "<q>" ,
118
+ "<rp>" ,
119
+ "<rt>" ,
120
+ "<ruby>" ,
121
+ "<s>" ,
122
+ "<samp>" ,
123
+ "<script>" ,
124
+ "<section>" ,
125
+ "<select>" ,
126
+ "<small>" ,
127
+ "<source>" ,
128
+ "<span>" ,
129
+ "<strong>" ,
130
+ "<style>" ,
131
+ "<sub>" ,
132
+ "<summary>" ,
133
+ "<sup>" ,
134
+ "<svg>" ,
135
+ "<table>" ,
136
+ "<tbody>" ,
137
+ "<td>" ,
138
+ "<template>" ,
139
+ "<textarea>" ,
140
+ "<tfoot>" ,
141
+ "<th>" ,
142
+ "<thead>" ,
143
+ "<time>" ,
144
+ "<title>" ,
145
+ "<tr>" ,
146
+ "<track>" ,
147
+ "<u>" ,
148
+ "<ul>" ,
149
+ "<var>" ,
150
+ "<video>" ,
151
+ "<wbr>" ,
152
+ };
153
+ private static final Set <String > allowedHtml5Tags = new HashSet <>(Arrays .asList (html5Tags ));
154
+ private static final Map <String , String > urlLinkConversion =
155
+ new HashMap <String , String >() {
156
+ {
157
+ put ("../../../api_docs/python/math_ops" , "org.tensorflow.op.MathOps" );
158
+ put (
159
+ "https://www.tensorflow.org/api_docs/python/tf/tensor_scatter_nd_update" ,
160
+ "org.tensorflow.op.Ops#tensorScatterNdUpdate" );
161
+ }
162
+ };
41
163
protected final JavaDocNodeRendererContext context ;
42
164
private final JavaDocWriter writer ;
43
165
private boolean firstParagraph ;
@@ -73,16 +195,6 @@ public Set<Class<? extends Node>> getNodeTypes() {
73
195
HardLineBreak .class ));
74
196
}
75
197
76
- private static Map <String , String > urlLinkConversion =
77
- new HashMap <String , String >() {
78
- {
79
- put ("../../../api_docs/python/math_ops" , "org.tensorflow.op.MathOps" );
80
- put (
81
- "https://www.tensorflow.org/api_docs/python/tf/tensor_scatter_nd_update" ,
82
- "org.tensorflow.op.Ops#tensorScatterNdUpdate" );
83
- }
84
- };
85
-
86
198
@ Override
87
199
public void render (Node node ) {
88
200
node .accept (this );
@@ -97,12 +209,11 @@ public void visit(Document document) {
97
209
98
210
@ Override
99
211
public void visit (Heading heading ) {
100
- String htag = "h" + heading .getLevel ();
101
- writer .line ();
102
- writer .tag (htag , getAttrs (heading , htag ));
212
+ // cannot use <h1>, JavaDoc complains.
213
+ writer .tag ("strong" );
103
214
visitChildren (heading );
104
- writer .tag ('/' + htag );
105
- writer .line ( );
215
+ writer .tag ("/strong" );
216
+ writer .tag ( "br" );
106
217
}
107
218
108
219
@ Override
@@ -181,12 +292,12 @@ public void visit(ThematicBreak thematicBreak) {
181
292
@ Override
182
293
public void visit (IndentedCodeBlock indentedCodeBlock ) {
183
294
renderCodeBlock (
184
- indentedCodeBlock .getLiteral (), indentedCodeBlock , Collections .< String , String > emptyMap ());
295
+ indentedCodeBlock .getLiteral (), indentedCodeBlock , Collections .emptyMap ());
185
296
}
186
297
187
298
@ Override
188
299
public void visit (Link link ) {
189
- Map < String , String > attrs = new LinkedHashMap <>();
300
+
190
301
String url = link .getDestination ();
191
302
192
303
if (url .contains ("api_docs/python" )) {
@@ -197,17 +308,24 @@ public void visit(Link link) {
197
308
if (endIndex == -1 ) {
198
309
String key = url .substring (startIndex );
199
310
opClass = urlLinkConversion .get (key );
200
-
311
+ if (opClass == null ) {
312
+ handleNormalURL (link );
313
+ return ;
314
+ }
201
315
} else {
202
316
String key = url .substring (startIndex , endIndex );
203
317
opClass = urlLinkConversion .get (key );
318
+ if (opClass == null ) {
319
+ handleNormalURL (link );
320
+ return ;
321
+ }
204
322
method = url .substring (endIndex + 1 );
205
323
if (method .contains ("_" )) {
206
324
method = CaseFormat .LOWER_UNDERSCORE .to (CaseFormat .LOWER_CAMEL , method );
207
325
}
208
326
if (Character .isUpperCase (method .charAt (0 ))) {
209
327
// change method first char to lower_case.
210
- char c [] = method .toCharArray ();
328
+ char [] c = method .toCharArray ();
211
329
c [0 ] = Character .toLowerCase (c [0 ]);
212
330
method = new String (c );
213
331
}
@@ -218,23 +336,29 @@ public void visit(Link link) {
218
336
writer .append (String .format (" {@link %s} " , opClass ));
219
337
220
338
} else {
221
- if (context .shouldSanitizeUrls ()) {
222
- url = context .urlSanitizer ().sanitizeLinkUrl (url );
223
- attrs .put ("rel" , "nofollow" );
224
- }
225
- writer .append (" " );
226
- url = context .encodeUrl (url );
227
- attrs .put ("href" , url );
228
- if (link .getTitle () != null ) {
229
- attrs .put ("title" , link .getTitle ());
230
- }
231
- writer .tag ("a" , getAttrs (link , "a" , attrs ));
232
- visitChildren (link );
233
- writer .tag ("/a" );
234
- writer .append (" " );
339
+ handleNormalURL (link );
235
340
}
236
341
}
237
342
343
+ private void handleNormalURL (Link link ) {
344
+ Map <String , String > attrs = new LinkedHashMap <>();
345
+ String url = link .getDestination ();
346
+ if (context .shouldSanitizeUrls ()) {
347
+ url = context .urlSanitizer ().sanitizeLinkUrl (url );
348
+ attrs .put ("rel" , "nofollow" );
349
+ }
350
+ writer .append (" " );
351
+ url = context .encodeUrl (url );
352
+ attrs .put ("href" , url );
353
+ if (link .getTitle () != null ) {
354
+ attrs .put ("title" , link .getTitle ());
355
+ }
356
+ writer .tag ("a" , getAttrs (link , "a" , attrs ));
357
+ visitChildren (link );
358
+ writer .tag ("/a" );
359
+ writer .append (" " );
360
+ }
361
+
238
362
@ Override
239
363
public void visit (ListItem listItem ) {
240
364
writer .tag ("li" , getAttrs (listItem , "li" ));
@@ -306,10 +430,16 @@ public void visit(Code code) {
306
430
307
431
@ Override
308
432
public void visit (HtmlInline htmlInline ) {
309
- if (context .shouldEscapeHtml ()) {
310
- writer .text (htmlInline .getLiteral ());
433
+ String text = htmlInline .getLiteral ();
434
+ // handle non- JavaDoc html, e.g. <bytes>
435
+ String tag = text .replace ("\\ " , "" );
436
+ if (!allowedHtml5Tags .contains (tag .toLowerCase ())) {
437
+ text = text .replace ("<" , "<" ).replace (">" , ">" );
438
+ writer .raw (text );
439
+ } else if (context .shouldEscapeHtml ()) {
440
+ writer .text (text );
311
441
} else {
312
- writer .raw (htmlInline . getLiteral () );
442
+ writer .raw (text );
313
443
}
314
444
}
315
445
@@ -336,14 +466,17 @@ protected void visitChildren(Node parent) {
336
466
337
467
private void renderCodeBlock (String literal , Node node , Map <String , String > attributes ) {
338
468
writer .line ();
339
- writer .tag ("pre" , getAttrs (node , "pre" ));
340
- // writer.tag("code", getAttrs(node, "code", attributes));
341
- writer .line ();
342
- writer .text (literal );
343
- writer .line ();
344
- // writer.tag("/code");
345
- writer .tag ("/pre" );
346
- writer .line ();
469
+ // skip empty <pre> block
470
+ if (!literal .isEmpty ()) {
471
+ writer .tag ("pre" , getAttrs (node , "pre" ));
472
+ // writer.tag("code", getAttrs(node, "code", attributes));
473
+ writer .line ();
474
+ writer .text (literal );
475
+ writer .line ();
476
+ // writer.tag("/code");
477
+ writer .tag ("/pre" );
478
+ writer .line ();
479
+ }
347
480
}
348
481
349
482
private void renderListBlock (
@@ -370,7 +503,7 @@ private boolean isInTightList(Paragraph paragraph) {
370
503
}
371
504
372
505
private Map <String , String > getAttrs (Node node , String tagName ) {
373
- return getAttrs (node , tagName , Collections .< String , String > emptyMap ());
506
+ return getAttrs (node , tagName , Collections .emptyMap ());
374
507
}
375
508
376
509
private Map <String , String > getAttrs (
0 commit comments