Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 3687fec

Browse files
authored
KDoc: Add support for bold, italics, and code (#387)
* KDoc: Add support for bold, italics, and code * Also handle strong and em * Demonstrate formatting in the Java example
1 parent 094b282 commit 3687fec

File tree

13 files changed

+122
-58
lines changed

13 files changed

+122
-58
lines changed

samples/java-webmvc/generated-docs/index.html

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,10 +1109,13 @@ <h3 id="resources-item-resource-test-add-item"><a class="anchor" href="#resource
11091109
<p><code>POST /items</code></p>
11101110
</div>
11111111
<div class="paragraph">
1112-
<p>Adds new item.</p>
1112+
<p>Adds a new <strong>item</strong>.</p>
11131113
</div>
11141114
<div class="paragraph">
1115-
<p>An example of accepting a custom type as request body and returning a {@link ResponseEntity}.</p>
1115+
<p>An <em>example</em> of accepting a custom type as request body and returning a {@link ResponseEntity}.</p>
1116+
</div>
1117+
<div class="paragraph">
1118+
<p><code>someCode("foo")</code>.</p>
11161119
</div>
11171120
<div class="sect3">
11181121
<h4 id="_authorization_3"><a class="anchor" href="#_authorization_3"></a><a class="link" href="#_authorization_3">2.3.1. Authorization</a></h4>
@@ -1236,7 +1239,7 @@ <h4 id="_example_request"><a class="anchor" href="#_example_request"></a><a clas
12361239
<div class="content">
12371240
<pre class="highlightjs highlight"><code class="language-bash hljs" data-lang="bash">$ curl 'http://localhost:8080/items' -i -X POST \
12381241
-H 'Content-Type: application/json' \
1239-
-H 'Authorization: Bearer 0b765e43-cb96-4f00-b747-af7e62590e11' \
1242+
-H 'Authorization: Bearer 5f5cb4c9-67a4-44da-bf06-e7d299ed9287' \
12401243
-d '{"description":"Hot News"}'</code></pre>
12411244
</div>
12421245
</div>
@@ -1539,7 +1542,7 @@ <h4 id="_example_request_2"><a class="anchor" href="#_example_request_2"></a><a
15391542
<div class="content">
15401543
<pre class="highlightjs highlight"><code class="language-bash hljs" data-lang="bash">$ curl 'http://localhost:8080/items/1' -i -X PUT \
15411544
-H 'Content-Type: application/json' \
1542-
-H 'Authorization: Bearer 0b765e43-cb96-4f00-b747-af7e62590e11' \
1545+
-H 'Authorization: Bearer 5f5cb4c9-67a4-44da-bf06-e7d299ed9287' \
15431546
-d '{"description":"Hot News"}'</code></pre>
15441547
</div>
15451548
</div>
@@ -1663,7 +1666,7 @@ <h4 id="_example_request_3"><a class="anchor" href="#_example_request_3"></a><a
16631666
<div class="listingblock">
16641667
<div class="content">
16651668
<pre class="highlightjs highlight"><code class="language-bash hljs" data-lang="bash">$ curl 'http://localhost:8080/items/1' -i -X DELETE \
1666-
-H 'Authorization: Bearer 0b765e43-cb96-4f00-b747-af7e62590e11'</code></pre>
1669+
-H 'Authorization: Bearer 5f5cb4c9-67a4-44da-bf06-e7d299ed9287'</code></pre>
16671670
</div>
16681671
</div>
16691672
</div>
@@ -2228,15 +2231,15 @@ <h4 id="_example_response_5"><a class="anchor" href="#_example_response_5"></a><
22282231
"totalElements" : 1,
22292232
"last" : true,
22302233
"size" : 20,
2234+
"numberOfElements" : 1,
2235+
"first" : true,
22312236
"number" : 0,
22322237
"sort" : {
22332238
"orders" : [ ],
22342239
"sorted" : false,
22352240
"unsorted" : true,
22362241
"empty" : true
22372242
},
2238-
"numberOfElements" : 1,
2239-
"first" : true,
22402243
"empty" : false
22412244
}</code></pre>
22422245
</div>
@@ -3527,7 +3530,7 @@ <h4 id="_example_response_12"><a class="anchor" href="#_example_response_12"></a
35273530
</div>
35283531
<div id="footer">
35293532
<div id="footer-text">
3530-
Last updated 2020-02-10 13:59:32 +0100
3533+
Last updated 2020-04-12 14:15:02 +0200
35313534
</div>
35323535
</div>
35333536
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/github.min.css">

