Ejemplo n.º 1
0
    def handle_proof(self, tag: QqTag) -> str:
        """
        Uses tags: proof, label, outline, of

        Examples:

            \proof
                Here is the proof

            \proof \of theorem \ref{thm:1}
                Now we pass to proof of theorem \ref{thm:1}

        :param tag:
        :return: HTML of proof
        """
        doc, html, text = Doc().tagtext()
        with html("div", klass="env env__proof"):
            if tag.find("label"):
                doc.attr(id=self.label2id(tag.label_.value))
            with html("span", klass="env-title env-title__proof"):
                if tag.exists("outline"):
                    proofline = 'Proof outline'
                else:
                    proofline = 'Proof'
                doc.asis(
                    join_nonempty(
                        self.localize(proofline),
                        self.format(tag.find("of"),
                                    blanks_to_pars=False)).rstrip() + ".")
            doc.asis(rstrip_p(" " + self.format(tag, blanks_to_pars=True)))
            doc.asis("<span class='end-of-proof'>&#8718;</span>")
        return doc.getvalue() + "\n<p>"
Ejemplo n.º 2
0
    def handle_enumerateable(self, tag: QqTag) -> str:
        """
        Uses tags: label, number, name
        Add tags used manually from enumerateable_envs

        :param tag:
        :return:
        """
        doc, html, text = Doc().tagtext()
        name = tag.name
        env_localname = self.localize(self.enumerateable_envs[name])
        with html("div", klass="env env__" + name):
            if tag.find("label"):
                doc.attr(id=self.label2id(tag.label_.value))

            number = tag.get("number", "")
            with html("span", klass="env-title env-title__" + name):
                if tag.find("label"):
                    with html("a",
                              klass="env-title env-title__" + name,
                              href="#" + self.label2id(tag.label_.value)):
                        text(join_nonempty(env_localname, number) + ".")
                else:
                    text(join_nonempty(env_localname, number) + ".")

            doc.asis(" " + self.format(tag, blanks_to_pars=True))
        return "<p>" + doc.getvalue() + "</p>\n<p>"
Ejemplo n.º 3
0
 def tag_id(self, tag: QqTag) -> str:
     """
     Returns id of tag:
     - If it has label, it is label-based
     - If it does not have label, but have number, it is number-based
     :param tag:
     :return: str id
     """
     if tag.find("label"):
         return self.label2id(tag.label_.value)
     elif tag.find("number"):
         return (self.label2id(tag.name + "_number_" +
                               str(tag.number_.value)))
     else:
         return ""
Ejemplo n.º 4
0
    def handle_heading(self, tag: QqTag) -> str:
        """
        Uses tags: chapter, section, subsection, subsubsection
        Uses tags: label, number

        Example:

            \chapter This is first heading

            \section This is the second heading \label{sec:second}

        :param tag:
        :return:
        """
        tag_to_hx = {
            'chapter': 'h1',
            'section': 'h2',
            'subsection': 'h3',
            'subsubsection': 'h4'
        }

        doc, html, text = Doc().tagtext()
        with html(tag_to_hx[tag.name]):
            doc.attr(id=self.tag_id(tag))
            if tag.find("number"):
                with html("span", klass="section__number"):
                    with html("a",
                              href="#" + self.tag_id(tag),
                              klass="section__number"):
                        text(tag.number_.value)
            doc.asis(self.format(tag, blanks_to_pars=False))
        ret = doc.getvalue()
        if tag.next() and isinstance(tag.next(), str):
            ret += "<p>"
        return doc.getvalue()
Ejemplo n.º 5
0
    def handle_figure(self, tag: QqTag) -> str:
        """
        Currently, only python-generated figures and plotly figures are
        supported. Also one can use \rawhtml to embed arbitrary HTML code
        (e.g. use D3.js).

        Example:

        \figure \label fig:figure
            \pythonfigure
                plt.plot([1, 2, 3], [1, 4, 9])
            \caption
                Some figure

        Uses tags: figure, label, caption, number, showcode, collapsed

        :param tag: QqTag
        :return: HTML of figure
        """
        doc, html, text = Doc().tagtext()
        subtags = ['pythonfigure', 'plotly', 'rawhtml']
        langs = {
            'pythonfigure': 'python',
            'plotly': 'python',
            'rawhtml': 'html'
        }
        with html("div", klass="figure"):
            if tag.find("label"):
                doc.attr(id=self.label2id(tag.label_.value))
                label = tag.label_.value
            else:
                label = None
            for child in tag.children_tags():
                if child.name in subtags:
                    if tag.exists("showcode"):
                        doc.asis(
                            self.showcode(child,
                                          collapsed=tag.exists("collapsed"),
                                          lang=langs.get(child.name)))
                    doc.asis(self.handle(child))
                elif child.name == 'caption':
                    with html("div", klass="figure_caption"):
                        if label is not None:
                            with html("a",
                                      klass="figure_caption_anchor",
                                      href="#" + self.label2id(label)):
                                text(
                                    join_nonempty(self.localize("Fig."),
                                                  tag.get("number")))
                            text(": ")
                        else:
                            text(
                                join_nonempty(self.localize("Fig."),
                                              tag.get("number")) + ": ")
                        doc.asis(self.format(child, blanks_to_pars=True))
        return doc.getvalue()
Ejemplo n.º 6
0
    def handle_equation(self, tag: QqTag) -> str:
        """
        Uses tags: equation, number, label

        Example:

        \equation \label eq:first
            x^2 + y^2 = z^2

        :param tag:
        :return:
        """
        doc, html, text = Doc().tagtext()
        with html("div", klass="latex_equation"):
            text("\\[\n")
            text("\\begin{equation}\n")
            if tag.find('number'):
                text("\\tag{{{}}}\n".format(tag.number_.value))
            if tag.find('label'):
                doc.attr(id=self.label2id(tag.label_.value))
            doc.asis(self.format(tag, blanks_to_pars=False))
            text("\\end{equation}\n")
            text("\\]\n")
        return doc.getvalue()