Ejemplo n.º 1
0
class Command(NodeBase):
    """
    Typical zero or one argument command: \foo{bar}.

    If children do not exist then the braces are not included (e.g., \foo).
    """
    PROPERTIES = [
        Property('string', ptype=unicode),
        Property('start', ptype=str, default=''),
        Property('end', ptype=str, default=''),
        Property('options', ptype=dict, default=dict())
    ]

    def __init__(self, parent, name, *args, **kwargs):
        NodeBase.__init__(self, name=name, parent=parent, *args, **kwargs)
        if self.string is not None:
            String(self, content=self.string)

    def write(self):
        out = self.start
        out += '\\%s' % self.name
        if self.children:
            out += '{'
            for child in self.children:
                out += child.write()
            out += '}'
        out += self.end
        return out
Ejemplo n.º 2
0
class Environment(NodeBase):
    """
    Class for LaTeX environment: \\begin{foo}...\\end{foo}
    """
    PROPERTIES = [
        Property('string', ptype=unicode),
        Property('start', ptype=str, default='\n'),
        Property('end', ptype=str, default=''),
        Property('after_begin', ptype=str, default='\n'),
        Property('before_end', ptype=str, default='\n')
    ]

    def __init__(self, parent, name, *args, **kwargs):
        NodeBase.__init__(self, name=name, parent=parent, *args, **kwargs)
        if self.string is not None:
            String(self, content=self.string)

    def write(self):
        """
        Write to LaTeX string.
        """
        out = '%s\\begin{%s}%s' % (self.start, self.name, self.after_begin)
        for child in self.children:
            out += child.write()
        out += '%s\\end{%s}%s' % (self.before_end, self.name, self.end)
        return out
Ejemplo n.º 3
0
class String(NodeBase):
    """
    A node for containing string content, the parent must always be a Tag.
    """
    PROPERTIES = [
        Property('content', default=u'', ptype=unicode),
        Property('escape', default=True, ptype=bool)
    ]

    def write(self):
        """
        Write to LaTeX string.
        """
        out = escape(self.content) if self.escape else self.content
        for child in self.children:
            out += child.write()
        return out
Ejemplo n.º 4
0
class CustomCommand(Command):
    """
    Class for building up arbitrary commands, with both optional and mandatory arguments.
    """
    PROPERTIES = [
        Property('start', ptype=str, default=''),
        Property('end', ptype=str, default='')
    ]

    def write(self):
        """
        Write to LaTeX string.
        """
        out = self.start  #pylint: disable=no-member
        out += '\\%s' % self.name
        for child in self.children:
            out += child.write()
        out += self.end  #pylint: disable=no-member
        return out
Ejemplo n.º 5
0
class CustomCommand(NodeBase):
    """
    Class for building up arbitrary commands.

    Children should be Bracket or Brace objects to build up the command.
    """
    PROPERTIES = [
        Property('start', ptype=str, default=''),
        Property('end', ptype=str, default='')
    ]

    def write(self):
        """
        Write to LaTeX string.
        """
        out = self.start
        out += '\\%s' % self.name
        for child in self.children:
            out += child.write()
        out += self.end
        return out
Ejemplo n.º 6
0
class String(NodeBase):
    """
    A node for containing string content, the parent must always be a Tag.
    """
    PROPERTIES = [
        Property('content', default=u'', ptype=unicode),
        Property('escape', default=False, ptype=bool),
        Property('hide', default=False, ptype=bool)
    ]

    def __init__(self, parent=None, **kwargs):
        super(String, self).__init__(parent=parent, **kwargs)

        if (self.parent is not None) and (not isinstance(self.parent, Tag)):
            msg = "If set, the parent of he html.String '{}' must be a html.Tag object, a '{}' " \
                  " was provided."
            raise TypeError(msg.format(self.name, type(self.parent).__name__))

    def write(self):
        if self.escape:  #pylint: disable=no-member
            return cgi.escape(self.content, quote=True)  #pylint: disable=no-member
        else:
            return self.content  #pylint: disable=no-member
Ejemplo n.º 7
0
class Enclosure(NodeBase):
    """
    Class for enclosing other nodes in characters, e.g. [], {}.
    """
    PROPERTIES = [
        Property('enclose', ptype=tuple, required=True),
        Property('string', ptype=unicode)
    ]

    def __init__(self, *args, **kwargs):
        NodeBase.__init__(self, *args, **kwargs)
        if self.string is not None:  #pylint: disable=no-member
            String(self, content=self.string)  #pylint: disable=no-member

    def write(self):
        """
        Write LaTex as a string.
        """
        out = self.enclose[0]  #pylint: disable=no-member
        for child in self.children:
            out += child.write()
        out += self.enclose[1]  #pylint: disable=no-member
        return out
