Skip to content

Commit

Permalink
Include property documentation in generated output
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Sep 29, 2023
1 parent 3fed0e3 commit e34d4ce
Show file tree
Hide file tree
Showing 5 changed files with 729 additions and 2 deletions.
9 changes: 8 additions & 1 deletion meta/generate_tag_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
from typing import TextIO
from .scrape_tags import main as generate_tag_data, TagInfo
from pyhtml.__util import increase_indent


TEMPLATES_FOLDER = Path('./meta/templates')
Expand Down Expand Up @@ -36,11 +37,12 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
"""
text = get_template_class(tag.base)

# Generate property arguments and unions
# Generate property arguments, unions and documentation
# To get a better idea of these, look inside the template files to see
# what would be replaced
prop_args_gen = []
prop_unions_gen = []
prop_docs_gen = []
for prop in tag.properties:
prop_args_gen.append(
# Yucky hard-coded spaces, I can't be bothered to fix this
Expand All @@ -49,9 +51,12 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
f" {prop.name}: Optional[{prop.type}] = {prop.default!r},"
)
prop_unions_gen.append(f" '{prop.name}': {prop.name},")
prop_docs_gen.append(f"* {prop.name}: {prop.doc}")

prop_args = '\n'.join(prop_args_gen).strip()
prop_unions = '\n'.join(prop_unions_gen).strip()
prop_docs_outer = '\n'.join(increase_indent(prop_docs_gen, 4)).strip()
prop_docs_inner = '\n'.join(increase_indent(prop_docs_gen, 8)).strip()

# Determine whether the class should mandate keyword-only args
# If there are no named properties, we set it to '' to avoid a syntax error
Expand All @@ -70,6 +75,8 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
.replace("{link}", tag.mdn_link)\
.replace("{prop_args}", prop_args)\
.replace("{prop_unions}", prop_unions)\
.replace("{prop_docs_outer}", prop_docs_outer)\
.replace("{prop_docs_inner}", prop_docs_inner)\
.replace("{kw_only}", kw_only)

print(text, file=output)
Expand Down
6 changes: 6 additions & 0 deletions meta/templates/class_props_SelfClosingTag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class {name}({base}):
"""
{description}
{prop_docs_outer}
[View full documentation]({link})
"""
def __init__(
Expand All @@ -13,6 +15,8 @@ def __init__(
"""
{description}
{prop_docs_inner}
[View full documentation]({link})
"""
properties |= {
Expand All @@ -29,6 +33,8 @@ def __call__(
"""
{description}
{prop_docs_inner}
[View full documentation]({link})
"""
properties |= {
Expand Down
6 changes: 6 additions & 0 deletions meta/templates/class_props_StylableTag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class {name}({base}):
"""
{description}
{prop_docs_outer}
[View full documentation]({link})
"""
def __init__(
Expand All @@ -16,6 +18,8 @@ def __init__(
"""
{description}
{prop_docs_inner}
[View full documentation]({link})
"""
properties |= {
Expand All @@ -38,6 +42,8 @@ def __call__(
"""
{description}
{prop_docs_inner}
[View full documentation]({link})
"""
properties |= {
Expand Down
6 changes: 6 additions & 0 deletions meta/templates/class_props_Tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class {name}({base}):
"""
{description}
{prop_docs_outer}
[View full documentation]({link})
"""
def __init__(
Expand All @@ -13,6 +15,8 @@ def __init__(
"""
{description}
{prop_docs_inner}
[View full documentation]({link})
"""
properties |= {
Expand All @@ -29,6 +33,8 @@ def __call__(
"""
{description}
{prop_docs_inner}
[View full documentation]({link})
"""
properties |= {
Expand Down
Loading

0 comments on commit e34d4ce

Please sign in to comment.