Skip to content

Commit

Permalink
Fixed DSL guide generation to handle HTML comments in Javadoc comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
adammurdoch committed Mar 17, 2014
1 parent ec13220 commit f6ac6b4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Expand Up @@ -34,6 +34,8 @@ class BasicJavadocLexer implements JavadocLexer {
private static final Pattern TAG = Pattern.compile("(?s)\\{@.+?\\}");
private static final Pattern END_TAG_NAME = Pattern.compile("(?s)\\s|}");
private static final Pattern WHITESPACE_WITH_EOL = Pattern.compile("(?s)\\s+");
private static final String START_HTML_COMMENT = "<!--";
private static final String END_HTML_COMMENT = "-->";
private static final Map<String, String> ENTITIES = new HashMap<String, String>();

static {
Expand All @@ -56,6 +58,10 @@ public void pushText(String rawCommentText) {

public void visit(TokenVisitor visitor) {
while (!scanner.isEmpty()) {
if (scanner.lookingAt(START_HTML_COMMENT)) {
skipComment();
continue;
}
if (scanner.lookingAt(HTML_ELEMENT)) {
parseStartElement(visitor);
continue;
Expand All @@ -67,6 +73,10 @@ public void visit(TokenVisitor visitor) {

StringBuilder text = new StringBuilder();
while (!scanner.isEmpty()) {
if (scanner.lookingAt(START_HTML_COMMENT)) {
skipComment();
continue;
}
if (scanner.lookingAt(HTML_ELEMENT)) {
break;
}
Expand All @@ -88,6 +98,16 @@ public void visit(TokenVisitor visitor) {
visitor.onEnd();
}

private void skipComment() {
scanner.next(4);
while (!scanner.isEmpty() && !scanner.lookingAt(END_HTML_COMMENT)) {
scanner.next();
}
if (!scanner.isEmpty()) {
scanner.next(3);
}
}

private void parseHtmlEntity(StringBuilder buffer) {
scanner.next();
scanner.mark();
Expand Down
Expand Up @@ -100,6 +100,32 @@ class BasicJavadocLexerTest extends Specification {
0 * visitor._
}

def discardsHtmlComments() {
when:
lexer.pushText("<p><!-- ignore me --></p>text <!-- -->2")
lexer.visit(visitor)

then:
1 * visitor.onStartHtmlElement('p')
1 * visitor.onStartHtmlElementComplete('p')
1 * visitor.onEndHtmlElement('p')
1 * visitor.onText("text 2")
1 * visitor.onEnd()
0 * visitor._
}

def handlesMissingEndOfComment() {
when:
lexer.pushText("<p><!-- ignore me ")
lexer.visit(visitor)

then:
1 * visitor.onStartHtmlElement('p')
1 * visitor.onStartHtmlElementComplete('p')
1 * visitor.onEnd()
0 * visitor._
}

def parsesJavadocTags() {
when:
lexer.pushText("{@tag some value}")
Expand Down
Expand Up @@ -100,6 +100,16 @@ line 2</para>'''
format(result.docbook) == '''<para>&lt;&gt;&amp; /&gt;</para>'''
}

def ignoresHtmlComments() {
_ * classMetaData.rawCommentText >> '<!-- <p>ignore me</p> --><p>para 1'

when:
def result = parser.parse(classMetaData, listener)

then:
format(result.docbook) == '''<para>para 1</para>'''
}

def convertsPElementsToParaElements() {
_ * classMetaData.rawCommentText >> '<p>para 1</p><P>para 2</P>'

Expand Down

0 comments on commit f6ac6b4

Please sign in to comment.