Skip to content

Commit

Permalink
Added rendering support for enum, dataclass, and exception
Browse files Browse the repository at this point in the history
  • Loading branch information
ltan10 committed Feb 26, 2025
1 parent 2a0a5de commit d9d60d0
Showing 1 changed file with 47 additions and 13 deletions.
60 changes: 47 additions & 13 deletions src/lazydocs/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import subprocess
import types
from dataclasses import dataclass, is_dataclass
from enum import Enum
from pydoc import locate
from typing import Any, Callable, Dict, List, Optional
from urllib.parse import quote
Expand Down Expand Up @@ -69,7 +70,7 @@
"""

_CLASS_TEMPLATE = """
{section} <kbd>class</kbd> `{header}`
{section} <kbd>{kind}</kbd> `{header}`
{doc}
{init}
{variables}
Expand Down Expand Up @@ -807,21 +808,43 @@ def class2md(self, cls: Any, depth: int = 2, is_mdx: bool = False) -> str:
return ""

section = "#" * depth
sectionheader = "#" * (depth + 1)
subsection = "#" * (depth + 2)
clsname = cls.__name__
modname = cls.__module__
header = clsname
path = self._get_src_path(cls)
doc = _doc2md(cls)
summary = _get_doc_summary(cls)
variables = []

# Handle different kinds of classes
if issubclass(cls, Enum):
kind = cls.__base__.__name__
if kind != "Enum":
kind = "enum[%s]" % (kind)
else:
kind = kind.lower()
variables.append(
"%s <kbd>symbols</kbd>\n" % (sectionheader)
)
elif is_dataclass(cls):
kind = "dataclass"
variables.append(
"%s <kbd>attributes</kbd>\n" % (sectionheader)
)
elif issubclass(cls, Exception):
kind = "exception"
else:
kind = "class"

self.generated_objects.append(
{
"type": "class",
"name": header,
"full_name": header,
"module": modname,
"anchor_tag": _get_anchor_tag("class-" + header),
"anchor_tag": _get_anchor_tag("%s-%s" % (kind, header)),
"description": summary,
}
)
Expand All @@ -839,21 +862,31 @@ def class2md(self, cls: Any, depth: int = 2, is_mdx: bool = False) -> str:
# this happens if __init__ is outside the repo
init = ""

variables = []
for name, obj in inspect.getmembers(
cls, lambda a: not (inspect.isroutine(a) or inspect.ismethod(a))
):
if not name.startswith("_") and type(obj) == property:
comments = _doc2md(obj) or inspect.getcomments(obj)
comments = "\n\n%s" % comments if comments else ""
property_name = f"{clsname}.{name}"
if not name.startswith("_"):
full_name = f"{clsname}.{name}"
if self.remove_package_prefix:
property_name = name
variables.append(
_SEPARATOR
+ "\n%s <kbd>property</kbd> %s%s\n"
% (subsection, property_name, comments)
)
full_name = name
if isinstance(obj, property):
comments = _doc2md(obj) or inspect.getcomments(obj)
comments = "\n\n%s" % comments if comments else ""
variables.append(
_SEPARATOR
+ "\n%s <kbd>property</kbd> %s%s\n"
% (subsection, full_name, comments)
)
elif isinstance(obj, Enum):
variables.append(
"- **%s** = %s\n" % (full_name, obj.value)
)
elif name == "__dataclass_fields__":
for name, field in sorted((obj).items()):
variables.append(
"- ```%s``` (%s)\n" % (name,
field.type.__name__)
)

handlers = []
for name, obj in inspect.getmembers(cls, inspect.ismethoddescriptor):
Expand Down Expand Up @@ -890,6 +923,7 @@ def class2md(self, cls: Any, depth: int = 2, is_mdx: bool = False) -> str:

markdown = _CLASS_TEMPLATE.format(
section=section,
kind=kind,
header=header,
doc=doc if doc else "",
init=init,
Expand Down

0 comments on commit d9d60d0

Please sign in to comment.