Skip to content

Commit 7c83a74

Browse files
committed
fixes #552
1 parent f90b4ca commit 7c83a74

File tree

4 files changed

+363
-50
lines changed

4 files changed

+363
-50
lines changed

fastcore/_modidx.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,13 @@
534534
'fastcore.xdg.xdg_data_home': ('xdg.html#xdg_data_home', 'fastcore/xdg.py'),
535535
'fastcore.xdg.xdg_runtime_dir': ('xdg.html#xdg_runtime_dir', 'fastcore/xdg.py'),
536536
'fastcore.xdg.xdg_state_home': ('xdg.html#xdg_state_home', 'fastcore/xdg.py')},
537-
'fastcore.xml': { 'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
537+
'fastcore.xml': { 'fastcore.xml.Checkbox': ('xml.html#checkbox', 'fastcore/xml.py'),
538+
'fastcore.xml.Hidden': ('xml.html#hidden', 'fastcore/xml.py'),
539+
'fastcore.xml.XT': ('xml.html#xt', 'fastcore/xml.py'),
540+
'fastcore.xml._attrmap': ('xml.html#_attrmap', 'fastcore/xml.py'),
541+
'fastcore.xml.fill_dataclass': ('xml.html#fill_dataclass', 'fastcore/xml.py'),
542+
'fastcore.xml.fill_form': ('xml.html#fill_form', 'fastcore/xml.py'),
543+
'fastcore.xml.set_val': ('xml.html#set_val', 'fastcore/xml.py'),
538544
'fastcore.xml.to_xml': ('xml.html#to_xml', 'fastcore/xml.py'),
539545
'fastcore.xml.xt': ('xml.html#xt', 'fastcore/xml.py')},
540546
'fastcore.xtras': { 'fastcore.xtras.ContextManagers': ('xtras.html#contextmanagers', 'fastcore/xtras.py'),

fastcore/xml.py

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
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
1314
from .utils import *
1415

16+
from dataclasses import dataclass, asdict
1517
import types
1618
from functools import partial
1719
from 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
2533
def 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

4352
for 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
4664
voids = 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
4967
def 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

Comments
 (0)