示例#1
0
    def process(self, text):
        """ Simpler version of parse, turns values of text elements into INodes
        (intermediary nodes).  Results are ITextNodes that may contain more
        ITextNodes and ICommandNodes.
            :param text: string to parse.
        """
        #print('LatexFieldToINode called with "%s"' % text)
        self.math_mode = False
        self.node = None
        if not text:
            return ""
        text = text.strip()
        if not text:
            return ""
        self.feed = list(text)
        self.feed.reverse()
        nodes = []
        while self.feed:
            feed_progress = len(self.feed)
            node = self.parse_word()
            # ensure that we are not stuck in endless loop
            if len(self.feed) < feed_progress:
                nodes.append(node)
            else:
                self.feed.pop()

        if len(nodes) == 1:
            return nodes[0]
        elif nodes:
            node = ITextNode(parts=nodes)
            return node.tidy()
        else:
            return ""
示例#2
0
    def process(self, text):
        """ Simpler version of parse, turns values of text elements into INodes
        (intermediary nodes).  Results are ITextNodes that may contain more
        ITextNodes and ICommandNodes.
            :param text: string to parse.
        """
        #print('LatexFieldToINode called with "%s"' % text)
        self.math_mode = False
        self.node = None
        if not text:
            return ""
        text = text.strip()
        if not text:
            return ""
        self.feed = list(text)
        self.feed.reverse()
        nodes = []
        while self.feed:
            feed_progress = len(self.feed)
            node = self.parse_word()
            # ensure that we are not stuck in endless loop
            if len(self.feed) < feed_progress:
                nodes.append(node)
            else:
                self.feed.pop()

        if len(nodes) == 1:
            return nodes[0]
        elif nodes:
            node = ITextNode(parts=nodes)
            return node.tidy()
        else:
            return ""
示例#3
0
    def parse_word(self, end_on_space=False, return_rows=False):
        """ Turn text into ITextNodes. If something special (commands, curlybraces,
        brackets is found, deal with them by creating new Nodes of specific types
            :param feed: list of chars (strings of length 1)
        """

        node = ITextNode()
        rows = []

        while self.feed:
            c = self.feed[-1]
            if c == '{':
                new_node = self.parse_curlies()
                node.append(new_node)
            elif c == '}':
                break
            elif c == '\\':
                new_node = self.parse_command()
                if return_rows and isinstance(new_node, ICommandNode) and new_node.command == 'br':
                    # fixme: doesn't handle if some style scope continues across line break
                    rows.append(node)
                    node = ITextNode()
                else:
                    node.append(new_node)
            elif c == '$':
                self.toggle_math_mode()
            elif c in one_character_commands:
                new_node = self.parse_one_character_command()
                node.append(new_node)
            elif c.isspace() and end_on_space:
                self.feed.pop()
                break
            #elif c == self.rbracket and not self.math_mode:
            #    break
            #elif c == self.lbracket and not self.math_mode:
            #    break
            #elif c in ['&', '<', '>'] and False:
            #    self.feed.pop()
            #    node.append(html.escape(c))
            else:
                self.feed.pop()
                node.append(c)
        node = node.tidy(keep_node=False)
        if return_rows:
            if node:
                rows.append(node)
            return rows
        else:
            return node
示例#4
0
    def parse_word(self, end_on_space=False, return_rows=False):
        """ Turn text into ITextNodes. If something special (commands, curlybraces,
        brackets is found, deal with them by creating new Nodes of specific types
            :param feed: list of chars (strings of length 1)
        """

        node = ITextNode()
        rows = []

        while self.feed:
            c = self.feed[-1]
            if c == '{':
                new_node = self.parse_curlies()
                node.append(new_node)
            elif c == '}':
                break
            elif c == '\\':
                new_node = self.parse_command()
                if return_rows and isinstance(new_node, ICommandNode) and new_node.command == 'br':
                    # fixme: doesn't handle if some style scope continues across line break
                    rows.append(node)
                    node = ITextNode()
                else:
                    node.append(new_node)
            elif c == '$':
                self.toggle_math_mode()
            elif c in one_character_commands:
                new_node = self.parse_one_character_command()
                node.append(new_node)
            elif c.isspace() and end_on_space:
                self.feed.pop()
                break
            #elif c == self.rbracket and not self.math_mode:
            #    break
            #elif c == self.lbracket and not self.math_mode:
            #    break
            #elif c in ['&', '<', '>'] and False:
            #    self.feed.pop()
            #    node.append(html.escape(c))
            else:
                self.feed.pop()
                node.append(c)
        node = node.tidy(keep_node=False)
        if return_rows:
            if node:
                rows.append(node)
            return rows
        else:
            return node