Skip to content

Commit 522fff9

Browse files
Implement default render options in generator code
1 parent 606308e commit 522fff9

File tree

12 files changed

+962
-890
lines changed

12 files changed

+962
-890
lines changed

meta/generate_tag_defs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def _get_tag_pre_content(self) -> Optional[str]:
2525
return {}
2626
"""
2727

28+
GET_DEFAULT_RENDER_OPTIONS = """
29+
def _get_default_render_options(self) -> RenderOptions:
30+
return {}
31+
"""
32+
2833

2934
def get_template_class(name: str):
3035
try:
@@ -108,6 +113,13 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
108113
file=output,
109114
)
110115

116+
# Add render options function if needed
117+
if tag.render_options is not None:
118+
print(
119+
GET_DEFAULT_RENDER_OPTIONS.replace("{}", f"{tag.render_options}"),
120+
file=output,
121+
)
122+
111123
# And a nice trailing newline to make flake8 happy
112124
print(file=output)
113125

@@ -120,6 +132,7 @@ def main(output: TextIO):
120132

121133
with open(TEMPLATES_FOLDER.joinpath("main.py")) as f:
122134
print(f.read(), file=output)
135+
print(file=output)
123136

124137
for tag in tags:
125138
# Generate the tag

meta/scrape_tags.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import yaml
1717
from typing_extensions import NotRequired
1818

19+
from pyhtml.__render_options import RenderOptions
20+
1921
TAGS_YAML = "meta/tags.yml"
2022
"""File location to load custom tag data from"""
2123

@@ -118,6 +120,15 @@ class AttrYmlItem(TypedDict):
118120
"""Python type to accept for the attribute"""
119121

120122

123+
class RenderOptionsYmlItem(TypedDict):
124+
"""
125+
Render options as a dictionary
126+
"""
127+
128+
indent: NotRequired[str]
129+
spacing: NotRequired[str]
130+
131+
121132
class TagsYmlItem(TypedDict):
122133
"""
123134
A tag which has suggested keys
@@ -143,6 +154,11 @@ class TagsYmlItem(TypedDict):
143154
Pre-content for the element (eg `<!DOCTYPE html>`)
144155
"""
145156

157+
render_options: NotRequired[RenderOptionsYmlItem]
158+
"""
159+
Render options for this element
160+
"""
161+
146162

147163
TagsYaml = dict[str, TagsYmlItem]
148164
"""Type alias for type of tags.yml file"""
@@ -216,6 +232,11 @@ class TagInfo:
216232
Pre-content for the element (eg `<!DOCTYPE html>`)
217233
"""
218234

235+
render_options: Optional[RenderOptions]
236+
"""
237+
Render options
238+
"""
239+
219240

220241
def fetch_mdn():
221242
"""
@@ -468,6 +489,21 @@ def get_tag_pre_content(tags: TagsYaml, tag_name: str) -> Optional[str]:
468489
return tag.get("pre_content", None)
469490

470491

492+
def get_tag_render_options(
493+
tags: TagsYaml, tag_name: str
494+
) -> Optional[RenderOptions]:
495+
"""
496+
Return pre-content for the tag
497+
"""
498+
if tag_name not in tags:
499+
return None
500+
tag = tags[tag_name]
501+
if "render_options" in tag:
502+
return RenderOptions(**tag["render_options"])
503+
else:
504+
return None
505+
506+
471507
def make_mdn_link(tag: str) -> str:
472508
"""Generate an MDN docs link for the given tag"""
473509
return f"{MDN_ELEMENT_PAGE}/{tag}"
@@ -496,6 +532,7 @@ def elements_to_element_structs(
496532
escape_children=get_tag_escape_children(tag_attrs, name),
497533
attributes=attr_entries_to_object(tag_attrs, name),
498534
pre_content=get_tag_pre_content(tag_attrs, name),
535+
render_options=get_tag_render_options(tag_attrs, name),
499536
)
500537
)
501538

meta/tags.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ label:
9696

9797
p:
9898
base: StylableTag
99+
render_options:
100+
spacing: ""
99101

100102
br:
101103
base: SelfClosingTag

meta/templates/class_attrs_SelfClosingTag.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class {name}({base}):
88
"""
99
def __init__(
1010
self,
11-
*options: Options,
11+
*options: RenderOptions,
1212
{attr_args}
1313
**attributes: AttributeType,
1414
) -> None:
@@ -22,11 +22,11 @@ def __init__(
2222
attributes |= {
2323
{attr_unions}
2424
}
25-
super().__init__(**attributes)
25+
super().__init__(*options, **attributes)
2626

2727
def __call__( # type: ignore
2828
self,
29-
*options: Options,
29+
*options: RenderOptions,
3030
{attr_args}
3131
**attributes: AttributeType,
3232
):
@@ -40,7 +40,7 @@ def __call__( # type: ignore
4040
attributes |= {
4141
{attr_unions}
4242
}
43-
return super().__call__(**attributes)
43+
return super().__call__(*options, **attributes)
4444

4545
def _get_default_attributes(self, given: dict[str, AttributeType]) -> dict[str, AttributeType]:
4646
return {default_attrs}

meta/templates/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
88
https://creativecommons.org/licenses/by-sa/2.5/
99
"""
10-
from typing import Optional, Union, Literal
10+
from typing import Literal, Optional, Union
1111

12-
from ..__render_options import Options
13-
from ..__tag_base import Tag, SelfClosingTag, WhitespaceSensitiveTag
12+
from ..__render_options import RenderOptions
13+
from ..__tag_base import SelfClosingTag, Tag, WhitespaceSensitiveTag
1414
from ..__types import AttributeType, ChildrenType

0 commit comments

Comments
 (0)