コード例 #1
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 len(tag) == 1:
            tag = tag.unitemized()
        if tag.is_simple:
            title = tag.value.replace("\n", " ")
            target = self.find_tag_by_flabel(title)
            label = target.label_.value
        else:
            if len(tag) != 2:
                raise Exception("Incorrect number of arguments in " +
                                str(tag) + (": one or two arguments "
                                            "expected"))
            title, label = tag.children_values(not_simple='keep')
            # TODO: testme

        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()
コード例 #2
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 len(tag) == 1:
            tag = tag.unitemized()

        if tag.is_simple:
            prefix = None
            label = tag.value
        else:
            if len(tag) != 2:
                raise Exception("Incorrect number of arguments in " +
                                str(tag) + ": 2 arguments expected")

            prefix, label = tag.children_values(not_simple='keep')

        number = self.label_to_number.get(label, "???")
        target = self.label_to_tag.get(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)
                    if target else "")

        eqref = (target
                 and (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.label_to_title.get(label, "")):
                if prefix:
                    doc.asis(self.format(prefix, blanks_to_pars=False))
                if eqref:
                    eq_id = label if self.eq_preview_by_labels else number
                    try:
                        doc.attr(('data-url', self.url_for_eq_snippet(eq_id)))
                    except NotImplementedError:
                        pass
                if (not isinstance(prefix, QqTag)
                        or not prefix.exists("nonumber")):
                    if prefix:
                        doc.asis(" ")
                    if eqref:
                        text("(" + number + ")")
                    else:
                        text(number)
        return doc.getvalue()