Skip to content

Commit e617246

Browse files
committed
Convert Javadoc <br> tags to Markdown line breaks
1 parent 9105dbd commit e617246

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

src/main/java/org/openrewrite/java/migrate/lang/JavadocToMarkdownDocComment.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ static class JavadocToMarkdownConverter {
128128
private final List<String> lines = new ArrayList<>();
129129
private StringBuilder currentLine = new StringBuilder();
130130
private boolean inPre = false;
131+
private boolean suppressNextLineBreak = false;
131132
private final Deque<String> listStack = new ArrayDeque<>();
132133
private final Deque<Integer> listCounterStack = new ArrayDeque<>();
133134

@@ -153,11 +154,17 @@ void convert(List<Javadoc> body) {
153154
}
154155

155156
private void convertNode(Javadoc node) {
157+
// A `<br>` already ended the current line; an immediately following physical
158+
// line break is the same break, so don't let it add a spurious blank line.
159+
boolean afterBr = suppressNextLineBreak;
160+
suppressNextLineBreak = false;
156161
if (node instanceof Javadoc.Text) {
157162
currentLine.append(decodeHtmlEntities(((Javadoc.Text) node).getText()));
158163
} else if (node instanceof Javadoc.LineBreak) {
159-
lines.add(currentLine.toString());
160-
currentLine = new StringBuilder();
164+
if (!afterBr) {
165+
lines.add(currentLine.toString());
166+
currentLine = new StringBuilder();
167+
}
161168
} else if (node instanceof Javadoc.Literal) {
162169
convertLiteral((Javadoc.Literal) node);
163170
} else if (node instanceof Javadoc.Link) {
@@ -266,6 +273,15 @@ private void convertStartElement(Javadoc.StartElement element) {
266273
lines.add("");
267274
currentLine = new StringBuilder();
268275
break;
276+
case "br":
277+
// Line break: end the current line. Suppress the physical line break that
278+
// usually follows so the two collapse into a single break rather than a
279+
// blank line; a redundant break before a `<p>` is absorbed when blank lines
280+
// are collapsed.
281+
lines.add(currentLine.toString());
282+
currentLine = new StringBuilder();
283+
suppressNextLineBreak = true;
284+
break;
269285
case "em":
270286
case "i":
271287
currentLine.append('_');

src/test/java/org/openrewrite/java/migrate/lang/JavadocToMarkdownDocCommentTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,56 @@ public void m() {}
164164
);
165165
}
166166

167+
@Test
168+
void javadocWithLineBreakBeforeParagraph() {
169+
rewriteRun(
170+
java(
171+
"""
172+
public class A {
173+
/**
174+
* Verifies the value.<br>
175+
* <p>
176+
* More details.
177+
*/
178+
public void m() {}
179+
}
180+
""",
181+
"""
182+
public class A {
183+
/// Verifies the value.
184+
///
185+
/// More details.
186+
public void m() {}
187+
}
188+
"""
189+
)
190+
);
191+
}
192+
193+
@Test
194+
void javadocWithLineBreak() {
195+
rewriteRun(
196+
java(
197+
"""
198+
public class A {
199+
/**
200+
* First line.<br>
201+
* Second line.
202+
*/
203+
public void m() {}
204+
}
205+
""",
206+
"""
207+
public class A {
208+
/// First line.
209+
/// Second line.
210+
public void m() {}
211+
}
212+
"""
213+
)
214+
);
215+
}
216+
167217
@Test
168218
void javadocWithEmphasis() {
169219
rewriteRun(

0 commit comments

Comments
 (0)