Ejemplo n.º 1
0
    def _parse_attributes(self):
        self.get_token(TokenTypes.LITERAL, "[")
        attributes = self.get_token(TokenTypes.TEXT).value
        self.get_token(TokenTypes.LITERAL, "]")

        p = analyse(
            PreprocessVariablesParser(self.variables),
            attributes,
        )

        attributes = p.nodes[0].value

        p = analyse(ArgumentsParser(), attributes)

        self._push_attributes(p.args, p.kwargs)
Ejemplo n.º 2
0
    def parse_macro(self):
        self.get_token(TokenTypes.LITERAL, "[")
        macro_name = self.get_token_value(TokenTypes.TEXT)
        self.get_token(TokenTypes.LITERAL, "]")
        self.get_token(TokenTypes.LITERAL, "(")

        raw = False
        if macro_name == "footnote":
            raw = True

        arguments = self.collect_join(
            stop_tokens=[Token(TokenTypes.LITERAL, ")"), Token(TokenTypes.EOL)],
        )
        p = analyse(ArgumentsParser(raw=raw), arguments)

        self.get_token(TokenTypes.LITERAL, ")")

        if macro_name == "link":
            return self.parse_macro_link(args=p.args, kwargs=p.kwargs)
        if macro_name == "mailto":
            return self.parse_macro_mailto(args=p.args, kwargs=p.kwargs)
        elif macro_name == "image":
            return self.parse_macro_image(args=p.args, kwargs=p.kwargs)
        elif macro_name == "footnote":
            return self.parse_macro_footnote(args=p.args, kwargs=p.kwargs)

        return MacroNode(macro_name, args=p.args, kwargs=p.kwargs)
Ejemplo n.º 3
0
    def _parse_standard_block(self, blocktype, content, secondary_content,
                              title, args, kwargs):
        pc = analyse(MainParser(variables=self.variables), "\n".join(content))
        ps = analyse(MainParser(variables=self.variables),
                     "\n".join(secondary_content))

        self._add_footnotes(pc.footnotes)

        self._save(
            BlockNode(
                blocktype=blocktype,
                content=pc.nodes,
                secondary_content=ps.nodes,
                args=args,
                kwargs=kwargs,
                title=title,
            ))
Ejemplo n.º 4
0
    def _parse_quote_block(self, content, title, args, kwargs):
        _, kwargs = merge_args(args, kwargs, ["attribution"])

        p = analyse(MainParser(), "\n".join(content))

        self._save(
            QuoteNode(
                kwargs.pop("attribution"),
                content=p.nodes,
                kwargs=kwargs,
            ))
Ejemplo n.º 5
0
    def _parse_text_content(self, text):
        if text is None:
            return None

        p = analyse(
            PreprocessVariablesParser(self.variables),
            text,
        )

        text = p.nodes[0].value

        p = analyse(TextParser(footnotes_start_with=len(self.footnotes) + 1),
                    text)

        # Text should return a single sentence node
        result = p.nodes[0]

        if len(p.footnotes) > 0:
            self._add_footnotes(p.footnotes)

        return result
Ejemplo n.º 6
0
    def parse_macro_footnote(self, args, kwargs):
        args, kwargs = merge_args(args, kwargs, ["content"])
        content_text = kwargs.get("content")
        refanchor, defanchor = footnote_anchors(content_text)
        number = self.footnotes_start_with + len(self.footnotes)

        p = analyse(TextParser(), content_text)

        self.footnotes.append(
            FootnoteDefNode(
                refanchor=refanchor, defanchor=defanchor, number=number, content=p.nodes
            )
        )
        return FootnoteRefNode(refanchor=refanchor, defanchor=defanchor, number=number)
Ejemplo n.º 7
0
    def _parse_conditional_block(self, condition, content, args, kwargs):
        _, kwargs = merge_args(args, kwargs, ["variable", "value"])

        match = self.variables.get(kwargs["variable"]) == kwargs.get(
            "value", True)
        test = True if condition == "if" else False

        if match is test:
            p = analyse(MainParser(variables=self.variables),
                        "\n".join(content))

            self._add_footnotes(p.footnotes)

            self.nodes.extend(p.nodes)
Ejemplo n.º 8
0
    def _parse_title(self):
        self.get_token(TokenTypes.LITERAL, ".")

        with self:
            self.get_token(TokenTypes.WHITESPACE)

        text = self.get_token(TokenTypes.TEXT).value
        self.get_token(TokenTypes.EOL)

        p = analyse(TextParser(footnotes_start_with=len(self.footnotes) + 1),
                    text)
        title = p.nodes[0]

        self._push_title(title)
Ejemplo n.º 9
0
    def _parse_command(self):
        self.get_token(TokenTypes.LITERAL, "::")
        name = self.get_token(TokenTypes.TEXT).value
        self.get_token(TokenTypes.LITERAL, ":")

        args = None
        kwargs = None

        with self:
            arguments = self.get_token(TokenTypes.TEXT).value
            p = analyse(ArgumentsParser(), arguments)
            args = p.args
            kwargs = p.kwargs

        self._save(CommandNode(name=name, args=args, kwargs=kwargs))
Ejemplo n.º 10
0
    def _parse_admonition_block(self, content, args, kwargs):
        _, kwargs = merge_args(args, kwargs, ["class", "icon", "label"])

        p = analyse(MainParser(variables=self.variables), "\n".join(content))

        self._add_footnotes(p.footnotes)

        self._save(
            AdmonitionNode(
                admclass=kwargs.pop("class"),
                icon=kwargs.pop("icon"),
                label=kwargs.pop("label"),
                content=p.nodes,
                kwargs=kwargs,
            ))