Exemplo n.º 1
0
Arquivo: std.py Projeto: nedbat/sphinx
def make_glossary_term(env: "BuildEnvironment", textnodes: Iterable[Node],
                       index_key: str, source: str, lineno: int, node_id: str,
                       document: nodes.document) -> nodes.term:
    # get a text-only representation of the term and register it
    # as a cross-reference target
    term = nodes.term('', '', *textnodes)
    term.source = source
    term.line = lineno
    termtext = term.astext()

    if node_id:
        # node_id is given from outside (mainly i18n module), use it forcedly
        term['ids'].append(node_id)
    else:
        node_id = make_id(env, document, 'term', termtext)
        term['ids'].append(node_id)
        document.note_explicit_target(term)

    std = cast(StandardDomain, env.get_domain('std'))
    std.note_object('term', termtext, node_id, location=term)

    # add an index entry too
    indexnode = addnodes.index()
    indexnode['entries'] = [('single', termtext, node_id, 'main', index_key)]
    indexnode.source, indexnode.line = term.source, term.line
    term.append(indexnode)

    return term
Exemplo n.º 2
0
 def result_nodes(self, document: nodes.document, env: "BuildEnvironment", node: Element,
                  is_ref: bool) -> Tuple[List[Node], List[system_message]]:
     if not is_ref:
         return [node], []
     varname = node['reftarget']
     tgtid = 'index-%s' % env.new_serialno('index')
     indexnode = addnodes.index()
     indexnode['entries'] = [
         ('single', varname, tgtid, '', None),
         ('single', _('environment variable; %s') % varname, tgtid, '', None)
     ]
     targetnode = nodes.target('', '', ids=[tgtid])
     document.note_explicit_target(targetnode)
     return [indexnode, targetnode, node], []
Exemplo n.º 3
0
def make_glossary_term(env: "BuildEnvironment",
                       textnodes: Iterable[Node],
                       index_key: str,
                       source: str,
                       lineno: int,
                       node_id: str = None,
                       document: nodes.document = None) -> nodes.term:
    # get a text-only representation of the term and register it
    # as a cross-reference target
    term = nodes.term('', '', *textnodes)
    term.source = source
    term.line = lineno
    termtext = term.astext()

    if node_id:
        # node_id is given from outside (mainly i18n module), use it forcedly
        term['ids'].append(node_id)
    elif document:
        node_id = make_id(env, document, 'term', termtext)
        term['ids'].append(node_id)
        document.note_explicit_target(term)
    else:
        warnings.warn(
            'make_glossary_term() expects document is passed as an argument.',
            RemovedInSphinx40Warning)
        gloss_entries = env.temp_data.setdefault('gloss_entries', set())
        node_id = nodes.make_id('term-' + termtext)
        if node_id == 'term':
            # "term" is not good for node_id.  Generate it by sequence number instead.
            node_id = 'term-%d' % env.new_serialno('glossary')

        while node_id in gloss_entries:
            node_id = 'term-%d' % env.new_serialno('glossary')
        gloss_entries.add(node_id)
        term['ids'].append(node_id)

    std = cast(StandardDomain, env.get_domain('std'))
    std.note_object('term',
                    termtext.lower(),
                    node_id,
                    location=(env.docname, lineno))

    # add an index entry too
    indexnode = addnodes.index()
    indexnode['entries'] = [('single', termtext, node_id, 'main', index_key)]
    indexnode.source, indexnode.line = term.source, term.line
    term.append(indexnode)

    return term
Exemplo n.º 4
0
    def parse(self, text: str, document: nodes.document, line_number: int):
        self.reset()
        self._attrs = None
        try:
            self.feed(text)
        except ExitImageParse:
            pass
        if self._attrs is None:
            return

        # TODO check for preceding text?

        if "src" not in self._attrs:
            return make_error(document, "missing src attribute", text,
                              line_number)

        options = {}
        for name, key, spec in [
            ("src", "uri", directives.uri),
            ("class", "classes", directives.class_option),
            ("alt", "alt", directives.unchanged),
            ("height", "height", directives.length_or_unitless),
            ("width", "width", directives.length_or_percentage_or_unitless),
            ("align", "align", align)
                # note: docutils also has scale and target
        ]:
            if name in self._attrs:
                value = self._attrs[name]
                try:
                    options[key] = spec(value)
                except (ValueError, TypeError) as error:
                    error_msg = "Invalid attribute: (key: '{}'; value: {})\n{}".format(
                        name, value, error)
                    return make_error(document, error_msg, text, line_number)

        node = nodes.image(text, **options)
        if "name" in self._attrs:
            name = nodes.fully_normalize_name(self._attrs["name"])
            node["names"].append(name)
            document.note_explicit_target(node, node)

        return node
Exemplo n.º 5
0
    def result_nodes(
            self, document: nodes.document, env: BuildEnvironment,
            node: nodes.Element, is_ref: bool
    ) -> Tuple[List[nodes.Node], List[nodes.system_message]]:
        if not is_ref:
            return [node], []

        make_index = _index_objects.get(ThriftObjectKind(node['reftype']),
                                        None)
        if make_index is not None:
            target_id = 'index-{}-{}'.format(
                env.new_serialno('index-{}'.format(node['reftarget'])),
                node['reftarget'])
            target = nodes.target('', '', ids=[target_id])
            document.note_explicit_target(target)

            index = addnodes.index()
            index['entries'] = [
                make_index(cast(ThriftDomain, env.get_domain('thrift')),
                           node['reftarget'], target_id)
            ]
            return [target, index, node], []
        return [node], []