|
@@ -49,17 +49,17 @@ class Builder(object): |
|
|
self._push() |
|
|
self._push() |
|
|
while self._tokens: |
|
|
while self._tokens: |
|
|
token = self._tokens.pop(0) |
|
|
token = self._tokens.pop(0) |
|
|
if isinstance(token, tokens.TEMPLATE_PARAM_EQUALS): |
|
|
|
|
|
|
|
|
if isinstance(token, tokens.TemplateParamEquals): |
|
|
key = self._pop() |
|
|
key = self._pop() |
|
|
showkey = True |
|
|
showkey = True |
|
|
self._push() |
|
|
self._push() |
|
|
elif isinstance(token, (tokens.TEMPLATE_PARAM_SEPARATOR, |
|
|
|
|
|
tokens.TEMPLATE_CLOSE)): |
|
|
|
|
|
|
|
|
elif isinstance(token, (tokens.TemplateParamSeparator, |
|
|
|
|
|
tokens.TemplateClose)): |
|
|
self._tokens.insert(0, token) |
|
|
self._tokens.insert(0, token) |
|
|
value = self._pop() |
|
|
value = self._pop() |
|
|
return Parameter(key, value, showkey) |
|
|
return Parameter(key, value, showkey) |
|
|
else: |
|
|
else: |
|
|
self._stack.write(self._handle_token()) |
|
|
|
|
|
|
|
|
self._write(self._handle_token()) |
|
|
|
|
|
|
|
|
def _handle_template(self): |
|
|
def _handle_template(self): |
|
|
params = [] |
|
|
params = [] |
|
@@ -68,7 +68,7 @@ class Builder(object): |
|
|
self._push() |
|
|
self._push() |
|
|
while self._tokens: |
|
|
while self._tokens: |
|
|
token = self._tokens.pop(0) |
|
|
token = self._tokens.pop(0) |
|
|
if isinstance(token, tokens.TEMPLATE_PARAM_SEPARATOR): |
|
|
|
|
|
|
|
|
if isinstance(token, tokens.TemplateParamSeparator): |
|
|
if not params: |
|
|
if not params: |
|
|
name = self._pop() |
|
|
name = self._pop() |
|
|
param = self._handle_parameter(min(int_key_range - int_keys)) |
|
|
param = self._handle_parameter(min(int_key_range - int_keys)) |
|
@@ -76,18 +76,18 @@ class Builder(object): |
|
|
int_keys.add(int(param.name)) |
|
|
int_keys.add(int(param.name)) |
|
|
int_key_range.add(len(int_keys) + 1) |
|
|
int_key_range.add(len(int_keys) + 1) |
|
|
params.append(param) |
|
|
params.append(param) |
|
|
elif isinstance(token, tokens.TEMPLATE_CLOSE): |
|
|
|
|
|
|
|
|
elif isinstance(token, tokens.TemplateClose): |
|
|
if not params: |
|
|
if not params: |
|
|
name = self._pop() |
|
|
name = self._pop() |
|
|
return Template(name, params) |
|
|
return Template(name, params) |
|
|
else: |
|
|
else: |
|
|
self._stack.write(self._handle_token()) |
|
|
|
|
|
|
|
|
self._write(self._handle_token()) |
|
|
|
|
|
|
|
|
def _handle_entity(self): |
|
|
def _handle_entity(self): |
|
|
token = self._tokens.pop(0) |
|
|
token = self._tokens.pop(0) |
|
|
if isinstance(token, tokens.HTML_ENTITY_NUMERIC): |
|
|
|
|
|
|
|
|
if isinstance(token, tokens.HTMLEntityNumeric): |
|
|
token = self._tokens.pop(0) |
|
|
token = self._tokens.pop(0) |
|
|
if isinstance(token, tokens.HTML_ENTITY_HEX): |
|
|
|
|
|
|
|
|
if isinstance(token, tokens.HTMLEntityHex): |
|
|
token = self._tokens.pop(0) |
|
|
token = self._tokens.pop(0) |
|
|
return HTMLEntity(token.text, named=False, hexadecimal=True) |
|
|
return HTMLEntity(token.text, named=False, hexadecimal=True) |
|
|
return HTMLEntity(token.text, named=False, hexadecimal=False) |
|
|
return HTMLEntity(token.text, named=False, hexadecimal=False) |
|
@@ -98,30 +98,30 @@ class Builder(object): |
|
|
self._push() |
|
|
self._push() |
|
|
while self._tokens: |
|
|
while self._tokens: |
|
|
token = self._tokens.pop(0) |
|
|
token = self._tokens.pop(0) |
|
|
if isinstance(token, tokens.HEADING_BLOCK): |
|
|
|
|
|
|
|
|
if isinstance(token, tokens.HeadingBlock): |
|
|
title = self._pop() |
|
|
title = self._pop() |
|
|
return Heading(title, level) |
|
|
return Heading(title, level) |
|
|
else: |
|
|
else: |
|
|
self._stack.write(self._handle_token()) |
|
|
|
|
|
|
|
|
self._write(self._handle_token()) |
|
|
|
|
|
|
|
|
def _handle_attribute(self): |
|
|
def _handle_attribute(self): |
|
|
name, quoted = None, False |
|
|
name, quoted = None, False |
|
|
self._push() |
|
|
self._push() |
|
|
while self._tokens: |
|
|
while self._tokens: |
|
|
token = self._tokens.pop(0) |
|
|
token = self._tokens.pop(0) |
|
|
if isinstance(token, tokens.TAG_ATTR_EQUALS): |
|
|
|
|
|
|
|
|
if isinstance(token, tokens.TagAttrEquals): |
|
|
name = self._pop() |
|
|
name = self._pop() |
|
|
self._push() |
|
|
self._push() |
|
|
elif isinstance(token, tokens.TAG_ATTR_QUOTE): |
|
|
|
|
|
|
|
|
elif isinstance(token, tokens.TagAttrQuote): |
|
|
quoted = True |
|
|
quoted = True |
|
|
elif isinstance(token, (tokens.TAG_ATTR_START, |
|
|
|
|
|
tokens.TAG_CLOSE_OPEN)): |
|
|
|
|
|
|
|
|
elif isinstance(token, (tokens.TagAttrStart, |
|
|
|
|
|
tokens.TagCloseOpen)): |
|
|
self._tokens.insert(0, token) |
|
|
self._tokens.insert(0, token) |
|
|
if name is not None: |
|
|
if name is not None: |
|
|
return Attribute(name, self._pop(), quoted) |
|
|
return Attribute(name, self._pop(), quoted) |
|
|
return Attribute(self._pop(), quoted=quoted) |
|
|
return Attribute(self._pop(), quoted=quoted) |
|
|
else: |
|
|
else: |
|
|
self._stack.write(self._handle_token()) |
|
|
|
|
|
|
|
|
self._write(self._handle_token()) |
|
|
|
|
|
|
|
|
def _handle_tag(self, token): |
|
|
def _handle_tag(self, token): |
|
|
type_, showtag = token.type, token.showtag |
|
|
type_, showtag = token.type, token.showtag |
|
@@ -129,40 +129,40 @@ class Builder(object): |
|
|
self._push() |
|
|
self._push() |
|
|
while self._tokens: |
|
|
while self._tokens: |
|
|
token = self._tokens.pop(0) |
|
|
token = self._tokens.pop(0) |
|
|
if isinstance(token, tokens.TAG_ATTR_START): |
|
|
|
|
|
|
|
|
if isinstance(token, tokens.TagAttrStart): |
|
|
attrs.append(self._handle_attribute()) |
|
|
attrs.append(self._handle_attribute()) |
|
|
elif isinstance(token, tokens.TAG_CLOSE_OPEN): |
|
|
|
|
|
|
|
|
elif isinstance(token, tokens.TagCloseOpen): |
|
|
open_pad = token.padding |
|
|
open_pad = token.padding |
|
|
tag = self._pop() |
|
|
tag = self._pop() |
|
|
self._push() |
|
|
self._push() |
|
|
elif isinstance(token, tokens.TAG_CLOSE_SELFCLOSE): |
|
|
|
|
|
|
|
|
elif isinstance(token, tokens.TagCloseSelfclose): |
|
|
tag = self._pop() |
|
|
tag = self._pop() |
|
|
return Tag(type_, tag, attrs=attrs, showtag=showtag, |
|
|
return Tag(type_, tag, attrs=attrs, showtag=showtag, |
|
|
self_closing=True, open_padding=token.padding) |
|
|
self_closing=True, open_padding=token.padding) |
|
|
elif isinstance(token, tokens.TAG_OPEN_CLOSE): |
|
|
|
|
|
|
|
|
elif isinstance(token, tokens.TagOpenClose): |
|
|
contents = self._pop() |
|
|
contents = self._pop() |
|
|
elif isinstance(token, tokens.TAG_CLOSE_CLOSE): |
|
|
|
|
|
|
|
|
elif isinstance(token, tokens.TagCloseClose): |
|
|
return Tag(type_, tag, contents, attrs, showtag, False, |
|
|
return Tag(type_, tag, contents, attrs, showtag, False, |
|
|
open_pad, token.padding) |
|
|
open_pad, token.padding) |
|
|
else: |
|
|
else: |
|
|
self._stack.write(self._handle_token()) |
|
|
|
|
|
|
|
|
self._write(self._handle_token()) |
|
|
|
|
|
|
|
|
def _handle_token(self): |
|
|
def _handle_token(self): |
|
|
token = self._tokens.pop(0) |
|
|
token = self._tokens.pop(0) |
|
|
if isinstance(token, tokens.TEXT): |
|
|
|
|
|
|
|
|
if isinstance(token, tokens.Text): |
|
|
return Text(token.text) |
|
|
return Text(token.text) |
|
|
elif isinstance(token, tokens.TEMPLATE_OPEN): |
|
|
|
|
|
|
|
|
elif isinstance(token, tokens.TemplateOpen): |
|
|
return self._handle_template() |
|
|
return self._handle_template() |
|
|
elif isinstance(token, tokens.HTML_ENTITY_START): |
|
|
|
|
|
|
|
|
elif isinstance(token, tokens.HTMLEntityStart): |
|
|
return self._handle_entity() |
|
|
return self._handle_entity() |
|
|
elif isinstance(token, tokens.HEADING_BLOCK): |
|
|
|
|
|
|
|
|
elif isinstance(token, tokens.HeadingBlock): |
|
|
return self._handle_heading(token) |
|
|
return self._handle_heading(token) |
|
|
elif isinstance(token, tokens.TAG_OPEN_OPEN): |
|
|
|
|
|
|
|
|
elif isinstance(token, tokens.TagOpenOpen): |
|
|
return self._handle_tag(token) |
|
|
return self._handle_tag(token) |
|
|
|
|
|
|
|
|
def build(self, tokenlist): |
|
|
def build(self, tokenlist): |
|
|
self._tokens = tokenlist |
|
|
self._tokens = tokenlist |
|
|
self._push() |
|
|
self._push() |
|
|
while self._tokens: |
|
|
while self._tokens: |
|
|
self._stack.write(self._handle_token()) |
|
|
|
|
|
|
|
|
self._write(self._handle_token()) |
|
|
return self._pop() |
|
|
return self._pop() |