Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add index to all object's index #31

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/sphinxnotes/any/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@
from docutils.nodes import Element, literal, Text

from sphinx.addnodes import pending_xref
from sphinx.domains import Domain, ObjType
from sphinx.domains import Domain, ObjType, Index
from sphinx.util import logging
from sphinx.util.nodes import make_refnode

from .schema import Schema, Object
from .directives import AnyDirective
from .roles import AnyRole
from .indices import AnyIndex
from .indices import AnyIndex, AnyTypeIndex

if TYPE_CHECKING:
from sphinx.application import Sphinx
from sphinx.builders import Builder
from sphinx.environment import BuildEnvironment
from sphinx.util.typing import RoleFunction

logger = logging.getLogger(__name__)

Expand All @@ -41,14 +40,10 @@ class AnyDomain(Domain):
name:str = 'any'
#: Domain label: longer, more descriptive (used in messages)
label = 'Any'
#: Type (usually directive) name -> ObjType instance
object_types:dict[str,ObjType]= {}
#: Directive name -> directive class
directives:dict[str,type[AnyDirective]] = {}
#: Role name -> role callable
roles:dict[str,RoleFunction] = {}
#: A list of Index subclasses
indices:list[type[AnyIndex]] = []
indices: list[type[Index]] = [AnyTypeIndex]

# TODO: move to data?
#: AnyDomain specific: type -> index class
_indices_for_reftype:dict[str,type[AnyIndex]] = {}
#: AnyDomain specific: type -> Schema instance
Expand Down
20 changes: 20 additions & 0 deletions src/sphinxnotes/any/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,23 @@ def generate(self, docnames:Iterable[str]|None = None
sorted_content = sorted(content.items())

return sorted_content, False


class AnyTypeIndex(Index):
name = 'type'
localname = 'Any Type Reference Index'
shortname = 'reference'

def generate(self, docnames:Iterable[str]|None = None
) -> tuple[list[tuple[str,list[IndexEntry]]], bool]:
"""Override parent method."""
content = {} # type: dict[str, list[IndexEntry]]
objtypes = sorted(self.domain.object_types.keys())
for objtype in objtypes:
entries = content.setdefault(objtype, [])
entries.append(IndexEntry(objtype, 0, 'any-'+objtype, '', '', '', ''))

# sort by first letter
sorted_content = sorted(content.items())

return sorted_content, False