Permalink
Browse files

Progress.

  • Loading branch information...
1 parent c2a81e4 commit 5d22ac9576c1723299fa5374048511792f98976f @NSinopoli committed Nov 21, 2011
Showing with 64 additions and 15 deletions.
  1. +41 −13 nephrite/core/parser.py
  2. +23 −2 nephrite/test/parser.py
View
@@ -2,7 +2,7 @@
import string
from pyparsing import Combine, Literal, oneOf, OneOrMore, Optional, Word, \
ZeroOrMore, printables, Suppress, restOfLine, LineStart, \
- alphanums, alphas
+ alphanums, alphas, nestedExpr, empty, White, CharsNotIn
grammar = '''
<line> :: <tag> | <comment> | <doctype> | <code> | <include> | <mixin>
@@ -44,9 +44,7 @@
<variable> :: "$" ( <alphanum> | "_" )
<escape-interpolation> :: "\" <output-interpolation>
-<text-only> :: ( <text-only-tags> [ <attribute> ]
- | <element> [ <attribute> ] "." ) <indent> <text>
-<text-only-tags> :: "script" | "style"
+<text-only> :: <element> [ <attribute> ] "." <indent> <text>
<indent> :: "\n" <tab>
<tab> :: "\t" | " "
@@ -102,10 +100,17 @@ def detect_token(jade):
+ Optional(element_id).setResultsName('element_id'))
selectors.setParseAction(parse_selectors)
- element = selectors | (Word(alphas) + Optional(selectors))
+ element = selectors.setResultsName('selectors') \
+ | (Word(alphas).setResultsName('element_name') \
+ + Optional(selectors).setResultsName('selectors'))
+ element.setParseAction(parse_element)
- tag = element
+ attribute = CharsNotIn('('+')')
+ attributes = nestedExpr(content=attribute)
+ tag = element.setResultsName('element') \
+ + Optional(attributes).setResultsName('attributes')
+ tag.setParseAction(parse_tag)
# TODO: block-comment and conditional-comment
@@ -121,11 +126,9 @@ def detect_token(jade):
parsed = source.parseString(jade)
-
return ' '.join(parsed)
-
'''
char = Word(printables)
variable = Literal('$') + Word(alphanums + '_')
@@ -193,12 +196,12 @@ def detect_token(jade):
return ' '.join(parsed)
'''
-def parse_buffered_comment(orig, loc, toks):
- return '<!--' + toks[0] + ' -->'
-
def parse_attribute(orig, loc, toks):
return ' '.join(toks[0])
+def parse_buffered_comment(orig, loc, toks):
+ return '<!--' + toks[0] + ' -->'
+
def parse_doctype(orig, loc, toks):
results = ' '.join(toks)
@@ -222,6 +225,25 @@ def parse_doctype(orig, loc, toks):
doctype = right.strip().lower()
return doctypes.get(doctype, doctypes['default'])
+def parse_element(orig, loc, toks):
+ self_closing = ['meta', 'img', 'link', 'input', 'area', 'base', 'col', 'br', 'hr']
+
+ if 'element_name' in toks:
+ element = toks['element_name']
+ else:
+ element = 'div'
+
+ selectors = ''
+ if 'selectors' in toks:
+ selectors = ' ' + toks['selectors']
+
+ if element in self_closing:
+ element = element = '<' + element + selectors + '{ATTRIBUTES} />'
+ else:
+ element = '<' + element + selectors + '{ATTRIBUTES}>' + '</' + element + '>'
+
+ return element
+
def parse_include(orig, loc, toks):
include = ''.join(toks) + '.jade'
if not os.path.isfile(include):
@@ -239,5 +261,11 @@ def parse_selectors(orig, loc, toks):
return selectors.rstrip()
def parse_tag(orig, loc, toks):
- #print(results)
- return toks;
+ tag = toks['element']
+ attributes = ''
+ if 'attributes' in toks:
+ attributes = ' ' + ''.join(toks['attributes'][0])
+
+ tag = tag.replace('{ATTRIBUTES}', attributes)
+
+ return tag
View
@@ -10,9 +10,11 @@ class TestParser(unittest.TestCase):
def test_doctypes(self):
self.assertEqual('<?xml version="1.0" encoding="utf-8" ?>', parser.parse('!!! xml'));
self.assertEqual('<!DOCTYPE html>', parser.parse('doctype html'));
- self.assertEqual('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">', parser.parse('doctype BaSiC'));
+ self.assertEqual('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"' \
+ + ' "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">', parser.parse('doctype BaSiC'));
self.assertEqual('<!DOCTYPE html>', parser.parse('!!! 5'));
- self.assertEqual('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">', parser.parse('!!!'));
+ self.assertEqual('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' \
+ + ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">', parser.parse('!!!'));
self.assertEqual('<!DOCTYPE html>', parser.parse('!!! html'));
def test_buffered_comment(self):
@@ -33,6 +35,25 @@ def test_tag(self):
self.assertEqual('<div id="foo" class="bar baz"></div>', parser.parse('.bar.baz#foo'));
self.assertEqual('<div class="bar"></div>', parser.parse('.bar'));
+ self.assertEqual('<a class="bar"></a>', parser.parse('a.bar'));
+ self.assertEqual('<p id="foo" class="bar"></p>', parser.parse('p#foo.bar'));
+ self.assertEqual('<li id="foo" class="bar"></li>', parser.parse('li#foo.bar'));
+ self.assertEqual('<ul id="foo" class="bar baz"></ul>', parser.parse('ul.bar.baz#foo'));
+
+ self.assertEqual('<a id="foo" class="bar baz" href="/login" alt="Log in"></a>', \
+ parser.parse('a.bar.baz#foo (href="/login" alt="Log in")'));
+ self.assertEqual('<a href="/login" alt="Log in"></a>', \
+ parser.parse('a(href="/login" alt="Log in")'));
+
+ self.assertEqual('<input id="tos" type="checkbox" checked name="tos" />', \
+ parser.parse('input#tos(type="checkbox" checked name="tos")'));
+
+ self.assertEqual('<img class="img large" src="test.jpg" />', \
+ parser.parse('img.img.large(src="test.jpg")'));
+
+ self.assertEqual('<br />', \
+ parser.parse('br'));
+
# def test_line_endings(self):
# jade = '\n'.join(['p', 'div', 'img']);
# html = ''.join(['<p></p>','<div></div>','<img/>']);

0 comments on commit 5d22ac9

Please sign in to comment.