Skip to content

Commit

Permalink
Support default property values
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Sep 29, 2023
1 parent 3f7de9e commit 98aafc7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion meta/generate_tag_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
prop_unions_gen = []
for prop in tag.properties:
# Yucky hard-coded spaces, I can't be bothered to fix this
prop_args_gen.append(f" {prop.name}: Any = None,")
prop_args_gen.append(f" {prop.name}: Any = {prop.default!r},")
prop_unions_gen.append(f" '{prop.name}': {prop.name},")

prop_args = '\n'.join(prop_args_gen).strip()
Expand Down
33 changes: 28 additions & 5 deletions meta/scrape_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,23 @@ def domXrefReplace(lookup: str, presentation: Optional[str] = None) -> str:
"""Type definition for info grabbed from MDN docs"""


class PropYmlItem(TypedDict):
"""
Properties of a tag, defined in tags.yml
"""

doc: str
"""Documentation for the property"""

default: NotRequired[str]
"""Default value of the property"""


class TagsYmlItem(TypedDict):
"""
A tag which has suggested keys
"""
properties: NotRequired[dict[str, str]]
properties: NotRequired[dict[str, str | PropYmlItem]]
"""Mapping of properties used by the tag (name: description)"""

base: NotRequired[str]
Expand All @@ -127,9 +139,14 @@ class Prop:
Name of the property
"""

description: Optional[str]
doc: Optional[str]
"""
Description of the property if applicable
Documentation of the property if applicable
"""

default: Optional[str]
"""
Default value for the property
"""


Expand Down Expand Up @@ -347,8 +364,14 @@ def prop_entries_to_object(
return []

props = []
for name, description in tag_data['properties'].items():
props.append(Prop(name, description))
for name, value in tag_data['properties'].items():
if isinstance(value, str):
doc: Optional[str] = value
default: Optional[str] = None
else:
doc = value.get("doc")
default = value.get("default")
props.append(Prop(name, doc, default))
return props


Expand Down
6 changes: 6 additions & 0 deletions meta/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ a:
href: URL of page to link to
target: Use "_blank" to open in a new tab

script:
properties:
type:
doc: Type of script to use
default: text/javascript

p:
base: StylableTag

Expand Down
8 changes: 4 additions & 4 deletions pyhtml/__tags/generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -3340,7 +3340,7 @@ class script(Tag):
def __init__(
self,
*children: Any,

type: Any = 'text/javascript',
**properties: Any,
) -> None:
"""
Expand All @@ -3349,14 +3349,14 @@ def __init__(
[View full documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script)
"""
properties |= {

'type': type,
}
super().__init__(*children, **properties)

def __call__(
self,
*children: Any,

type: Any = 'text/javascript',
**properties: Any,
):
"""
Expand All @@ -3365,7 +3365,7 @@ def __call__(
[View full documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script)
"""
properties |= {

'type': type,
}
return super().__call__(*children, **properties)

Expand Down
3 changes: 2 additions & 1 deletion tests/basic_rendering_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def test_larger_page():
' <title>',
' Hello, world!',
' </title>',
' <script src="http://example.com/script.js"></script>',
' <script src="http://example.com/script.js" '
'type="text/javascript"></script>',
' </head>',
' <body>',
' <h1>',
Expand Down

0 comments on commit 98aafc7

Please sign in to comment.