samples/java-webmvc/src/main/java/capital/scalable/restdocs/example/items/ItemResource.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ public ItemResponse[] allItems() {
108108
}
109109

110110
/**
111-
* Adds new item.
111+
* Adds a new <b>item</b>.
112112
* <p>
113-
* An example of accepting a custom type as request body and returning a {@link ResponseEntity}.
113+
* An <i>example</i> of accepting a custom type as request body and returning a {@link ResponseEntity}.
114+
* <p>
115+
* <code>someCode("foo")</code>
114116
*
115117
* @param itemUpdate Item information
116118
* @return response

samples/kotlin-webmvc/generated-docs/index.html

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,10 +1073,13 @@ <h3 id="resources-item-resource-test-add-item"><a class="anchor" href="#resource
10731073
<p><code>POST /items</code></p>
10741074
</div>
10751075
<div class="paragraph">
1076-
<p>Adds new item.</p>
1076+
<p>Adds a new <strong>item</strong>.</p>
10771077
</div>
10781078
<div class="paragraph">
1079-
<p>An example of accepting a custom type as request body and returning a ResponseEntity.</p>
1079+
<p>An <em>example</em> of accepting a custom type as request body and returning a ResponseEntity.</p>
1080+
</div>
1081+
<div class="paragraph">
1082+
<p><code>someCode("foo")</code></p>
10801083
</div>
10811084
<div class="sect3">
10821085
<h4 id="_authorization_3"><a class="anchor" href="#_authorization_3"></a><a class="link" href="#_authorization_3">2.3.1. Authorization</a></h4>
@@ -1186,7 +1189,7 @@ <h4 id="_example_request"><a class="anchor" href="#_example_request"></a><a clas
11861189
<div class="content">
11871190
<pre class="highlightjs highlight"><code class="language-bash hljs" data-lang="bash">$ curl 'http://localhost:8080/items' -i -X POST \
11881191
-H 'Content-Type: application/json' \
1189-
-H 'Authorization: Bearer e75106d3-5eb2-456f-9d98-f0d516990b01' \
1192+
-H 'Authorization: Bearer 81c803fa-adc9-4c33-8d7f-d460882862a3' \
11901193
-d '{"description":"Hot News"}'</code></pre>
11911194
</div>
11921195
</div>
@@ -1468,7 +1471,7 @@ <h4 id="_example_request_2"><a class="anchor" href="#_example_request_2"></a><a
14681471
<div class="content">
14691472
<pre class="highlightjs highlight"><code class="language-bash hljs" data-lang="bash">$ curl 'http://localhost:8080/items/1' -i -X PUT \
14701473
-H 'Content-Type: application/json' \
1471-
-H 'Authorization: Bearer e75106d3-5eb2-456f-9d98-f0d516990b01' \
1474+
-H 'Authorization: Bearer 81c803fa-adc9-4c33-8d7f-d460882862a3' \
14721475
-d '{"description":"Hot News"}'</code></pre>
14731476
</div>
14741477
</div>
@@ -1576,7 +1579,7 @@ <h4 id="_example_request_3"><a class="anchor" href="#_example_request_3"></a><a
15761579
<div class="listingblock">
15771580
<div class="content">
15781581
<pre class="highlightjs highlight"><code class="language-bash hljs" data-lang="bash">$ curl 'http://localhost:8080/items/1' -i -X DELETE \
1579-
-H 'Authorization: Bearer e75106d3-5eb2-456f-9d98-f0d516990b01'</code></pre>
1582+
-H 'Authorization: Bearer 81c803fa-adc9-4c33-8d7f-d460882862a3'</code></pre>
15801583
</div>
15811584
</div>
15821585
</div>
@@ -2089,22 +2092,22 @@ <h4 id="_example_response_5"><a class="anchor" href="#_example_response_5"></a><
20892092
"offset" : 0,
20902093
"pageNumber" : 0,
20912094
"pageSize" : 20,
2092-
"paged" : true,
2093-
"unpaged" : false
2095+
"unpaged" : false,
2096+
"paged" : true
20942097
},
20952098
"total" : 1,
2099+
"totalElements" : 1,
20962100
"totalPages" : 1,
20972101
"last" : true,
2098-
"totalElements" : 1,
20992102
"size" : 20,
2100-
"numberOfElements" : 1,
2103+
"number" : 0,
21012104
"sort" : {
21022105
"orders" : [ ],
21032106
"sorted" : false,
21042107
"unsorted" : true,
21052108
"empty" : true
21062109
},
2107-
"number" : 0,
2110+
"numberOfElements" : 1,
21082111
"first" : true,
21092112
"empty" : false
21102113
}</code></pre>
@@ -2766,7 +2769,7 @@ <h4 id="_example_requestresponse_3"><a class="anchor" href="#_example_requestres
27662769
</div>
27672770
<div id="footer">
27682771
<div id="footer-text">
2769-
Last updated 2020-02-10 13:52:46 +0100
2772+
Last updated 2020-04-12 14:15:02 +0200
27702773
</div>
27712774
</div>
27722775
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/github.min.css">

samples/kotlin-webmvc/src/main/java/capital/scalable/restdocs/example/items/ItemResource.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ internal class ItemResource {
8585
fun allItems() = arrayOf(ITEM, CHILD)
8686

8787
/**
88-
* Adds new item.
88+
* Adds a new **item**.
8989
*
90+
* An *example* of accepting a custom type as request body and returning a [ResponseEntity].
9091
*
91-
* An example of accepting a custom type as request body and returning a [ResponseEntity].
92+
* `someCode("foo")`
9293
*
9394
* @param itemUpdate Item information
9495
* @return response

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/javadoc/JavadocUtil.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -45,8 +45,14 @@ public static String convertFromJavadoc(String javadoc, TemplateFormatting templ
4545
.replace("</li>", "")
4646
.replace("<b>", templateFormatting.getBold())
4747
.replace("</b>", templateFormatting.getBold())
48+
.replace("<strong>", templateFormatting.getBold())
49+
.replace("</strong>", templateFormatting.getBold())
4850
.replace("<i>", templateFormatting.getItalics())
4951
.replace("</i>", templateFormatting.getItalics())
52+
.replace("<em>", templateFormatting.getItalics())
53+
.replace("</em>", templateFormatting.getItalics())
54+
.replace("<code>", templateFormatting.getCode())
55+
.replace("</code>", templateFormatting.getCode())
5056
.replaceAll("<a\\s+href\\s*=\\s*[\"\'](.*?)[\"\']\\s*>(.*?)</a>",
5157
templateFormatting.link());
5258

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/util/TemplateFormatting.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -26,24 +26,28 @@ public class TemplateFormatting {
2626
private static final String BOLD_MD = "**";
2727
private static final String ITALICS_ADOC = "__";
2828
private static final String ITALICS_MD = "*";
29+
private static final String CODE_ADOC = "`";
30+
private static final String CODE_MD = "`";
2931
private static final String LINK_ADOC = "link:$1[$2]";
3032
private static final String LINK_MD = "[$2]($1)";
3133

3234
public static TemplateFormatting ASCIIDOC =
33-
new TemplateFormatting(LINE_BREAK_ADOC, BOLD_ADOC, ITALICS_ADOC, LINK_ADOC);
35+
new TemplateFormatting(LINE_BREAK_ADOC, BOLD_ADOC, ITALICS_ADOC, CODE_ADOC, LINK_ADOC);
3436

3537
public static TemplateFormatting MARKDOWN =
36-
new TemplateFormatting(LINE_BREAK_MD, BOLD_MD, ITALICS_MD, LINK_MD);
38+
new TemplateFormatting(LINE_BREAK_MD, BOLD_MD, ITALICS_MD, CODE_MD, LINK_MD);
3739

3840
private final String lineBreak;
3941
private final String bold;
4042
private final String italics;
43+
private final String code;
4144
private final String link;
4245

43-
private TemplateFormatting(String lineBreak, String bold, String italics, String link) {
46+
private TemplateFormatting(String lineBreak, String bold, String italics, String code, String link) {
4447
this.lineBreak = lineBreak;
4548
this.bold = bold;
4649
this.italics = italics;
50+
this.code = code;
4751
this.link = link;
4852
}
4953

@@ -59,6 +63,10 @@ public String getItalics() {
5963
return italics;
6064
}
6165

66+
public String getCode() {
67+
return code;
68+
}
69+
6270
public String link() {
6371
return link;
6472
}

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/javadoc/JavadocUtilTest.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -108,17 +108,49 @@ public void convertBulletedListMarkdown() {
108108
}
109109

110110
@Test
111-
public void convertStylingAsciidoc() {
112-
String actual = "<b>bold</b>normal<i>italics</i>";
113-
String expected = "**bold**normal__italics__";
111+
public void convertBoldStylingAsciidoc() {
112+
String actual = "before<b>bold</b>normal<strong>bold2</strong>after";
113+
String expected = "before**bold**normal**bold2**after";
114114
assertThat(JavadocUtil.convertFromJavadoc(actual, TemplateFormatting.ASCIIDOC),
115115
is(expected));
116116
}
117117

118118
@Test
119-
public void convertStylingMarkdown() {
120-
String actual = "<b>bold</b>normal<i>italics</i>";
121-
String expected = "**bold**normal*italics*";
119+
public void convertItalicsStylingAsciidoc() {
120+
String actual = "before<i>italics</i>normal<em>italics2</em>after";
121+
String expected = "before__italics__normal__italics2__after";
122+
assertThat(JavadocUtil.convertFromJavadoc(actual, TemplateFormatting.ASCIIDOC),
123+
is(expected));
124+
}
125+
126+
@Test
127+
public void convertCodeStylingAsciidoc() {
128+
String actual = "before <code>code()</code> after";
129+
String expected = "before `code()` after";
130+
assertThat(JavadocUtil.convertFromJavadoc(actual, TemplateFormatting.ASCIIDOC),
131+
is(expected));
132+
}
133+
134+
@Test
135+
public void convertBoldStylingMarkdown() {
136+
String actual = "before<b>bold</b>normal<strong>bold2</strong>after";
137+
String expected = "before**bold**normal**bold2**after";
138+
assertThat(JavadocUtil.convertFromJavadoc(actual, TemplateFormatting.MARKDOWN),
139+
is(expected));
140+
}
141+
142+
@Test
143+
public void convertItalicsStylingMarkdown() {
144+
String actual = "before<i>italics</i>normal<em>italics2</em>after";
145+
String expected = "before*italics*normal*italics2*after";
146+
assertThat(JavadocUtil.convertFromJavadoc(actual, TemplateFormatting.MARKDOWN),
147+
is(expected));
148+
}
149+
150+
@Test
151+
public void convertCodeStylingMarkdown() {
152+
String actual = "before <code>code()</code> after";
153+
String expected = "before `code()` after";
122154
assertThat(JavadocUtil.convertFromJavadoc(actual, TemplateFormatting.MARKDOWN),
123155
is(expected));
124156
}

spring-auto-restdocs-dokka-json/src/main/kotlin/capital/scalable/dokka/json/JsonFormatService.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,28 @@ open class JsonOutputBuilder(
127127
private fun extractContent(content: ContentNode, topLevel: Boolean): String {
128128
when (content) {
129129
is ContentText -> return content.text
130+
is ContentEmphasis -> return wrap("<i>", "</i>", joinChildren(content))
131+
is ContentStrong -> return wrap("<b>", "</b>", joinChildren(content))
132+
is ContentCode -> return wrap("<code>", "</code>", joinChildren(content))
130133
is ContentUnorderedList -> return wrap("<ul>", "</ul>", joinChildren(content))
131134
is ContentListItem -> return listItem(content)
132135
is ContentParagraph -> return paragraph(content, topLevel)
136+
is ContentSection -> return paragraph(content, topLevel)
133137
is ContentExternalLink -> return "<a href=\"${content.href}\">${joinChildren(content)}</a>"
134138
// Ignore href of references to other code parts and just show the link text, e.g. class name.
135139
is ContentNodeLink -> return joinChildren(content)
136140
// Fallback. Some of the content types above are derived from ContentBlock and
137141
// thus this one has to be after them.
138142
is ContentBlock -> return joinChildren(content)
139143
is ContentEmpty -> return ""
144+
is ContentSymbol -> return ""
145+
is NodeRenderContent -> return ""
140146
else -> logger.warn("Unhandled content node: $content")
141147
}
142148
return ""
143149
}
144150

145-
private fun paragraph(paragraph: ContentParagraph, topLevel: Boolean): String {
151+
private fun paragraph(paragraph: ContentBlock, topLevel: Boolean): String {
146152
return if (topLevel) {
147153
// Ignore paragraphs on the top level
148154
joinChildren(paragraph)

spring-auto-restdocs-dokka-json/testdata/JavaClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Javadoc on a Java class
55
* <p>
66
* Next paragraph
7-
*
7+
* UTF 8 test: 我能吞下玻璃而不伤身体。Árvíztűrő tükörfúrógép
88
* <ul>
99
* <li>Item 1</li>
1010
* <li>Item 2</li>

spring-auto-restdocs-dokka-json/testdata/JavaClass.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"comment" : "Javadoc on a Java class<p>Next paragraph</p><ul><li>Item 1</li> <li>Item 2</li></ul>",
2+
"comment" : "Javadoc on a Java class<p>Next paragraph UTF 8 test: 我能吞下玻璃而不伤身体。Árvíztűrő tükörfúrógép</p><ul><li>Item 1</li> <li>Item 2</li></ul>",
33
"fields" : {
44
"someField" : {
55
"comment" : "A Java field",

0 commit comments

Comments
 (0)