Ejemplo n.º 8
0
class Command(NodeBase):
    """
    Typical one argument command: \foo{bar}.
    """
    PROPERTIES = [
        Property('string', ptype=unicode),
        Property('start', ptype=str, default=''),
        Property('end', ptype=str, default=''),
        Property('options', ptype=dict, default=dict())
    ]

    def __init__(self, parent, name, *args, **kwargs):
        NodeBase.__init__(self, name=name, parent=parent, *args, **kwargs)
        if self.string is not None:  #pylint: disable=no-member
            String(self, content=self.string)  #pylint: disable=no-member

    def write(self):
        out = self.start  #pylint: disable=no-member
        out += '\\%s{' % self.name
        for child in self.children:
            out += child.write()
        out += '}' + self.end  #pylint: disable=no-member
        return out
Ejemplo n.º 9
0
class String(NodeBase):
    """
    A node for containing string content, the parent must always be a Tag.
    """
    PROPERTIES = [Property('content', default=u'', ptype=unicode)]

    def write(self):
        """
        Write to LaTeX string.
        """
        out = escape(self.content)  #pylint: disable=no-member
        for child in self.children:
            out += child.write()
        return out
Ejemplo n.º 10
0
class Environment(NodeBase):
    """
    Class for LaTeX environment: \\begin{foo}...\\end{foo}
    """
    PROPERTIES = [Property('string', ptype=unicode)]

    def __init__(self, parent, name, *args, **kwargs):
        NodeBase.__init__(self, name=name, parent=parent, *args, **kwargs)
        if self.string is not None:  #pylint: disable=no-member
            String(self, content=self.string)  #pylint: disable=no-member

    def write(self):
        """
        Write to LaTeX string.
        """
        out = '\n\\begin{%s}\n' % self.name
        for child in self.children:
            out += child.write()
        out += '\n\\end{%s}\n' % self.name
        return out
Ejemplo n.º 11
0
	def assignParent(self, parent):
		Property.assignParent(self, parent)
		self.owner.addEventListener(events.CHILD_ACCESS, self.onChildAccess)
Ejemplo n.º 12
0
	def assignParent(self, parent):
		Property.assignParent(self, parent)
		self.owner.addEventListener(Closable.TRY_OPEN, self.onTryOpen)
Ejemplo n.º 13
0
	def assignParent(self, parent):
		Property.assignParent(self, parent)
		self.owner.addEventListener(CHILD_HANDLE, self.onChildHandle)
		self.owner.addEventListener(DESCRIBE, self.onDescribe)
Ejemplo n.º 14
0
	def __init__(self, *args, **kwargs):
		Property.__init__(self, *args, **kwargs)
		self.shouldTraverse = False
Ejemplo n.º 15
0
	def assignParent(self, parent):
		Property.assignParent(self, parent)
		self.owner.addEventListener(game_events.MOVE, self.onMove)
Ejemplo n.º 16
0
 def __init__(self,*args):
     Property.__init__(self,*args)
     self.definingMorphism = self.homomorphism.get_edge_image(self.morph)
Ejemplo n.º 17
0
class Tag(NodeBase):
    """
    A node representing an HTML tag (e.g., h1, strong).
    """
    PROPERTIES = [
        Property('close', default=True, ptype=bool),
        Property('string', ptype=unicode)
    ]

    def __init__(self, parent, name, **kwargs):

        super(Tag, self).__init__(name=name, parent=parent, **kwargs)

        #pylint: disable=no-member
        if self.string:
            String(self, content=self.string)

    def addStyle(self, style):
        """
        Add to the existing style settings.
        """
        s = self.get('style', '').split(';')
        s += style.split(';')
        self['style'] = ';'.join(s)

    def addClass(self, *args):
        """
        Add to the existing class list.
        """
        c = self.get('class', '').split(' ')
        c += args
        self['class'] = ' '.join(c)

    def write(self):
        """Write the HTML as a string, e.g., <foo>...</foo>."""
        out = ''
        attr_list = []
        for key, value in self.attributes.iteritems():
            if value:  # and (key != 'class'):
                attr_list.append('{}="{}"'.format(key, str(value).strip()))

        attr = ' '.join(attr_list)
        if attr:
            out += '<{} {}>'.format(self.name, attr)
        else:
            out += '<{}>'.format(self.name)

        for child in self.children:
            out += child.write()
        if self.close:  #pylint: disable=no-member
            out += '</{}>'.format(self.name)
        return out

    def text(self):
        """
        Convert String objects into a single string.
        """
        strings = []
        for node in anytree.PreOrderIter(self):
            if isinstance(node, String) and not node.hide:
                strings.append(node.content)
        return u' '.join(strings)