Exemple #1
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()
Exemple #2
0
 def handle_link(self, tag: QqTag) -> str:
     doc, html, text = Doc().tagtext()
     with html("p", klass="meta meta-link"):
         if tag.exists("role"):
             doc.add_class("meta-link-" + tag.role_.value)
         if tag.exists("url"):
             with html("a", href=tag.url_.value):
                 doc.asis(self.format(tag, blanks_to_pars=False))
         else:
             doc.asis(self.format(tag, blanks_to_pars=False))
     return doc.getvalue()
Exemple #3
0
    def handle_snippet(self, tag: QqTag) -> str:
        """
        Uses tags: hidden, backref, label

        :param tag:
        :return:
        """
        anchor = ""
        if not tag.exists("backref") and tag.exists("label"):
            anchor = "<span id='{}'></span>".format(
                self.label2id(tag.label_.value))
        if tag.exists("hidden"):
            return anchor
        return anchor + self.format(tag, blanks_to_pars=True)
Exemple #4
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>"
Exemple #5
0
    def handle_pythonfigure(self, tag: QqTag) -> str:
        """
        Uses tags: pythonfigure, style

        :param tag:
        :return:
        """

        path = self.make_python_fig(tag.text_content, exts=("svg", ))
        doc, html, text = Doc().tagtext()
        with html("img",
                  klass="figure img-responsive",
                  src=self.url_for_figure(path + "/" + self.default_figname +
                                          ".svg")):
            if tag.exists("style"):
                doc.attr(style=tag.style_.value)
        return doc.getvalue()
Exemple #6
0
 def get_counter_for_tag(self, tag: QqTag) -> Optional[Counter]:
     name = tag.name
     counters = self.counters
     while True:
         if tag.exists('nonumber'):
             return None
         current = counters.get(name)
         if current is None:
             return None
         if isinstance(current, Counter):
             return current
         if isinstance(current, dict):
             counters = current
             tag = tag.parent
             name = tag.name
             continue
         return None
Exemple #7
0
    def handle_quiz(self, tag: QqTag) -> str:
        """
        Uses tags: choice, correct, comment

        Example:

        \question
        Do you like qqmbr?
        \quiz
            \choice
                No.
                \comment You didn't even try!
            \choice \correct
                Yes, i like it very much!
                \comment And so do I!

        :param tag:
        :return:
        """
        if not tag.exists('md5id'):
            tag.append_child(QqTag('md5id', [self.tag_hash_id(tag)]))
        template = Template(
            filename=os.path.join(self.templates_dir, "quiz.html"))
        return template.render(formatter=self, tag=tag)