Skip to content

Commit

Permalink
EOF will now close all open tags when using the : form.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicerobot committed Apr 18, 2011
1 parent 7a11ba6 commit fdabc81
Show file tree
Hide file tree
Showing 12 changed files with 873 additions and 678 deletions.
22 changes: 12 additions & 10 deletions struxt/README.md
Expand Up @@ -38,8 +38,7 @@ Not only is the Struxt far less verbose, it's simply easier to read.
to: "Tove".
from: "Jani".
heading: "Reminder".
body: "Don't forget me this weekend!".
.
body: "Don't forget me this weekend!"

### Alternately, {} can replace :. ###

Expand Down Expand Up @@ -76,12 +75,15 @@ equivalently produce:

### HTML Example ###

html{
html:
head{title:"My Struxt".}
body{
a href "/nicerobot/text-plain/tree/master/struxt":"Struxt".
}
}
body:
a "/nicerobot/text-plain/tree/master/struxt":"Struxt".

This example demonstrates a couple points.

1. When using `tag:` syntax, EOF will close all open tags.
1. Certain tags have default attribute names. e.g. `a` tag's default attribute name is `href`.

### HTML Output ###

Expand Down Expand Up @@ -178,7 +180,7 @@ Produces [this Metal Control](/nicerobot/metal/blob/master/metal-common/src/test

### For [this XML](/nicerobot/text-plain/tree/master/language/src/test/resources/ical.xml) ###

<?xml version="1.0"?>
<?xml version="1.1"?>
<iCalendar xmlns="urn:ietf:params:xml:ns:xcal">
<vcalendar>
<version>2.0</version>
Expand Down Expand Up @@ -221,8 +223,8 @@ Produces [this Metal Control](/nicerobot/metal/blob/master/metal-common/src/test
### The Struxt ###

stylesheet/xsl 2.0 version,
"http://www.w3.org/1999/xhtml" xhtml/xmlns,
"http://www.w3.org/1999/XSL/Transform" xsl/xmlns
"http://www.w3.org/1999/xhtml" xhtml@xmlns,
"http://www.w3.org/1999/XSL/Transform" xsl@xmlns
{
template/xsl match "*": "[" value-of/xsl select ".". "]".
}
Expand Down
15 changes: 15 additions & 0 deletions struxt/samples/cdata.struxt
@@ -0,0 +1,15 @@
cdata-test {
[["

this'll be verbatim.

"]]

xmldecl {
one XML.
execute {[["addPI("xml");"]]}
one attributes.
one '.'.
execute {[["closeNode();"]]}
}
}
5 changes: 5 additions & 0 deletions struxt/samples/doctype.struxt
@@ -0,0 +1,5 @@
doctype {
!!!
whatever.
!!!
}
1 change: 1 addition & 0 deletions struxt/samples/eof1.struxt
@@ -0,0 +1 @@
html:body:
2 changes: 2 additions & 0 deletions struxt/samples/eof2.struxt
@@ -0,0 +1,2 @@
html:body:
"struxt stream test"
3 changes: 3 additions & 0 deletions struxt/samples/eof3.struxt
@@ -0,0 +1,3 @@
html:body:
"struxt stream test"
a "/nicerobot/text-plain":
4 changes: 4 additions & 0 deletions struxt/samples/eof4.struxt
@@ -0,0 +1,4 @@
html:body:
"struxt stream test"
a "/nicerobot/text-plain":
"text-plain".
4 changes: 4 additions & 0 deletions struxt/samples/eof5.struxt
@@ -0,0 +1,4 @@
html:head:title{"eof test"}.body:
"struxt stream test"
a "/nicerobot/text-plain":
"text-plain".
7 changes: 7 additions & 0 deletions struxt/samples/eof6.struxt
@@ -0,0 +1,7 @@
html:head:title{"eof test"}.body:
"struxt stream test"
a "/nicerobot/text-plain":
"text-plain".
div:
span:"streams".
img "logo.png".
34 changes: 29 additions & 5 deletions struxt/struxt.parsers/src/main/antlr/Struxt.g
Expand Up @@ -30,6 +30,7 @@ import org.jdom.Comment;
import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.CDATA;
import org.jdom.Namespace;
import org.jdom.ProcessingInstruction;
import org.jdom.Text;
Expand Down Expand Up @@ -75,6 +76,12 @@ import org.jdom.output.XMLOutputter;
return node;
}

public Content addCDATA (String text) {
final Content node = getCurrent();
((Element)node).addContent(new CDATA(unquote(text)));
return node;
}

public Content addNode (Token ns, Token name) {
final String s = null!=ns?ns.getText():"";
final String n = name.getText();
Expand Down Expand Up @@ -120,10 +127,21 @@ import org.jdom.output.XMLOutputter;
// user-customizable and namespace sensitive.
if ("a".equals(na)) {
n = "href";
} else if ("control".equals(na)
|| "input".equals(na)
|| "param".equals(na)) {
n = "name";
} else if ("img".equals(na)
|| "frame".equals(na)
|| "iframe".equals(na)
|| "script".equals(na)) {
n = "src";
} else if ("option".equals(na)) {
n = "value";
} else if ("form".equals(na)) {
n = "action";
} else if ("value-of".equals(na)) {
n = "select";
} else if ("control".equals(na)) {
n = "name";
} else {
// TODO: provide support to add multiple unqualified values.
// That is, a "b", "c", "d". => <a id="b" id1="c" id2="d"/>
Expand Down Expand Up @@ -195,8 +213,9 @@ import org.jdom.output.XMLOutputter;
public static String unquote(String s) {
int q=0;
if (s.startsWith("'''")) q+=3;
if (s.startsWith("!!!")) q+=3;
if (s.startsWith("\"\"\"")) q+=2;
else if (s.startsWith("[[\"")) q+=3;
else if (s.startsWith("!!!")) q+=3;
else if (s.startsWith("\"\"\"")) q+=2;
if (s.startsWith("\"")) q+=1;
if (0==q) return s;
return s.substring(q,s.length()-q);
Expand All @@ -221,6 +240,7 @@ xmldecl
node
: tag children {closeNode();}
| text=value {addText($text);}
| cdata=CDATA {addCDATA($cdata.text);}
;

tag
Expand All @@ -229,7 +249,7 @@ tag

fragment children
: '{' childs '}'
| (':' node*)? ('.'|';')
| (':' node*)? ('.'|';'|EOF)
;

fragment childs
Expand Down Expand Up @@ -286,6 +306,10 @@ DOC
: '!!!' (options {greedy=false;}:.)+ '!!!'
;

CDATA
: '[["' (options {greedy=false;}:.)+ '"]]'
;

STR
: '"""' (options {greedy=false;}:.)* '"""'
| '\'\'\'' (options {greedy=false;}:.)* '\'\'\''
Expand Down

0 comments on commit fdabc81

Please sign in to comment.