Skip to content

Commit a25c3cb

Browse files
JimClarke5rnett
authored andcommitted
Reformat code.
Fix handling of oddities from Python, like illegal html tag <bytes>
1 parent ee4b57b commit a25c3cb

File tree

1 file changed

+178
-45
lines changed

1 file changed

+178
-45
lines changed

tensorflow-core/tensorflow-core-generator/src/main/java/org/tensorflow/op/generator/javadoc/CoreJavaDocNodeRenderer.java

Lines changed: 178 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,128 @@
3838
*/
3939
public class CoreJavaDocNodeRenderer extends AbstractVisitor implements NodeRenderer {
4040

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+
};
41163
protected final JavaDocNodeRendererContext context;
42164
private final JavaDocWriter writer;
43165
private boolean firstParagraph;
@@ -73,16 +195,6 @@ public Set<Class<? extends Node>> getNodeTypes() {
73195
HardLineBreak.class));
74196
}
75197

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-
86198
@Override
87199
public void render(Node node) {
88200
node.accept(this);
@@ -97,12 +209,11 @@ public void visit(Document document) {
97209

98210
@Override
99211
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");
103214
visitChildren(heading);
104-
writer.tag('/' + htag);
105-
writer.line();
215+
writer.tag("/strong");
216+
writer.tag("br");
106217
}
107218

108219
@Override
@@ -181,12 +292,12 @@ public void visit(ThematicBreak thematicBreak) {
181292
@Override
182293
public void visit(IndentedCodeBlock indentedCodeBlock) {
183294
renderCodeBlock(
184-
indentedCodeBlock.getLiteral(), indentedCodeBlock, Collections.<String, String>emptyMap());
295+
indentedCodeBlock.getLiteral(), indentedCodeBlock, Collections.emptyMap());
185296
}
186297

187298
@Override
188299
public void visit(Link link) {
189-
Map<String, String> attrs = new LinkedHashMap<>();
300+
190301
String url = link.getDestination();
191302

192303
if (url.contains("api_docs/python")) {
@@ -197,17 +308,24 @@ public void visit(Link link) {
197308
if (endIndex == -1) {
198309
String key = url.substring(startIndex);
199310
opClass = urlLinkConversion.get(key);
200-
311+
if (opClass == null) {
312+
handleNormalURL(link);
313+
return;
314+
}
201315
} else {
202316
String key = url.substring(startIndex, endIndex);
203317
opClass = urlLinkConversion.get(key);
318+
if (opClass == null) {
319+
handleNormalURL(link);
320+
return;
321+
}
204322
method = url.substring(endIndex + 1);
205323
if (method.contains("_")) {
206324
method = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, method);
207325
}
208326
if (Character.isUpperCase(method.charAt(0))) {
209327
// change method first char to lower_case.
210-
char c[] = method.toCharArray();
328+
char[] c = method.toCharArray();
211329
c[0] = Character.toLowerCase(c[0]);
212330
method = new String(c);
213331
}
@@ -218,23 +336,29 @@ public void visit(Link link) {
218336
writer.append(String.format(" {@link %s} ", opClass));
219337

220338
} 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);
235340
}
236341
}
237342

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+
238362
@Override
239363
public void visit(ListItem listItem) {
240364
writer.tag("li", getAttrs(listItem, "li"));
@@ -306,10 +430,16 @@ public void visit(Code code) {
306430

307431
@Override
308432
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("<", "&lt;").replace(">", "&gt;");
438+
writer.raw(text);
439+
} else if (context.shouldEscapeHtml()) {
440+
writer.text(text);
311441
} else {
312-
writer.raw(htmlInline.getLiteral());
442+
writer.raw(text);
313443
}
314444
}
315445

@@ -336,14 +466,17 @@ protected void visitChildren(Node parent) {
336466

337467
private void renderCodeBlock(String literal, Node node, Map<String, String> attributes) {
338468
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+
}
347480
}
348481

349482
private void renderListBlock(
@@ -370,7 +503,7 @@ private boolean isInTightList(Paragraph paragraph) {
370503
}
371504

372505
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());
374507
}
375508

376509
private Map<String, String> getAttrs(

0 commit comments

Comments
 (0)