Exemple #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()
Exemple #2
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 #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_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 #6
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()
Exemple #7
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 #8
0
 def get_counter_for_tag(self, tag: QqTag) -> 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 #9
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 #10
0
 def get_counter_for_tag(self, tag: QqTag) -> 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 #11
0
    def handle_quiz(self, tag: QqTag):
        """
        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="templates/quiz.html")
        return template.render(formatter=self, tag=tag)
Exemple #12
0
    def handle_quiz(self, tag: QqTag):
        """
        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="templates/quiz.html")
        return template.render(formatter=self, tag=tag)
Exemple #13
0
    def handle_snref(self, tag: QqTag) -> str:
        """
        Makes snippet ref.

        Example:

            Consider \snref[Initial Value Problem|sn:IVP].

        Here sn:IVP -- label of snippet.

        If no separator present, fuzzy search will be performed over flabels

        Example:

        \snippet \label sn:IVP \flabel Initial Value Problem
            Initial Value Problem is a problem with initial value

        Consider \snref[initial value problem].


        :param tag:
        :return:
        """
        doc, html, text = Doc().tagtext()
        if tag.exists("separator"):
            title, labelfield = tag.split_by_sep()
            label = "".join(labelfield)
        else:
            title = tag.value.replace("\n", " ")
            target = self.find_tag_by_flabel(title)
            label = target._label.value

        data_url = self.url_for_snippet(label)
        with html("a", ('data-url', data_url), klass="snippet-ref"):
            doc.asis(self.format(title, blanks_to_pars=True))
        return doc.getvalue()
Exemple #14
0
    def handle_snref(self, tag: QqTag) -> str:
        """
        Makes snippet ref.

        Example:

            Consider \snref[Initial Value Problem|sn:IVP].

        Here sn:IVP -- label of snippet.

        If no separator present, fuzzy search will be performed over flabels

        Example:

        \snippet \label sn:IVP \flabel Initial Value Problem
            Initial Value Problem is a problem with initial value

        Consider \snref[initial value problem].


        :param tag:
        :return:
        """
        doc, html, text = Doc().tagtext()
        if tag.exists("separator"):
            title, labelfield = tag.split_by_sep()
            label = "".join(labelfield)
        else:
            title = tag.value.replace("\n", " ")
            target = self.find_tag_by_flabel(title)
            label = target._label.value

        data_url = self.url_for_snippet(label)
        with html("a", ('data-url', data_url), klass="snippet-ref"):
            doc.asis(self.format(title, blanks_to_pars=True))
        return doc.getvalue()
Exemple #15
0
    def handle_ref(self, tag: QqTag):
        """
        Examples:

            See Theorem \ref{thm:existence}

        Other way:

            See \ref[Theorem|thm:existence]

        In this case word ``Theorem'' will be part of a reference: e.g. in HTML it will look like

            See <a href="#label_thm:existence">Theorem 1</a>

        If you want to omit number, just use \nonumber tag like so:

            See \ref[Theorem\nonumber|thm:existence]

        This will produce HTML like
            See <a href="#label_thm:existence">Theorem</a>


        Uses tags: ref, nonumber

        :param tag:
        :return:
        """
        doc, html, text = Doc().tagtext()
        if tag.is_simple:
            prefix = None
            label = tag.value
        else:
            prefix, labelfield = tag.split_by_sep()
            label = "".join(labelfield).strip()

        number = self.label2number.get(label, "???")
        target = self.label2tag[label]
        href = ""
        if self.mode == 'bychapters':
            if 'snippet' not in [t.name for t in tag.ancestor_path()]:
                # check that we're not inside snippet now
                fromindex = self.tag2chapter(tag)
            else:
                fromindex = None
            href = self.url_for_chapter(self.tag2chapter(target),
                                        fromindex=fromindex)

        eqref = target.name in self.formulaenvs or target.name == 'item' and target.parent.name in self.formulaenvs

        if eqref:
            href += "#mjx-eqn-" + str(number)
        else:
            href += "#" + self.label2id(label)

        with html("span", klass="ref"):
            with html("a",
                      klass="a-ref",
                      href=href,
                      title=self.label2title.get(label, "")):
                if prefix:
                    doc.asis(self.format(prefix, blanks_to_pars=False))
                if eqref and hasattr(self, 'url_for_eq_snippet'):
                    doc.attr(('data-url', self.url_for_eq_snippet(number)))
                if not tag.exists("nonumber"):
                    if prefix:
                        doc.asis(" ")
                    if eqref:
                        text("(" + number + ")")
                    else:
                        text(number)
        return doc.getvalue()
Exemple #16
0
    def handle_ref(self, tag: QqTag):
        """
        Examples:

            See Theorem \ref{thm:existence}

        Other way:

            See \ref[Theorem|thm:existence]

        In this case word ``Theorem'' will be part of a reference: e.g. in HTML it will look like

            See <a href="#label_thm:existence">Theorem 1</a>

        If you want to omit number, just use \nonumber tag like so:

            See \ref[Theorem\nonumber|thm:existence]

        This will produce HTML like
            See <a href="#label_thm:existence">Theorem</a>


        Uses tags: ref, nonumber

        :param tag:
        :return:
        """
        doc, html, text = Doc().tagtext()
        if tag.is_simple:
            prefix = None
            label = tag.value
        else:
            prefix, labelfield = tag.split_by_sep()
            label = "".join(labelfield).strip()

        number = self.label2number.get(label, "???")
        target = self.label2tag[label]
        href = ""
        if self.mode == 'bychapters':
            if 'snippet' not in [t.name for t in tag.ancestor_path()]:
                # check that we're not inside snippet now
                fromindex = self.tag2chapter(tag)
            else:
                fromindex = None
            href = self.url_for_chapter(self.tag2chapter(target), fromindex=fromindex)

        eqref = target.name in self.formulaenvs or target.name == 'item' and target.parent.name in self.formulaenvs

        if eqref:
            href += "#mjx-eqn-" + str(number)
        else:
            href += "#"+self.label2id(label)

        with html("span", klass="ref"):
            with html("a", klass="a-ref", href=href,
                      title=self.label2title.get(label, "")):
                if prefix:
                    doc.asis(self.format(prefix, blanks_to_pars=False))
                if eqref and hasattr(self, 'url_for_eq_snippet'):
                    doc.attr(('data-url', self.url_for_eq_snippet(number)))
                if not tag.exists("nonumber"):
                    if prefix:
                        doc.asis(" ")
                    if eqref:
                        text("(" + number + ")")
                    else:
                        text(number)
        return doc.getvalue()