11# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.
22
33# %% auto 0
4- __all__ = ['voids' , 'xt' , 'to_xml' , 'Html' , 'Head' , 'Title' , 'Meta' , 'Link' , 'Style' , 'Body' , 'Pre' , 'Code' , 'Div' , 'Span' , 'P' ,
5- 'H1' , 'H2' , 'H3' , 'H4' , 'H5' , 'H6' , 'Strong' , 'Em' , 'B' , 'I' , 'U' , 'S' , 'Strike' , 'Sub' , 'Sup' , 'Hr' , 'Br' ,
6- 'Img' , 'A' , 'Nav' , 'Ul' , 'Ol' , 'Li' , 'Dl' , 'Dt' , 'Dd' , 'Table' , 'Thead' , 'Tbody' , 'Tfoot' , 'Tr' , 'Th' , 'Td' ,
7- 'Caption' , 'Col' , 'Colgroup' , 'Form' , 'Input' , 'Textarea' , 'Button' , 'Select' , 'Option' , 'Label' , 'Fieldset' ,
8- 'Legend' , 'Details' , 'Summary' , 'Main' , 'Header' , 'Footer' , 'Section' , 'Article' , 'Aside' , 'Figure' ,
9- 'Figcaption' , 'Mark' , 'Small' , 'Iframe' , 'Object' , 'Embed' , 'Param' , 'Video' , 'Audio' , 'Source' , 'Canvas' ,
10- 'Svg' , 'Math' , 'Script' , 'Noscript' , 'Template' , 'Slot' ]
4+ __all__ = ['named' , 'voids' , 'XT' , 'xt' , 'Checkbox' , 'Hidden' , 'to_xml' , 'set_val' , 'fill_form' , 'fill_dataclass' , 'Html' , 'Head' ,
5+ 'Title' , 'Meta' , 'Link' , 'Style' , 'Body' , 'Pre' , 'Code' , 'Div' , 'Span' , 'P' , 'H1' , 'H2' , 'H3' , 'H4' , 'H5' ,
6+ 'H6' , 'Strong' , 'Em' , 'B' , 'I' , 'U' , 'S' , 'Strike' , 'Sub' , 'Sup' , 'Hr' , 'Br' , 'Img' , 'A' , 'Nav' , 'Ul' , 'Ol' ,
7+ 'Li' , 'Dl' , 'Dt' , 'Dd' , 'Table' , 'Thead' , 'Tbody' , 'Tfoot' , 'Tr' , 'Th' , 'Td' , 'Caption' , 'Col' , 'Colgroup' ,
8+ 'Form' , 'Input' , 'Textarea' , 'Button' , 'Select' , 'Option' , 'Label' , 'Fieldset' , 'Legend' , 'Details' ,
9+ 'Summary' , 'Main' , 'Header' , 'Footer' , 'Section' , 'Article' , 'Aside' , 'Figure' , 'Figcaption' , 'Mark' ,
10+ 'Small' , 'Iframe' , 'Object' , 'Embed' , 'Param' , 'Video' , 'Audio' , 'Source' , 'Canvas' , 'Svg' , 'Math' , 'Script' ,
11+ 'Noscript' , 'Template' , 'Slot' ]
1112
1213# %% ../nbs/11_xml.ipynb 2
1314from .utils import *
1415
16+ from dataclasses import dataclass , asdict
1517import types
1618from functools import partial
1719from html import escape
@@ -22,13 +24,20 @@ def _attrmap(o):
2224 return o .lstrip ('_' ).replace ('_' , '-' )
2325
2426# %% ../nbs/11_xml.ipynb 5
27+ named = set ('a button form frame iframe img input map meta object param select textarea' .split ())
28+
29+ # %% ../nbs/11_xml.ipynb 6
30+ class XT (list ): patch
31+
32+ # %% ../nbs/11_xml.ipynb 7
2533def xt (tag :str , * c , ** kw ):
2634 "Create an XML tag structure `[tag,children,attrs]` for `toxml()`"
2735 if len (c )== 1 and isinstance (c [0 ], types .GeneratorType ): c = tuple (c [0 ])
2836 kw = {_attrmap (k ):str (v ) for k ,v in kw .items ()}
29- return [tag .lower (),c ,kw ]
37+ if tag in named and 'id' in kw and 'name' not in kw : kw ['name' ] = kw ['id' ]
38+ return XT ([tag .lower (),c ,kw ])
3039
31- # %% ../nbs/11_xml.ipynb 6
40+ # %% ../nbs/11_xml.ipynb 8
3241_g = globals ()
3342_all_ = ['Html' , 'Head' , 'Title' , 'Meta' , 'Link' , 'Style' , 'Body' , 'Pre' , 'Code' ,
3443'Div' , 'Span' , 'P' , 'H1' , 'H2' , 'H3' , 'H4' , 'H5' , 'H6' , 'Strong' , 'Em' , 'B' ,
@@ -42,10 +51,19 @@ def xt(tag:str, *c, **kw):
4251
4352for o in _all_ : _g [o ] = partial (xt , o .lower ())
4453
45- # %% ../nbs/11_xml.ipynb 9
54+ # %% ../nbs/11_xml.ipynb 13
55+ def Checkbox (value :bool = False , ** kw ):
56+ checked = {"checked" :"1" } if value else {}
57+ return Input (type = "checkbox" , ** checked , ** kw )
58+
59+ # %% ../nbs/11_xml.ipynb 14
60+ def Hidden (value :str = "" , ** kw ):
61+ return Input (type = "hidden" , value = value , ** kw )
62+
63+ # %% ../nbs/11_xml.ipynb 15
4664voids = set ('area base br col command embed hr img input keygen link meta param source track wbr' .split ())
4765
48- # %% ../nbs/11_xml.ipynb 10
66+ # %% ../nbs/11_xml.ipynb 16
4967def to_xml (elm , lvl = 0 ):
5068 "Convert `xt` element tree into an XML string"
5169 if isinstance (elm , tuple ): return '\n ' .join (to_xml (o ) for o in elm )
@@ -67,3 +85,26 @@ def to_xml(elm, lvl=0):
6785 res += '' .join (to_xml (c , lvl = lvl + 2 ) for c in cs )
6886 if tag not in voids : res += f'{ sp } { cltag } \n '
6987 return res
88+
89+ # %% ../nbs/11_xml.ipynb 21
90+ def set_val (tag , attr , val ):
91+ if attr .get ('type' , '' ) in ('checkbox' ,'radio' ):
92+ if val : attr ['checked' ] = '1'
93+ else : attr .pop ('checked' , '' )
94+ else : attr ['value' ] = val
95+
96+ # %% ../nbs/11_xml.ipynb 22
97+ def fill_form (form , obj ):
98+ "Modifies form in-place and returns it"
99+ inps = {attrs ['id' ]:(tag ,attrs ) for tag ,c ,attrs in form [1 ] if 'id' in attrs }
100+ for nm ,val in asdict (obj ).items ():
101+ if nm in inps :
102+ tag ,attr = inps [nm ]
103+ set_val (tag , attr , val )
104+ return form
105+
106+ # %% ../nbs/11_xml.ipynb 24
107+ def fill_dataclass (src , dest ):
108+ "Modifies dataclass in-place and returns it"
109+ for nm ,val in asdict (src ).items (): setattr (dest , nm , val )
110+ return dest
0 commit comments