11# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.
22
33# %% auto 0
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' ]
4+ __all__ = ['named' , 'voids' , 'XT' , 'xt' , 'to_xml' , 'Html' , 'Head' , 'Title' , 'Meta' , 'Link' , 'Style' , 'Body' , 'Pre' , 'Code' , 'Div' ,
5+ 'Span' , 'P' , 'H1' , 'H2' , 'H3' , 'H4' , 'H5' , 'H6' , 'Strong' , 'Em' , 'B' , 'I' , 'U' , 'S' , 'Strike' , 'Sub' , 'Sup' ,
6+ 'Hr' , 'Br' , 'Img' , 'A' , 'Nav' , 'Ul' , 'Ol' , 'Li' , 'Dl' , 'Dt' , 'Dd' , 'Table' , 'Thead' , 'Tbody' , 'Tfoot' , 'Tr' ,
7+ 'Th' , 'Td' , 'Caption' , 'Col' , 'Colgroup' , 'Form' , 'Input' , 'Textarea' , 'Button' , 'Select' , 'Option' , 'Label' ,
8+ 'Fieldset' , 'Legend' , 'Details' , 'Summary' , 'Main' , 'Header' , 'Footer' , 'Section' , 'Article' , 'Aside' ,
9+ 'Figure' , 'Figcaption' , 'Mark' , 'Small' , 'Iframe' , 'Object' , 'Embed' , 'Param' , 'Video' , 'Audio' , 'Source' ,
10+ 'Canvas' , 'Svg' , 'Math' , 'Script' , 'Noscript' , 'Template' , 'Slot' ]
1211
1312# %% ../nbs/11_xml.ipynb 2
1413from .utils import *
@@ -33,7 +32,7 @@ class XT(list): patch
3332def xt (tag :str , * c , ** kw ):
3433 "Create an XML tag structure `[tag,children,attrs]` for `toxml()`"
3534 if len (c )== 1 and isinstance (c [0 ], types .GeneratorType ): c = tuple (c [0 ])
36- kw = {_attrmap (k ):str (v ) for k ,v in kw .items ()}
35+ kw = {_attrmap (k ):str (v ) for k ,v in kw .items () if v is not None }
3736 if tag in named and 'id' in kw and 'name' not in kw : kw ['name' ] = kw ['id' ]
3837 return XT ([tag .lower (),c ,kw ])
3938
@@ -52,18 +51,9 @@ def xt(tag:str, *c, **kw):
5251for o in _all_ : _g [o ] = partial (xt , o .lower ())
5352
5453# %% ../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
6454voids = set ('area base br col command embed hr img input keygen link meta param source track wbr' .split ())
6555
66- # %% ../nbs/11_xml.ipynb 16
56+ # %% ../nbs/11_xml.ipynb 14
6757def to_xml (elm , lvl = 0 ):
6858 "Convert `xt` element tree into an XML string"
6959 if isinstance (elm , tuple ): return '\n ' .join (to_xml (o ) for o in elm )
@@ -86,25 +76,9 @@ def to_xml(elm, lvl=0):
8676 if tag not in voids : res += f'{ sp } { cltag } \n '
8777 return res
8878
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
79+ # %% ../nbs/11_xml.ipynb 16
80+ @patch
81+ def _repr_markdown_ (self :XT ):
82+ try : from IPython import display
83+ except ImportError : return repr (self )
84+ return f'```html\n { to_xml (self )} \n ```'
0 commit comments