Skip to content

Commit

Permalink
chore: move md generation to MkClickDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Nov 4, 2023
1 parent 7a3bd43 commit 3bac1e6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
33 changes: 32 additions & 1 deletion mknodes/basenodes/mkclickdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,39 @@ def _to_markdown(self) -> str:
attrs = self.attributes
mod = importlib.import_module(attrs["module"])
instance = getattr(mod, attrs["command"])

def param_to_md(param) -> str:
lines = [f"### {param.opt_str}"]
if param.required:
lines.append("**REQUIRED**")
if param.envvar:
lines.append(f"**Environment variable:** {param.envvar}")
if param.multiple:
lines.append("**Multiple values allowed.**")
if param.default:
lines.append(f"**Default:** {param.default}")
if param.is_flag:
lines.append(f"**Flag:** {param.flag_value}")
if param.help:
lines.append(param.help)
return "\n\n".join(lines)

def info_to_md(info, recursive: bool = False) -> str:
import mknodes as mk

header = f"## {info.name}\n\n"
text = header + info.description + "\n\n" + str(mk.MkCode(info.usage))
params = [param_to_md(i) for i in info.params]
cmd_text = text + "\n\n\n" + "\n\n\n".join(params)
if not recursive:
return cmd_text
children_text = "\n".join(
info_to_md(i, recursive=True) for i in info.subcommands.values()
)
return cmd_text + children_text

info = clihelpers.get_typer_info(instance, command=attrs["prog_name"])
return info.to_markdown(recursive=self.show_subcommands)
return info_to_md(info, recursive=self.show_subcommands)

@classmethod
def create_example_page(cls, page):
Expand Down
38 changes: 4 additions & 34 deletions mknodes/utils/clihelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import typer


@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class Param:
count: bool = False
"""Whether the parameter increments an integer."""
Expand Down Expand Up @@ -61,24 +61,8 @@ def opt_str(self) -> str:
"""A formatted and sorted string containing the the options."""
return ", ".join(f"`{i}`" for i in reversed(self.opts))

def to_markdown(self):
lines = [f"### {self.opt_str}"]
if self.required:
lines.append("**REQUIRED**")
if self.envvar:
lines.append(f"**Environment variable:** {self.envvar}")
if self.multiple:
lines.append("**Multiple values allowed.**")
if self.default:
lines.append(f"**Default:** {self.default}")
if self.is_flag:
lines.append(f"**Flag:** {self.flag_value}")
if self.help:
lines.append(self.help)
return "\n\n".join(lines)


@dataclasses.dataclass

@dataclasses.dataclass(frozen=True)
class CommandInfo:
name: str
"""The name of the command."""
Expand All @@ -100,20 +84,6 @@ class CommandInfo:
def __getitem__(self, name):
return self.subcommands[name]

def to_markdown(self, recursive: bool = False):
import mknodes as mk

header = f"## {self.name}\n\n"
text = header + self.description + "\n\n" + str(mk.MkCode(self.usage))
params = [i.to_markdown() for i in self.params]
cmd_text = text + "\n\n\n" + "\n\n\n".join(params)
if not recursive:
return cmd_text
children_text = "\n".join(
i.to_markdown(recursive=True) for i in self.subcommands.values()
)
return cmd_text + children_text


def get_typer_info(
instance: typer.Typer | click.Group,
Expand Down Expand Up @@ -195,4 +165,4 @@ def _make_usage(ctx: click.Context) -> str:
import mkdocs.__main__

info = get_typer_info(mkdocs.__main__.cli, command="mkdocs")
pprint(info.to_markdown(recursive=True))
pprint(info)

0 comments on commit 3bac1e6

Please sign in to comment.