Esempio n. 1
0
def create_header(
    title_str, doc, level=0, parse_text=False, identifier="", short_title=None
):
    """
    Create a header element.

    Because headers need to be referenced by later elements, references to the
    last found header is remembered.
    """
    if not isinstance(doc, pf.Doc):
        raise ValueError("create_header without Doc element")

    attributes = {}
    if short_title == title_str:
        short_title = None
    if short_title is not None:
        # Attach short title as attribute to be picked up later on by generate_innodoc
        attributes["short_title"] = short_title

    if parse_text:
        title = parse_fragment(title_str, doc.metadata["lang"].text)[0].content
    else:
        title = destringify(title_str)
    header = pf.Header(
        *title, level=level, identifier=identifier, attributes=attributes
    )
    remember(doc, "label", header)
    return header
Esempio n. 2
0
    def handle_mextlink(self, cmd_args, elem):
        r"""Handle ``\MExtLink`` command.

        This command inserts an external link.
        """
        url = cmd_args[0]
        text = destringify(cmd_args[1])
        link = pf.Link(*text, url=url)
        return block_wrap(link, elem)
Esempio n. 3
0
    def handle_msref(self, cmd_args, elem):
        r"""Handle ``\MSRef`` command.

        This command inserts a fragment-style link.
        """
        url = "#%s" % cmd_args[0]
        description = destringify(cmd_args[1])
        return block_wrap(
            pf.Link(*description, url=url, attributes={"data-msref": "true"}),
            elem,
        )
Esempio n. 4
0
    def _unknown_environment_debug(env_name, elem):
        """Handle unknown latex environment.

        Output visual feedback about the unknown environment.
        """
        classes = ELEMENT_CLASSES["DEBUG_UNKNOWN_ENV"] + [slugify(env_name)]
        div = pf.Div(classes=classes)
        div.content.extend([
            pf.Para(pf.Strong(*destringify("Unhandled environment:"))),
            pf.CodeBlock(elem.text),
        ])
        return div
Esempio n. 5
0
    def handle_myoutubevideo(self, cmd_args, elem):
        r"""Handle ``\MYoutubeVideo``.

        Just return a Link Element.
        """
        title, _, _, url = cmd_args
        link = pf.Link(
            *destringify(title),
            url=url,
            title=title,
            classes=ELEMENT_CLASSES["MYOUTUBE_VIDEO"],
        )
        return block_wrap(link, elem)
Esempio n. 6
0
    def _unknown_command_debug(cmd_name, elem):
        """Handle unknown latex commands.

        Output visual feedback about the unknown command.
        """
        classes = ELEMENT_CLASSES["DEBUG_UNKNOWN_CMD"] + [slugify(cmd_name)]
        msg_prefix = pf.Strong(*destringify("Unhandled command:"))
        if isinstance(elem, pf.Block):
            div = pf.Div(classes=classes)
            div.content.extend([pf.Para(msg_prefix), pf.CodeBlock(elem.text)])
            return div
        # RawInline
        span = pf.Span(classes=classes)
        span.content.extend([msg_prefix, pf.Space(), pf.Code(elem.text)])
        return span
Esempio n. 7
0
    def handle_mvideo(self, cmd_args, elem):
        r"""Handle ``\MVideo``.

        Just return a Link Element.
        """
        filename = "{}.mp4".format(cmd_args[0])
        title = cmd_args[1]
        link = pf.Link(
            *destringify(title),
            url=filename,
            title=title,
            classes=ELEMENT_CLASSES["MVIDEO"],
        )
        remember(elem.doc, "label", link)
        return block_wrap(link, elem)
Esempio n. 8
0
 def test_regular(self):
     """Test destringify with a regular string"""
     string = "This is a  really\tnice    string."
     comp = [
         pf.Str("This"),
         pf.Space(),
         pf.Str("is"),
         pf.Space(),
         pf.Str("a"),
         pf.Space(),
         pf.Str("really"),
         pf.Space(),
         pf.Str("nice"),
         pf.Space(),
         pf.Str("string."),
     ]
     ret = destringify(string)
     self._compare_list(ret, comp)
Esempio n. 9
0
 def test_whitespace(self):
     """Test destringify with leading and trailing whitespace"""
     string = "  foo bar  "
     comp = [pf.Str("foo"), pf.Space(), pf.Str("bar")]
     ret = destringify(string)
     self._compare_list(ret, comp)
Esempio n. 10
0
 def test_one_word(self):
     """Test destringify with one word"""
     string = "foobar"
     ret = destringify(string)
     self._compare_list(ret, [pf.Str("foobar")])
Esempio n. 11
0
 def test_empty_whitespace(self):
     """Test destringify with an whitespace string"""
     string = "   "
     ret = destringify(string)
     self.assertListEqual(ret, [])