_sphinxsymbols.py¶
clew/wiki/_sphinxsymbols.py
A Sphinx domain that puts every symbol section in the site's objects.inv (P5, S8; 2.0.0).
_keep_bodies_out_of_search
None clew.wiki._sphinxsymbols._keep_bodies_out_of_search()
Make Sphinx's search indexer skip source bodies and diagram text in raw HTML.
clew/wiki/_sphinxsymbols.py:142
Source · 46 lines
def _keep_bodies_out_of_search() -> None:
"""THE INDEXER SEES A STRIPPED COPY, the page keeps every byte: the original raw node is not
changed, so the HTML written is exactly as before. Sphinx itself strips `<style>` and
`<script>` the same way.
TWO SPHINX PATHS, both private, so both are covered and a test pins the result: Sphinx 8 feeds
the index through the module function `sphinx.search._feed_visit_nodes` (it recurses through
its global name, so the wrapper sees every child); 7.x through `WordCollector.dispatch_visit`.
The pinned range is `sphinx>=7.3,<9`; a release that drops both fails
`test_source_bodies_and_diagrams_are_not_in_the_search_index` loudly. Idempotent.
@brief Patch the search indexer.
@return None.
"""
import sphinx.search as search
feed_page = search.IndexBuilder.feed
if not getattr(feed_page, "_clew_wrapped", False):
def feed(self: Any, docname: str, *args: Any, **kwargs: Any) -> None:
if docname.startswith(_UNSEARCHED_PAGES):
return None
return feed_page(self, docname, *args, **kwargs)
feed._clew_wrapped = True # type: ignore[attr-defined]
search.IndexBuilder.feed = feed
if hasattr(search, "_feed_visit_nodes"):
original = search._feed_visit_nodes
if not getattr(original, "_clew_wrapped", False):
def feed(node: Any, **kwargs: Any) -> None:
return original(_stripped(node), **kwargs)
feed._clew_wrapped = True # type: ignore[attr-defined]
search._feed_visit_nodes = feed
collector = getattr(search, "WordCollector", None)
visit = getattr(collector, "dispatch_visit", None)
if visit is not None and not getattr(visit, "_clew_wrapped", False):
def dispatch_visit(self: Any, node: Any) -> None:
return visit(self, _stripped(node))
dispatch_visit._clew_wrapped = True # type: ignore[attr-defined]
collector.dispatch_visit = dispatch_visit
Activity · 18 steps
Requirements
get_objects
Any clew.wiki._sphinxsymbols.ClewDomain.get_objects(self)
Every inventory object: (name, display name, type, page, anchor, priority).
clew/wiki/_sphinxsymbols.py:48
Source · 14 lines
def get_objects(self) -> Any:
"""PRIORITY 1, Sphinx's "important" (the default for an object an index should list);
a bare alias is priority 2, "unimportant", so a search ranks the qualified form first.
@brief Yield objects.
@return Objects.
"""
rows = _rows(Path(self.env.srcdir) / OBJECTS_FILE)
once = Counter(r["name"] for r in rows)
for r in rows:
yield (f"{r['file']}::{r['name']}", r["name"], "function", r["page"], r["anchor"], 1)
if once[r["name"]] == 1:
yield (r["name"], r["name"], "function", r["page"], r["anchor"], 2)
Activity · 6 steps
Requirements
setup
dict[str, Any] clew.wiki._sphinxsymbols.setup(Any app)
Register the domain.
clew/wiki/_sphinxsymbols.py:111
Called by Documented_After_Decorative, Module_Init, Module_Init
Tested by
Source · 12 lines
def setup(app: Any) -> dict[str, Any]:
"""@brief Sphinx entry point. @return Metadata."""
app.add_domain(ClewDomain)
## 2.1 roadmap: ```mermaid fences in prose warned "Pygments lexer name 'mermaid' is not known"
## (9 in clew's own build). Highlighted as plain text: the diagram source shows, unwarned.
from pygments.lexers.special import TextLexer
app.add_lexer("mermaid", TextLexer)
_keep_bodies_out_of_search()
return {"version": "1", "parallel_read_safe": True, "parallel_write_safe": True}