def test_module_index_submodule(app): text = ".. py:module:: sphinx.config\n" restructuredtext.parse(app, text) index = PythonModuleIndex(app.env.get_domain('py')) assert index.generate() == ( [('s', [IndexEntry('sphinx', 1, '', '', '', '', ''), IndexEntry('sphinx.config', 2, 'index', 'module-sphinx.config', '', '', '')])], False )
def test_module_index_not_collapsed(app): text = (".. py:module:: docutils\n" ".. py:module:: sphinx\n") restructuredtext.parse(app, text) index = PythonModuleIndex(app.env.get_domain('py')) assert index.generate() == ([ ('d', [IndexEntry('docutils', 0, 'index', 'module-docutils', '', '', '')]), ('s', [IndexEntry('sphinx', 0, 'index', 'module-sphinx', '', '', '')]) ], True)
def generate(self, docnames=None): msg = f"generate {self.localname}" logger.info(prefix + msg) content = defaultdict(list) object_filter = ["class", "namespace", "module"] objects = { name: (dispname, typ, docname, anchor) for name, dispname, typ, docname, anchor, _ in self.domain.get_objects() if typ in object_filter } prev_modname = "" num_toplevels = 0 for obj in sorted(objects): dispname, typ, docname, anchor = objects[obj] subtype = 0 modname = dispname.split(".")[0] entries = content.setdefault(modname[0].lower(), []) if dispname != modname: # it's an object of a module # and even the first object - make parent a group head if prev_modname == dispname and entries: last = entries[-1] entries[-1] = IndexEntry(last[0], subtype, last[2], last[3], last[4], last[5], last[6]) elif not dispname.startswith(prev_modname): # object without parent in list, add dummy entry entries.append( IndexEntry(dispname, subtype, "", "", "", "", "")) subtype = 2 if self.use_short_names: dispname = dispname.split(".")[-1] else: num_toplevels += 1 subtype = 1 entries.append( IndexEntry(dispname, subtype, docname, anchor, "", "", typ)) prev_modname = modname # apply heuristics when to collapse index at page load: # only collapse if number of toplevel modules is larger than # number of objects (same logic in python domain) # update: customize behaviour with collapse option if self.collapse is None: collapse = len(objects) - num_toplevels < num_toplevels # sort by first letter sorted_content = sorted(content.items()) return sorted_content, self.collapse
def generate( self, docnames: Iterable[str] = None ) -> Tuple[List[Tuple[str, List[IndexEntry]]], bool]: content: Dict[str, List[IndexEntry]] = {} # list of prefixes to ignore ignores: List[str] = self.domain.env.config["dbus_index_common_prefix"] ignores = sorted(ignores, key=len, reverse=True) ifaces = sorted( [ x for x in self.domain.data["objects"].items() if x[1].objtype == "interface" ], key=lambda x: x[0].lower(), ) for name, (docname, node_id, _) in ifaces: if docnames and docname not in docnames: continue for ignore in ignores: if name.startswith(ignore): name = name[len(ignore) :] stripped = ignore break else: stripped = "" entries = content.setdefault(name[0].lower(), []) entries.append(IndexEntry(stripped + name, 0, docname, node_id, "", "", "")) # sort by first letter sorted_content = sorted(content.items()) return sorted_content, False
def test_modindex_common_prefix(app): text = (".. py:module:: docutils\n" ".. py:module:: sphinx\n" ".. py:module:: sphinx.config\n" ".. py:module:: sphinx.builders\n" ".. py:module:: sphinx.builders.html\n" ".. py:module:: sphinx_intl\n") restructuredtext.parse(app, text) index = PythonModuleIndex(app.env.get_domain('py')) assert index.generate() == ( [ ( 'b', [ IndexEntry('sphinx.builders', 1, 'index', 'module-sphinx.builders', '', '', ''), # NOQA IndexEntry('sphinx.builders.html', 2, 'index', 'module-sphinx.builders.html', '', '', '') ]), # NOQA ('c', [ IndexEntry('sphinx.config', 0, 'index', 'module-sphinx.config', '', '', '') ]), ('d', [ IndexEntry('docutils', 0, 'index', 'module-docutils', '', '', '') ]), ('s', [ IndexEntry('sphinx', 0, 'index', 'module-sphinx', '', '', ''), IndexEntry('sphinx_intl', 0, 'index', 'module-sphinx_intl', '', '', '') ]) ], True)
def generate( self, docnames: Iterable[str] = None ) -> Tuple[List[Tuple[str, List[IndexEntry]]], bool]: entries = [] objects = cast(ThriftDomain, self.domain).thrift_objects for obj in objects: entries.append( IndexEntry(name=obj.signature.name, subtype=0, docname=obj.document, anchor=str(obj.signature), extra=self.domain.object_types[ obj.signature.kind.value].lname, qualifier='', descr='')) content: Dict[str, List[IndexEntry]] = {} for key, group in groupby(entries, key=lambda t: t[0][0].upper()): content.setdefault(key, []).extend(group) return sorted(content.items(), key=lambda t: t[0]), False
def generate(self, docnames: Iterable[str] = None ) -> Tuple[List[Tuple[str, List[IndexEntry]]], bool]: content = {} # type: Dict[str, List[IndexEntry]] # list of prefixes to ignore ignores = None # type: List[str] ignores = self.domain.env.config['modindex_common_prefix'] # type: ignore ignores = sorted(ignores, key=len, reverse=True) # list of all modules, sorted by module name modules = sorted(self.domain.data['modules'].items(), key=lambda x: x[0].lower()) # sort out collapsable modules prev_modname = '' num_toplevels = 0 for modname, (docname, node_id, synopsis, platforms, deprecated) in modules: if docnames and docname not in docnames: continue for ignore in ignores: if modname.startswith(ignore): modname = modname[len(ignore):] stripped = ignore break else: stripped = '' # we stripped the whole module name? if not modname: modname, stripped = stripped, '' entries = content.setdefault(modname[0].lower(), []) package = modname.split('.')[0] if package != modname: # it's a submodule if prev_modname == package: # first submodule - make parent a group head if entries: last = entries[-1] entries[-1] = IndexEntry(last[0], 1, last[2], last[3], last[4], last[5], last[6]) elif not prev_modname.startswith(package): # submodule without parent in list, add dummy entry entries.append(IndexEntry(stripped + package, 1, '', '', '', '', '')) subtype = 2 else: num_toplevels += 1 subtype = 0 qualifier = _('Deprecated') if deprecated else '' entries.append(IndexEntry(stripped + modname, subtype, docname, node_id, platforms, qualifier, synopsis)) prev_modname = modname # apply heuristics when to collapse modindex at page load: # only collapse if number of toplevel modules is larger than # number of submodules collapse = len(modules) - num_toplevels < num_toplevels # sort by first letter sorted_content = sorted(content.items()) return sorted_content, collapse