Skip to content

Commit

Permalink
refactor: Update MarkdownEngine.make_converter to MarkdownEngine.conv…
Browse files Browse the repository at this point in the history
…erter property
  • Loading branch information
bow committed Aug 31, 2023
1 parent 28be9ba commit 7c9dabc
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions volt/engines/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,6 @@ class MarkdownEngine(Engine):
"footnotes": True,
}

@staticmethod
def make_converter(
extras: Optional[dict] = None,
default_extras: dict = default_extras,
) -> Callable[[str], str]:
resolved_extras = _resolve_extras(extras, default_extras)

kwargs: dict = {}
if isinstance((fd := resolved_extras.get("footnotes", None)), dict):
for k in ("footnote_return_symbol", "footnote_title"):
if (v := fd.get(k)) is not None:
kwargs[k] = v

return cast(
Callable[[str], str], Markdown(extras=resolved_extras, **kwargs).convert
)

def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
template_name = self.opts.pop("template_name", "page.html.j2")
Expand All @@ -91,7 +74,6 @@ def create_targets(self) -> Sequence[TemplateTarget]:

config = self.config
get_sources = self.get_sources
converter = self.make_converter(self.extras)

fps = get_sources() + (get_sources(drafts=True) if config.with_drafts else [])

Expand All @@ -100,7 +82,7 @@ def create_targets(self) -> Sequence[TemplateTarget]:
src=fp,
config=config,
is_draft=is_draft,
converter=converter,
converter=self.converter,
).to_template_target(self.template)
for fp, is_draft in fps
]
Expand All @@ -111,6 +93,20 @@ def get_sources(self, drafts: bool = False) -> list[tuple[Path, bool]]:
eff_dir = self.source_dir if not drafts else self.source_drafts_dir
return [(p, drafts) for p in eff_dir.glob(f"*{constants.MARKDOWN_EXT}")]

@cached_property
def converter(self) -> Callable[[str], str]:
resolved_extras = _resolve_extras(self.extras, self.default_extras)

kwargs: dict = {}
if isinstance((fd := resolved_extras.get("footnotes", None)), dict):
for k in ("footnote_return_symbol", "footnote_title"):
if (v := fd.get(k)) is not None:
kwargs[k] = v

return cast(
Callable[[str], str], Markdown(extras=resolved_extras, **kwargs).convert
)


@dataclass(kw_only=True, eq=False)
class MarkdownSource:
Expand Down

0 comments on commit 7c9dabc

Please sign in to comment.