예제 #1
0
    def handle_figure(self, tag: QqTag) -> str:
        """
        Currently, only python-generated figures and plotly figures are supported.

        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:
                if isinstance(child, QqTag):
                    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()
예제 #2
0
파일: qqhtml.py 프로젝트: ischurov/qqmbr
    def handle_figure(self, tag: QqTag) -> str:
        """
        Currently, only python-generated figures and plotly figures are supported.

        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:
                if isinstance(child, QqTag):
                    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()