66
77It also embeds suggested kwargs for some elements, using info from tags.yml.
88"""
9- from dataclasses import dataclass
10- from typing import Optional , TypedDict , Any , Union
11- from typing_extensions import NotRequired
9+
10+ import sys
1211from collections .abc import Iterator
12+ from dataclasses import dataclass
13+ from typing import Any , Optional , TypedDict , Union
14+
1315import requests
1416import yaml
15- import sys
16-
17+ from typing_extensions import NotRequired
1718
1819TAGS_YAML = "meta/tags.yml"
1920"""File location to load custom tag data from"""
@@ -89,7 +90,7 @@ def domXrefReplace(lookup: str, presentation: Optional[str] = None) -> str:
8990
9091DESCRIPTION_LOOKUPS = {
9192 "htmlelement" : htmlElementReplace ,
92- "glossary" : glossaryReplace ,
93+ "glossary" : glossaryReplace ,
9394 "cssxref" : cssXrefReplace ,
9495 "domxref" : domXrefReplace ,
9596}
@@ -121,6 +122,7 @@ class TagsYmlItem(TypedDict):
121122 """
122123 A tag which has suggested keys
123124 """
125+
124126 skip : NotRequired [bool ]
125127 """Whether to skip this tag when generating tags"""
126128
@@ -233,7 +235,7 @@ def handle_header_elements(description) -> list[TagMdnInfo]:
233235 tags = []
234236
235237 for i in range (6 ):
236- tags .append ((f"h{ i + 1 } " , description ))
238+ tags .append ((f"h{ i + 1 } " , description ))
237239
238240 return tags
239241
@@ -254,7 +256,7 @@ def format_description(description: str, ele: str) -> str:
254256 while (start := description .find ("{{" )) != - 1 :
255257 end = description .find ("}}" , start )
256258
257- element_text = description [start + 2 : end ]
259+ element_text = description [start + 2 : end ]
258260 # In format key("arg1", "arg2")
259261
260262 key , args = element_text .split ("(" )
@@ -283,7 +285,7 @@ def format_description(description: str, ele: str) -> str:
283285 description = (
284286 description [:start ]
285287 + DESCRIPTION_LOOKUPS [key .lower ()](lookup , presentation )
286- + description [end + 2 :]
288+ + description [end + 2 :]
287289 )
288290
289291 return description
@@ -295,13 +297,13 @@ def parse_markdown_table(lines: Iterator[str]) -> list[TagMdnInfo]:
295297 """
296298
297299 # Read in header row
298- assert next (lines ).startswith (' | ---' )
300+ assert next (lines ).startswith (" | ---" )
299301
300302 # Now grab each line
301303 tags : list [TagMdnInfo ] = []
302304 try :
303- while (line := next (lines )).startswith ('|' ):
304- _ , tag_base , description , _ = line .split ('|' )
305+ while (line := next (lines )).startswith ("|" ):
306+ _ , tag_base , description , _ = line .split ("|" )
305307
306308 tag_base = tag_base .strip ()
307309
@@ -310,9 +312,9 @@ def parse_markdown_table(lines: Iterator[str]) -> list[TagMdnInfo]:
310312 tags .extend (handle_header_elements (description ))
311313 continue
312314
313- tag_name = tag_base \
314- . removeprefix ( '{{HTMLElement("' )\
315- . removesuffix ( '")}}' )
315+ tag_name = tag_base . removeprefix ( '{{HTMLElement("' ). removesuffix (
316+ '")}}'
317+ )
316318
317319 description = format_description (description .strip (), tag_name )
318320
@@ -350,7 +352,7 @@ def parse_markdown(lines: Iterator[str]) -> list[TagMdnInfo]:
350352 print ("Skip obsolete tags" , file = sys .stderr )
351353 break
352354
353- if line .replace (' ' , '' ) == ' |Element|Description|' :
355+ if line .replace (" " , "" ) == " |Element|Description|" :
354356 # Start of table
355357 tags .extend (parse_markdown_table (lines ))
356358
@@ -390,23 +392,20 @@ def attr_entries_to_object(tags: TagsYaml, tag_name: str) -> list[Attr]:
390392
391393 tag_data = tags [tag_name ]
392394
393- if ' attributes' not in tag_data :
395+ if " attributes" not in tag_data :
394396 return []
395397
396398 attrs = []
397- for name , value in tag_data [' attributes' ].items ():
399+ for name , value in tag_data [" attributes" ].items ():
398400 if isinstance (value , str ):
399401 doc : Optional [str ] = value
400402 default : Optional [str ] = None
401403 type = "AttributeType"
402404 else :
403405 doc = value .get ("doc" )
404- if "default" in value :
405- # NOTE: This is safe, as it is only ever run at compile time
406- # This code is not distributed to users' systems
407- default = eval (value ["default" ])
408- else :
409- default = None
406+ # NOTE: This is safe, as it is only ever run at compile time
407+ # This code is not distributed to users' systems
408+ default = eval (value ["default" ]) if "default" in value else None
410409 type = value .get ("type" , "AttributeType" )
411410 attrs .append (Attr (name , doc , type , default ))
412411 return attrs
@@ -423,7 +422,7 @@ def get_tag_rename(tags: TagsYaml, tag_name: str) -> str:
423422 if "rename" not in tag :
424423 return tag_name
425424 else :
426- return tag [' rename' ]
425+ return tag [" rename" ]
427426
428427
429428def get_tag_base_class (tags : TagsYaml , tag_name : str ) -> str :
@@ -436,7 +435,7 @@ def get_tag_base_class(tags: TagsYaml, tag_name: str) -> str:
436435 if "base" not in tag :
437436 return "Tag"
438437 else :
439- return tag [' base' ]
438+ return tag [" base" ]
440439
441440
442441def get_tag_skip (tags : TagsYaml , tag_name : str ) -> bool :
@@ -446,7 +445,7 @@ def get_tag_skip(tags: TagsYaml, tag_name: str) -> bool:
446445 if tag_name not in tags :
447446 return False
448447 tag = tags [tag_name ]
449- return tag .get (' skip' , False )
448+ return tag .get (" skip" , False )
450449
451450
452451def get_tag_escape_children (tags : TagsYaml , tag_name : str ) -> bool :
@@ -456,7 +455,7 @@ def get_tag_escape_children(tags: TagsYaml, tag_name: str) -> bool:
456455 if tag_name not in tags :
457456 return True
458457 tag = tags [tag_name ]
459- return tag .get (' escape_children' , True )
458+ return tag .get (" escape_children" , True )
460459
461460
462461def get_tag_pre_content (tags : TagsYaml , tag_name : str ) -> Optional [str ]:
@@ -466,7 +465,7 @@ def get_tag_pre_content(tags: TagsYaml, tag_name: str) -> Optional[str]:
466465 if tag_name not in tags :
467466 return None
468467 tag = tags [tag_name ]
469- return tag .get (' pre_content' , None )
468+ return tag .get (" pre_content" , None )
470469
471470
472471def make_mdn_link (tag : str ) -> str :
@@ -488,15 +487,17 @@ def elements_to_element_structs(
488487 if get_tag_skip (tag_attrs , name ):
489488 continue
490489
491- output .append (TagInfo (
492- name = get_tag_rename (tag_attrs , name ),
493- description = description ,
494- base = get_tag_base_class (tag_attrs , name ),
495- mdn_link = make_mdn_link (name ),
496- escape_children = get_tag_escape_children (tag_attrs , name ),
497- attributes = attr_entries_to_object (tag_attrs , name ),
498- pre_content = get_tag_pre_content (tag_attrs , name )
499- ))
490+ output .append (
491+ TagInfo (
492+ name = get_tag_rename (tag_attrs , name ),
493+ description = description ,
494+ base = get_tag_base_class (tag_attrs , name ),
495+ mdn_link = make_mdn_link (name ),
496+ escape_children = get_tag_escape_children (tag_attrs , name ),
497+ attributes = attr_entries_to_object (tag_attrs , name ),
498+ pre_content = get_tag_pre_content (tag_attrs , name ),
499+ )
500+ )
500501
501502 return output
502503
@@ -523,9 +524,9 @@ def print_elements(parsed: list[TagInfo]):
523524 print ()
524525 print (ele .attributes )
525526 print ()
526- print (' ------------------' )
527+ print (" ------------------" )
527528 print ()
528529
529530
530- if __name__ == ' __main__' :
531+ if __name__ == " __main__" :
531532 print_elements (main ())
0 commit comments