Esempio n. 1
0
 def run(self) -> List[nodes.Node]:
     """Run the directive."""
     key = self.arguments[0]
     if "doc" in self.options:
         try:
             ref = create_pending_glue_ref(self.document, self.source,
                                           self.line, self.options["doc"],
                                           key)
         except PendingGlueReferenceError as exc:
             return [glue_warning(
                 str(exc),
                 self.document,
                 self.line,
             )]
         return [ref]
     try:
         data = retrieve_glue_data(self.document, key)
     except RetrievalError as exc:
         return [
             glue_warning(
                 f"{exc} (use 'doc' option, to glue from another document)",
                 self.document,
                 self.line,
             )
         ]
     return render_variable_output(data, self.document, self.line,
                                   self.source)
Esempio n. 2
0
    def run(self):
        try:
            data = retrieve_glue_data(self.document, self.arguments[0])
        except RetrievalError as exc:
            return [glue_warning(str(exc), self.document, self.line)]
        render: Dict[str, Any] = {}
        for key in ("alt", "height", "width", "scale", "class"):
            if key in self.options:
                render.setdefault("image",
                                  {})[key.replace("classes",
                                                  "class")] = self.options[key]
        paste_nodes = render_variable_output(data,
                                             self.document,
                                             self.line,
                                             self.source,
                                             render=render)

        # note: most of this is copied directly from sphinx.Figure

        # create figure node
        figure_node = nodes.figure("", *paste_nodes)
        self.set_source_info(figure_node)

        # add attributes
        figwidth = self.options.pop("figwidth", None)
        figclasses = self.options.pop("figclass", None)
        align = self.options.pop("align", None)
        if figwidth is not None:
            figure_node["width"] = figwidth
        if figclasses:
            figure_node["classes"] += figclasses
        if align:
            figure_node["align"] = align

        # add target
        self.add_name(figure_node)

        # create the caption and legend
        if self.content:
            node = nodes.Element()  # anonymous container for parsing
            self.state.nested_parse(self.content, self.content_offset, node)
            first_node = node[0]
            if isinstance(first_node, nodes.paragraph):
                caption = nodes.caption(first_node.rawsource, "",
                                        *first_node.children)
                caption.source = first_node.source
                caption.line = first_node.line
                figure_node += caption
            elif not (isinstance(first_node, nodes.comment)
                      and len(first_node) == 0):
                error = glue_warning(
                    "Figure caption must be a paragraph or empty comment.",
                    self.document,
                    self.lineno,
                )
                return [figure_node, error]
            if len(node) > 1:
                figure_node += nodes.legend("", *node[1:])

        return [figure_node]
Esempio n. 3
0
    def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]:

        # check if this is a pending reference
        doc_key = self.text.split("::", 1)
        if len(doc_key) == 2:
            doc, key = doc_key
            try:
                ref = create_pending_glue_ref(
                    self.document, self.source, self.line, doc, key, inline=True
                )
            except PendingGlueReferenceError as exc:
                return [], [
                    glue_warning(
                        str(exc),
                        self.document,
                        self.line,
                    )
                ]
            return [ref], []

        try:
            data = retrieve_glue_data(self.document, self.text)
        except RetrievalError as exc:
            return [], [glue_warning(str(exc), self.document, self.line)]
        paste_nodes = render_variable_output(
            data,
            self.document,
            self.line,
            self.source,
            inline=True,
        )
        return paste_nodes, []
Esempio n. 4
0
 def run(self) -> list[nodes.Node]:
     """Run the directive."""
     try:
         data = retrieve_eval_data(self.document, self.arguments[0])
     except RetrievalError as exc:
         return [eval_warning(str(exc), self.document, self.line)]
     return render_variable_output(
         data,
         self.document,
         self.line,
         self.source,
         inline=False,
     )
Esempio n. 5
0
    def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]:
        try:
            data = retrieve_eval_data(self.document, self.text)
        except RetrievalError as exc:
            return [], [eval_warning(str(exc), self.document, self.line)]

        # for text/plain, we want to strip quotes from strings
        data.metadata["strip_text_quotes"] = True

        _nodes = render_variable_output(
            data,
            self.document,
            self.line,
            self.source,
            inline=True,
        )
        return _nodes, []