Skip to content

Commit

Permalink
Add option to make types bold (#84)
Browse files Browse the repository at this point in the history
This makes xrefs look a bit better because sphinx seems to automatically render
them in bold
  • Loading branch information
hoodmane authored Sep 28, 2023
1 parent 6b47806 commit de15ed3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions sphinx_js/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def setup(app: Sphinx) -> None:
)
app.add_config_value("jsdoc_config_path", default=None, rebuild="env")
app.add_config_value("ts_type_xref_formatter", None, "env")
app.add_config_value("ts_type_bold", False, "env")
app.add_config_value("ts_should_destructure_arg", None, "env")
app.add_config_value("ts_post_convert", None, "env")

Expand Down
24 changes: 23 additions & 1 deletion sphinx_js/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class JsRenderer:
_renderer_type: Literal["function", "class", "attribute"]
_template: str
_type_xref_formatter: Callable[[TypeXRef], str]
_type_text_formatter: Callable[[str], str]
_partial_path: list[str]
_explicit_formal_params: str
_content: list[str]
Expand All @@ -74,7 +75,17 @@ def _set_type_xref_formatter(
def default_type_xref_formatter(xref: TypeXRef) -> str:
return xref.name

self._type_xref_formatter = default_type_xref_formatter
def _set_type_text_formatter(
self, formatter: Callable[[Config, str], str] | None
) -> None:
if formatter:
self._type_text_formatter = partial(formatter, self._app.config)
return

def default_type_text_formatter(text: str) -> str:
return text

self._type_text_formatter = default_type_text_formatter

def __init__(
self,
Expand All @@ -92,6 +103,15 @@ def __init__(
self._app = app
self._set_type_xref_formatter(app.config.ts_type_xref_formatter)

def bold_formatter(conf: Config, text: str) -> str:
parts = ["**" + part + "**" for part in text.split(" ") if part]
return " ".join(parts).strip()

if app.config.ts_type_bold:
self._set_type_text_formatter(bold_formatter)
else:
self._set_type_text_formatter(None)

# content, arguments, options, app: all need to be accessible to
# template_vars, so we bring them in on construction and stow them away
# on the instance so calls to template_vars don't need to concern
Expand Down Expand Up @@ -301,6 +321,8 @@ def strs() -> Iterator[str]:
while True:
xref: list[TypeXRef] = []
s = "".join(strs())
if s:
s = self._type_text_formatter(s)
if escape:
s = rst.escape(s)
if s:
Expand Down
1 change: 1 addition & 0 deletions tests/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class _app:
renderer._explicit_formal_params = None
renderer._content = []
renderer._set_type_xref_formatter(ts_xref_formatter)
renderer._set_type_text_formatter(None)
return renderer


Expand Down
2 changes: 2 additions & 0 deletions tests/test_typedoc_analysis/test_typedoc_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ def test_constrained_by_key(self):

# TODO: this part maybe belongs in a unit test for the renderer or something
a = AutoFunctionRenderer.__new__(AutoFunctionRenderer)
a._set_type_text_formatter(None)
a._explicit_formal_params = None
a._content = []
rst = a.rst([obj.name], obj)
Expand All @@ -540,6 +541,7 @@ def test_class_constrained(self):
description=[DescriptionText("The type we contain")],
)
a = AutoClassRenderer.__new__(AutoClassRenderer)
a._set_type_text_formatter(None)
a._explicit_formal_params = None
a._content = []
a._options = {}
Expand Down

0 comments on commit de15ed3

Please sign in to comment.