Skip to content

Commit

Permalink
fix #44
Browse files Browse the repository at this point in the history
  • Loading branch information
lruiz committed Sep 27, 2013
1 parent 6fda71a commit 3497e11
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 8 deletions.
29 changes: 29 additions & 0 deletions core/src/main/java/org/tautua/markdownpapers/HtmlEmitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ public void visit(Emphasis node) {
}
}

public void visit(EmptyTag node) {
TagAttributeList attributes = node.getAttributeList();
append("<");
append(node.getName());

if(attributes != null) {
attributes.accept(this);
}
append("/>");
}

public void visit(EndTag node) {
append("</");
append(node.getName());
append(">");
}

public void visit(Header node) {
String level = String.valueOf(node.getLevel());
append("<h");
Expand Down Expand Up @@ -284,6 +301,18 @@ public void visit(Text node) {
}
}

@Override
public void visit(StartTag node) {
TagAttributeList attributes = node.getAttributeList();
append("<");
append(node.getName());

if(attributes != null) {
attributes.accept(this);
}
append(">");
}

protected void visitChildrenAndAppendSeparator(Node node, char separator){
int count = node.jjtGetNumChildren();
for(int i = 0; i < count; i++) {
Expand Down
50 changes: 50 additions & 0 deletions core/src/main/java/org/tautua/markdownpapers/ast/EmptyTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2013, TAUTUA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.tautua.markdownpapers.ast;

/**
* @author Larry Ruiz
*/
public class EmptyTag extends SimpleNode {
protected String name;

public EmptyTag(int i) {
super(i);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public TagAttributeList getAttributeList() {
for(int i = 0; i < children.length; i++) {
if(children[i] instanceof TagAttributeList) {
return (TagAttributeList) children[i];
}
}
return null;
}

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
41 changes: 41 additions & 0 deletions core/src/main/java/org/tautua/markdownpapers/ast/EndTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2013, TAUTUA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.tautua.markdownpapers.ast;

/**
* @author Larry Ruiz
*/
public class EndTag extends SimpleNode {
protected String name;

public EndTag(int i) {
super(i);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
50 changes: 50 additions & 0 deletions core/src/main/java/org/tautua/markdownpapers/ast/StartTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2013, TAUTUA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.tautua.markdownpapers.ast;

/**
* @author Larry Ruiz
*/
public class StartTag extends SimpleNode {
protected String name;

public StartTag(int i) {
super(i);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public TagAttributeList getAttributeList() {
for(int i = 0; i < children.length; i++) {
if(children[i] instanceof TagAttributeList) {
return (TagAttributeList) children[i];
}
}
return null;
}

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
6 changes: 6 additions & 0 deletions core/src/main/java/org/tautua/markdownpapers/ast/Visitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public interface Visitor {

void visit(Emphasis node);

void visit(EmptyTag node);

void visit(EndTag node);

void visit(Header node);

void visit(Image node);
Expand Down Expand Up @@ -69,4 +73,6 @@ public interface Visitor {
void visit(TagBody node);

void visit(Text node);

void visit(StartTag node);
}
20 changes: 19 additions & 1 deletion core/src/main/jjtree/Markdown.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ void Line() #Line : {} {
| LOOKAHEAD( InlineURL() ) InlineURL()
| LOOKAHEAD( Emphasis() ) Emphasis()
| LOOKAHEAD( LineBreak() <EOL> ) LineBreak()
| LOOKAHEAD( TagOpen() ) Tag()
| LOOKAHEAD( Markup() ) Markup()
| Text()
)
)+
Expand Down Expand Up @@ -917,6 +917,24 @@ void TagOpen() : {} {
"<" <CHAR_SEQUENCE> ( <DIGITS> | <CHAR_SEQUENCE> )* ( ( <SPACE> )+ TagAttribute() )* ( <SPACE> )* ( "/" )? ">"
}

void Markup() : {
StringBuilder buff = new StringBuilder();
int ch = 0;
} {
"<"
(
TagName(buff)
( LOOKAHEAD( ( <SPACE> )+ TagAttribute() ) TagAttributeList() { ch++; } )? ( <SPACE> )*
(
LOOKAHEAD(2)
"/" ">" { jjtThis.setName(buff.toString()); } #EmptyTag(ch)
|
">" { jjtThis.setName(buff.toString()); } #StartTag(ch)
)
| "/" TagName(buff) ( <SPACE> )* ">" { jjtThis.setName(buff.toString()); } #EndTag
)
}

String refname() : {
Token t;
StringBuilder buff = new StringBuilder();
Expand Down
6 changes: 5 additions & 1 deletion core/src/test/resources/others/inline.html
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<p><a href="http://wikipedia.org/wiki_(computing)" title="wikipedia">a link</a>, <strong>emp</strong>, <strong>emp</strong>, *escapedchar*</p>
<p><strong>markdown</strong> syntax parsed within <i><em>inline elements</em></i>.</p>

<div>
but not within *block elements*
</div>
7 changes: 5 additions & 2 deletions core/src/test/resources/others/inline.text
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[a link](http://wikipedia.org/wiki_(computing) "wikipedia"), **emp**, __emp__, \*escapedchar\*

**markdown** syntax parsed within <i>*inline elements*</i>.

<div>
but not within *block elements*
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public void visit(Emphasis node) {
}
}

public void visit(EmptyTag node) {
//do nothing
}

public void visit(EndTag node) {
//do nothing
}

public void visit(Header node) {
if(stack.size() == 0) {
stack.push(node);
Expand All @@ -117,7 +125,6 @@ public void visit(Image node) {
}

sink.figureGraphics(resource.getLocation(), attr);

}

public void visit(Link node) {
Expand Down Expand Up @@ -226,7 +233,6 @@ public void visit(Tag node) {
}
}

@Override
public void visit(TagAttribute node) {
sink.rawText(" ");
sink.rawText(node.getName());
Expand All @@ -235,17 +241,19 @@ public void visit(TagAttribute node) {
sink.rawText("\"");
}

@Override
public void visit(TagAttributeList node) {
node.childrenAccept(this);
}

@Override
public void visit(TagBody node) {
node.childrenAccept(this);
}

public void visit(Text node) {
sink.text(node.getValue());
}

public void visit(StartTag node) {
//do nothing
}
}

0 comments on commit 3497e11

Please sign in to comment.