@@ -80,8 +80,8 @@ static List<Token> lex(String input) throws LexException {
8080 /**
8181 * Strips the margin from every continuation line: the {@code *} prefix (with the space after it) from lines that
8282 * have one, and the comment's own indentation from lines that do not. What remains of a bare line's indentation is
83- * relative to the comment, as on {@code *} lines after their {@code * }, so a {@code <pre>{ @code} block written
84- * without the {@code *} margin keeps its shape for {@link #deindentPreCodeBlocks}.
83+ * relative to the comment, as on {@code *} lines after their {@code * }, so a <code><pre>{ @code</code>
84+ * block written without the {@code *} margin keeps its shape for {@link #deindentPreCodeBlocks}.
8585 *
8686 * <p>The indentation stripped from bare lines is the smaller of the shortest {@code *} prefix and the shortest
8787 * indentation of a bare line, so a bare line is never cut into and lines of both kinds stay aligned with each
@@ -173,7 +173,8 @@ private List<Token> generateTokens() throws LexException {
173173 result = joinAdjacentLiteralsAndAdjacentWhitespace (result );
174174 result = inferParagraphTags (result );
175175 result = optionalizeSpacesAfterLinks (result );
176- return deindentPreCodeBlocks (result );
176+ result = deindentPreCodeBlocks (result );
177+ return removeTrailingParagraphTags (result );
177178 }
178179
179180 private Token readToken () throws LexException {
@@ -574,6 +575,35 @@ private static void deindentPreCodeBlock(List<Token> output, PeekingIterator<Tok
574575 }
575576 }
576577
578+ /**
579+ * Remove the {@code <p>} tags at the end of the comment, and the whitespace and {@code </p>} tags among them.
580+ * Nothing follows such a tag, but the writer would still put it on a line of its own after a blank one.
581+ *
582+ * <p>A {@code <p>} with attributes stays: its {@code id} may be the target of a link.
583+ */
584+ private static List <Token > removeTrailingParagraphTags (List <Token > input ) {
585+ // The last token is always END_JAVADOC, and the scan stops at BEGIN_JAVADOC at the latest.
586+ int end = input .size () - 1 ;
587+ int cut = end ;
588+ for (int i = end - 1 ; i >= 0 ; i --) {
589+ Token token = input .get (i );
590+ if (token .getType () == PARAGRAPH_OPEN_TAG
591+ && BARE_PARAGRAPH_OPEN_PATTERN .matcher (token .getValue ()).matches ()) {
592+ cut = i ;
593+ } else if (token .getType () != WHITESPACE && token .getType () != PARAGRAPH_CLOSE_TAG ) {
594+ break ;
595+ }
596+ }
597+ if (cut == end ) {
598+ return input ;
599+ }
600+ var output = new ArrayList <Token >(input .subList (0 , cut ));
601+ output .add (input .get (end ));
602+ return List .copyOf (output );
603+ }
604+
605+ private static final Pattern BARE_PARAGRAPH_OPEN_PATTERN = compile ("<p\\ s*/?\\ s*>" , CASE_INSENSITIVE );
606+
577607 private static final CharMatcher NEWLINE = CharMatcher .is ('\n' );
578608
579609 private static boolean hasMultipleNewlines (String s ) {
@@ -617,15 +647,15 @@ private static boolean hasMultipleNewlines(String s) {
617647 private static final Pattern BR_PATTERN = openTagPattern ("br" );
618648 private static final Pattern SNIPPET_TAG_OPEN_PATTERN = compile ("^[{]@snippet\\ b" );
619649 private static final Pattern INLINE_TAG_OPEN_PATTERN = compile ("^[{]@\\ w*" );
620- /*
621- * We exclude < so that we don't swallow following HTML tags. This lets us fix up " foo<p>" (~400
622- * hits in Google-internal code). We will join unnecessarily split "words" (like " foo<b>bar</b>")
623- * in a later step. There's a similar story for braces. I'm not sure I actually need to exclude @
624- * or *. TODO(cpovirk): Try removing them.
650+ /**
651+ * We exclude {@code <} so that we don't swallow following HTML tags. This lets us fix up {@code foo<p>} (~400
652+ * hits in Google-internal code). We will join unnecessarily split "words" (like {@code foo<b>bar</b>}) in a later
653+ * step. There's a similar story for braces. I'm not sure I actually need to exclude {@code @} or {@code *}.
654+ * TODO(cpovirk): Try removing them.
625655 *
626- * Thanks to the "rejoin" step in joinAdjacentLiteralsAndAdjacentWhitespace() , we could get away
627- * with matching only one character here. That would eliminate the need for the regex entirely.
628- * That might be faster or slower than what we do now.
656+ * <p> Thanks to the "rejoin" step in {@link # joinAdjacentLiteralsAndAdjacentWhitespace} , we could get away with
657+ * matching only one character here. That would eliminate the need for the regex entirely. That might be faster or
658+ * slower than what we do now.
629659 */
630660 private static final Pattern LITERAL_PATTERN = compile ("^.[^ \t \n @<{}*]*" , DOTALL );
631661
0 commit comments