From 8491217553e8abe764cc93c3f73088959fa8543a Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Wed, 21 Nov 2018 00:25:34 +0900 Subject: [PATCH] Add IndexEntry class to represent an entry of indices --- sphinx/builders/html.py | 4 ++-- sphinx/domains/__init__.py | 12 +++++++++++- sphinx/domains/python.py | 19 +++++++++++-------- sphinx/writers/latex.py | 5 +++-- sphinx/writers/texinfo.py | 7 ++++--- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index 8007c95bc..34513faa5 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -58,7 +58,7 @@ if False: from typing import Any, Dict, IO, Iterable, Iterator, List, Type, Tuple, Union # NOQA from sphinx.application import Sphinx # NOQA from sphinx.config import Config # NOQA - from sphinx.domains import Domain, Index # NOQA + from sphinx.domains import Domain, Index, IndexEntry # NOQA from sphinx.util.tags import Tags # NOQA # Experimental HTML5 Writer @@ -249,7 +249,7 @@ class StandaloneHTMLBuilder(Builder): default_html5_translator = False imgpath = None # type: unicode - domain_indices = [] # type: List[Tuple[unicode, Type[Index], List[Tuple[unicode, List[List[Union[unicode, int]]]]], bool]] # NOQA + domain_indices = [] # type: List[Tuple[unicode, Type[Index], List[Tuple[unicode, List[IndexEntry]]], bool]] # NOQA # cached publisher object for snippets _publisher = None diff --git a/sphinx/domains/__init__.py b/sphinx/domains/__init__.py index 070de1a5e..a221c783d 100644 --- a/sphinx/domains/__init__.py +++ b/sphinx/domains/__init__.py @@ -11,6 +11,7 @@ """ import copy +from typing import NamedTuple from sphinx.errors import SphinxError from sphinx.locale import _ @@ -53,6 +54,15 @@ class ObjType: self.attrs.update(attrs) +IndexEntry = NamedTuple('IndexEntry', [('name', str), + ('subtype', int), + ('docname', str), + ('anchor', str), + ('extra', str), + ('qualifier', str), + ('descr', str)]) + + class Index: """ An Index is the description for a domain-specific index. To add an index to @@ -80,7 +90,7 @@ class Index: self.domain = domain def generate(self, docnames=None): - # type: (Iterable[unicode]) -> Tuple[List[Tuple[unicode, List[List[Union[unicode, int]]]]], bool] # NOQA + # type: (Iterable[unicode]) -> Tuple[List[Tuple[unicode, List[IndexEntry]]], bool] """Return entries for the index given by *name*. If *docnames* is given, restrict to entries referring to these docnames. diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 77083cc3c..9890ac739 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -17,7 +17,7 @@ from docutils.parsers.rst import directives from sphinx import addnodes, locale from sphinx.deprecation import DeprecatedDict, RemovedInSphinx30Warning from sphinx.directives import ObjectDescription -from sphinx.domains import Domain, ObjType, Index +from sphinx.domains import Domain, ObjType, Index, IndexEntry from sphinx.locale import _, __ from sphinx.roles import XRefRole from sphinx.util import logging @@ -669,8 +669,8 @@ class PythonModuleIndex(Index): shortname = _('modules') def generate(self, docnames=None): - # type: (Iterable[unicode]) -> Tuple[List[Tuple[unicode, List[List[Union[unicode, int]]]]], bool] # NOQA - content = {} # type: Dict[unicode, List] + # type: (Iterable[unicode]) -> Tuple[List[Tuple[unicode, List[IndexEntry]]], bool] + content = {} # type: Dict[unicode, List[IndexEntry]] # list of prefixes to ignore ignores = None # type: List[unicode] ignores = self.domain.env.config['modindex_common_prefix'] # type: ignore @@ -705,19 +705,22 @@ class PythonModuleIndex(Index): if prev_modname == package: # first submodule - make parent a group head if entries: - entries[-1][1] = 1 + last = entries[-1] + entries[-1] = IndexEntry(last[0], 1, last[2], last[3], + last[4], last[5], last[6]) + entries.append(IndexEntry(stripped + package, 1, '', '', '', '', '')) elif not prev_modname.startswith(package): # submodule without parent in list, add dummy entry - entries.append([stripped + package, 1, '', '', '', '', '']) + entries.append(IndexEntry(stripped + package, 1, '', '', '', '', '')) subtype = 2 else: num_toplevels += 1 subtype = 0 qualifier = deprecated and _('Deprecated') or '' - entries.append([stripped + modname, subtype, docname, - 'module-' + stripped + modname, platforms, - qualifier, synopsis]) + entries.append(IndexEntry(stripped + modname, subtype, docname, + 'module-' + stripped + modname, platforms, + qualifier, synopsis)) prev_modname = modname # apply heuristics when to collapse modindex at page load: diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 5b9f5e75b..f06118709 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -44,6 +44,7 @@ if False: from typing import Any, Callable, Dict, Iterator, List, Pattern, Tuple, Set, Union # NOQA from sphinx.builders.latex import LaTeXBuilder # NOQA from sphinx.builders.latex import nodes as latexnodes # NOQA + from sphinx.domains import IndexEntry # NOQA logger = logging.getLogger(__name__) @@ -773,7 +774,7 @@ class LaTeXTranslator(nodes.NodeVisitor): def generate_indices(self): # type: () -> unicode def generate(content, collapsed): - # type: (List[Tuple[unicode, List[Tuple[unicode, unicode, unicode, unicode, unicode]]]], bool) -> None # NOQA + # type: (List[Tuple[unicode, List[IndexEntry]]], bool) -> None ret.append('\\begin{sphinxtheindex}\n') ret.append('\\let\\bigletter\\sphinxstyleindexlettergroup\n') for i, (letter, entries) in enumerate(content): @@ -809,7 +810,7 @@ class LaTeXTranslator(nodes.NodeVisitor): continue ret.append(u'\\renewcommand{\\indexname}{%s}\n' % indexcls.localname) - generate(content, collapsed) # type: ignore + generate(content, collapsed) return ''.join(ret) diff --git a/sphinx/writers/texinfo.py b/sphinx/writers/texinfo.py index 2b9530bac..89bad4a25 100644 --- a/sphinx/writers/texinfo.py +++ b/sphinx/writers/texinfo.py @@ -26,6 +26,7 @@ if False: # For type annotation from typing import Any, Callable, Dict, Iterator, List, Pattern, Set, Tuple, Union # NOQA from sphinx.builders.texinfo import TexinfoBuilder # NOQA + from sphinx.domains import IndexEntry # NOQA logger = logging.getLogger(__name__) @@ -480,15 +481,15 @@ class TexinfoTranslator(nodes.NodeVisitor): def collect_indices(self): # type: () -> None def generate(content, collapsed): - # type: (List[Tuple[unicode, List[List[Union[unicode, int]]]]], bool) -> unicode + # type: (List[Tuple[unicode, List[IndexEntry]]], bool) -> unicode ret = ['\n@menu\n'] # type: List[unicode] for letter, entries in content: for entry in entries: if not entry[3]: continue - name = self.escape_menu(entry[0]) # type: ignore + name = self.escape_menu(entry[0]) sid = self.get_short_id('%s:%s' % (entry[2], entry[3])) - desc = self.escape_arg(entry[6]) # type: ignore + desc = self.escape_arg(entry[6]) me = self.format_menu_entry(name, sid, desc) ret.append(me) ret.append('@end menu\n')