@@ -68,6 +68,7 @@ class Node(StringMixIn): | |||||
from . import extras | from . import extras | ||||
from .text import Text | from .text import Text | ||||
from .argument import Argument | from .argument import Argument | ||||
from .comment import Comment | |||||
from .heading import Heading | from .heading import Heading | ||||
from .html_entity import HTMLEntity | from .html_entity import HTMLEntity | ||||
from .tag import Tag | from .tag import Tag | ||||
@@ -0,0 +1,46 @@ | |||||
# -*- coding: utf-8 -*- | |||||
# | |||||
# Copyright (C) 2012 Ben Kurtovic <ben.kurtovic@verizon.net> | |||||
# | |||||
# Permission is hereby granted, free of charge, to any person obtaining a copy | |||||
# of this software and associated documentation files (the "Software"), to deal | |||||
# in the Software without restriction, including without limitation the rights | |||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |||||
# copies of the Software, and to permit persons to whom the Software is | |||||
# furnished to do so, subject to the following conditions: | |||||
# | |||||
# The above copyright notice and this permission notice shall be included in | |||||
# all copies or substantial portions of the Software. | |||||
# | |||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |||||
# SOFTWARE. | |||||
from __future__ import unicode_literals | |||||
from . import Node | |||||
from ..compat import str | |||||
__all__ = ["Comment"] | |||||
class Comment(Node): | |||||
"""Represents a hidden HTML comment, like ``<!-- foobar -->``.""" | |||||
def __init__(self, contents): | |||||
super(Text, self).__init__() | |||||
self._contents = contents | |||||
def __unicode__(self): | |||||
return "<!--" + self.contents + "-->" | |||||
@property | |||||
def contents(self): | |||||
"""The hidden text contained between ``<!--`` and ``-->``.""" | |||||
return self._contents | |||||
@value.setter | |||||
def contents(self, value): | |||||
self._contents = str(value) |
@@ -24,7 +24,7 @@ from __future__ import unicode_literals | |||||
from . import tokens | from . import tokens | ||||
from ..compat import str | from ..compat import str | ||||
from ..nodes import Argument, Heading, HTMLEntity, Tag, Template, Text | |||||
from ..nodes import Argument, Comment, Heading, HTMLEntity, Tag, Template, Text | |||||
from ..nodes.extras import Attribute, Parameter | from ..nodes.extras import Attribute, Parameter | ||||
from ..smart_list import SmartList | from ..smart_list import SmartList | ||||
from ..wikicode import Wikicode | from ..wikicode import Wikicode | ||||
@@ -152,6 +152,17 @@ class Builder(object): | |||||
else: | else: | ||||
self._write(self._handle_token(token)) | self._write(self._handle_token(token)) | ||||
def _handle_comment(self): | |||||
"""Handle a case where a hidden comment is at the head of the tokens.""" | |||||
self._push() | |||||
while self._tokens: | |||||
token = self._tokens.pop() | |||||
if isinstance(token, tokens.CommentEnd): | |||||
contents = self._pop() | |||||
return Comment(contents) | |||||
else: | |||||
self._write(self._handle_token(token)) | |||||
def _handle_attribute(self): | def _handle_attribute(self): | ||||
"""Handle a case where a tag attribute is at the head of the tokens.""" | """Handle a case where a tag attribute is at the head of the tokens.""" | ||||
name, quoted = None, False | name, quoted = None, False | ||||
@@ -209,6 +220,8 @@ class Builder(object): | |||||
return self._handle_entity() | return self._handle_entity() | ||||
elif isinstance(token, tokens.HeadingStart): | elif isinstance(token, tokens.HeadingStart): | ||||
return self._handle_heading(token) | return self._handle_heading(token) | ||||
elif isinstance(token, tokens.CommentStart): | |||||
return self._handle_comment() | |||||
elif isinstance(token, tokens.TagOpenOpen): | elif isinstance(token, tokens.TagOpenOpen): | ||||
return self._handle_tag(token) | return self._handle_tag(token) | ||||
@@ -87,6 +87,9 @@ HTMLEntityEnd = make("HTMLEntityEnd") # ; | |||||
HeadingStart = make("HeadingStart") # =... | HeadingStart = make("HeadingStart") # =... | ||||
HeadingEnd = make("HeadingEnd") # =... | HeadingEnd = make("HeadingEnd") # =... | ||||
CommentStart = make("CommentStart") # <!-- | |||||
CommentEnd = make("CommentEnd") # --> | |||||
TagOpenOpen = make("TagOpenOpen") # < | TagOpenOpen = make("TagOpenOpen") # < | ||||
TagAttrStart = make("TagAttrStart") | TagAttrStart = make("TagAttrStart") | ||||
TagAttrEquals = make("TagAttrEquals") # = | TagAttrEquals = make("TagAttrEquals") # = | ||||