Skip to content

Commit

Permalink
Merge branch 'master' into fixing-to-xml-bug-while-converting-ft
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 authored Aug 11, 2024
2 parents 586bf97 + 92932e6 commit cde87a7
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 29 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

<!-- do not remove -->

## 1.6.7

### New Features

- Do not update `-` to `_` if special chars present in `ft` ([#601](https://github.com/fastai/fastcore/issues/601))


## 1.6.6

### New Features

- Add `FT.get(k,default)` ([#600](https://github.com/fastai/fastcore/issues/600))
- Add `Safe` str and return it from `to_xml` ([#599](https://github.com/fastai/fastcore/issues/599))
- Feat: add an `indent` flag for `to_xml()` ([#594](https://github.com/fastai/fastcore/pull/594)), thanks to [@phact](https://github.com/phact)


## 1.6.5

### New Features
Expand Down
2 changes: 1 addition & 1 deletion fastcore/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.6.6"
__version__ = "1.6.8"
4 changes: 4 additions & 0 deletions fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,16 @@
'fastcore.xml.FT.__setattr__': ('xml.html#ft.__setattr__', 'fastcore/xml.py'),
'fastcore.xml.FT.attrs': ('xml.html#ft.attrs', 'fastcore/xml.py'),
'fastcore.xml.FT.children': ('xml.html#ft.children', 'fastcore/xml.py'),
'fastcore.xml.FT.get': ('xml.html#ft.get', 'fastcore/xml.py'),
'fastcore.xml.FT.tag': ('xml.html#ft.tag', 'fastcore/xml.py'),
'fastcore.xml.Html': ('xml.html#html', 'fastcore/xml.py'),
'fastcore.xml.Safe': ('xml.html#safe', 'fastcore/xml.py'),
'fastcore.xml.Safe.__html__': ('xml.html#safe.__html__', 'fastcore/xml.py'),
'fastcore.xml.__getattr__': ('xml.html#__getattr__', 'fastcore/xml.py'),
'fastcore.xml._escape': ('xml.html#_escape', 'fastcore/xml.py'),
'fastcore.xml._preproc': ('xml.html#_preproc', 'fastcore/xml.py'),
'fastcore.xml._to_attr': ('xml.html#_to_attr', 'fastcore/xml.py'),
'fastcore.xml._to_xml': ('xml.html#_to_xml', 'fastcore/xml.py'),
'fastcore.xml.attrmap': ('xml.html#attrmap', 'fastcore/xml.py'),
'fastcore.xml.ft': ('xml.html#ft', 'fastcore/xml.py'),
'fastcore.xml.highlight': ('xml.html#highlight', 'fastcore/xml.py'),
Expand Down
34 changes: 23 additions & 11 deletions fastcore/xml.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/11_xml.ipynb.

# %% auto 0
__all__ = ['voids', 'FT', 'attrmap', 'valmap', 'ft', 'Html', 'to_xml', 'highlight', 'showtags', 'Head', 'Title', 'Meta', 'Link',
'Style', 'Body', 'Pre', 'Code', 'Div', 'Span', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'Strong', 'Em', 'B',
'I', 'U', 'S', 'Strike', 'Sub', 'Sup', 'Hr', 'Br', 'Img', 'A', 'Nav', 'Ul', 'Ol', 'Li', 'Dl', 'Dt', 'Dd',
'Table', 'Thead', 'Tbody', 'Tfoot', 'Tr', 'Th', 'Td', 'Caption', 'Col', 'Colgroup', 'Form', 'Input',
'Textarea', 'Button', 'Select', 'Option', 'Label', 'Fieldset', 'Legend', 'Details', 'Summary', 'Main',
'Header', 'Footer', 'Section', 'Article', 'Aside', 'Figure', 'Figcaption', 'Mark', 'Small', 'Iframe',
__all__ = ['voids', 'FT', 'attrmap', 'valmap', 'ft', 'Html', 'Safe', 'to_xml', 'highlight', 'showtags', 'Head', 'Title', 'Meta',
'Link', 'Style', 'Body', 'Pre', 'Code', 'Div', 'Span', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'Strong',
'Em', 'B', 'I', 'U', 'S', 'Strike', 'Sub', 'Sup', 'Hr', 'Br', 'Img', 'A', 'Nav', 'Ul', 'Ol', 'Li', 'Dl',
'Dt', 'Dd', 'Table', 'Thead', 'Tbody', 'Tfoot', 'Tr', 'Th', 'Td', 'Caption', 'Col', 'Colgroup', 'Form',
'Input', 'Textarea', 'Button', 'Select', 'Option', 'Label', 'Fieldset', 'Legend', 'Details', 'Summary',
'Main', 'Header', 'Footer', 'Section', 'Article', 'Aside', 'Figure', 'Figcaption', 'Mark', 'Small', 'Iframe',
'Object', 'Embed', 'Param', 'Video', 'Audio', 'Source', 'Canvas', 'Svg', 'Math', 'Script', 'Noscript',
'Template', 'Slot']

Expand Down Expand Up @@ -41,11 +41,15 @@ def __setattr__(self, k, v):

def __getattr__(self, k):
if k.startswith('__') or k not in self.attrs: raise AttributeError(k)
return self.attrs[k.lstrip('_').replace('_', '-')]
return self.get(k)

def get(self, k, default=None): return self[2].get(k.lstrip('_').replace('_', '-'), default)

# %% ../nbs/11_xml.ipynb
_specials = set('@.-!~:[](){}$%^&*+=|/?<>,`')

def attrmap(o):
if o=='_': return o
if o=='_' or (_specials & set(o)): return o
o = dict(htmlClass='class', cls='class', _class='class', klass='class',
_for='for', fr='for', htmlFor='for').get(o, o)
return o.lstrip('_').replace('_', '-')
Expand Down Expand Up @@ -88,6 +92,10 @@ def Html(*c, doctype=True, **kwargs)->FT:
if not doctype: return res
return (ft('!DOCTYPE', html=True, void_=True), res)

# %% ../nbs/11_xml.ipynb
class Safe(str):
def __html__(self): return self

# %% ../nbs/11_xml.ipynb
def _escape(s): return '' if s is None else s.__html__() if hasattr(s, '__html__') else escape(s) if isinstance(s, str) else s

Expand All @@ -104,13 +112,13 @@ def _to_attr(k,v):
return f'{k}={qt}{v}{qt}'

# %% ../nbs/11_xml.ipynb
def to_xml(elm, lvl=0, indent:bool=True):
"Convert `ft` element tree into an XML string"
def _to_xml(elm, lvl, indent:bool):
nl = '\n'
if not indent: lvl,nl = 0,''
if elm is None: return ''
if hasattr(elm, '__ft__'): elm = elm.__ft__()
if isinstance(elm, tuple): return f'{nl}'.join(to_xml(o, indent=indent) for o in elm)
if isinstance(elm, bytes): return elm.decode('utf-8')
sp = ' ' * lvl
if not isinstance(elm, list): return f'{_escape(elm)}{nl}'

Expand All @@ -128,7 +136,11 @@ def to_xml(elm, lvl=0, indent:bool=True):
res = f'{sp}<{stag}>{nl}'
res += ''.join(to_xml(c, lvl=lvl+2, indent=indent) for c in cs)
if not isvoid: res += f'{sp}{cltag}{nl}'
return res
return Safe(res)

def to_xml(elm, lvl=0, indent:bool=True):
"Convert `ft` element tree into an XML string"
return Safe(_to_xml(elm, lvl, indent))

FT.__html__ = to_xml

Expand Down
4 changes: 2 additions & 2 deletions nbs/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5308,7 +5308,7 @@
"@patch_to(_T3)\n",
"def func1(self, a): return self+a\n",
"\n",
"t = _T3(1) # we initilized `t` to a type int = 1\n",
"t = _T3(1) # we initialized `t` to a type int = 1\n",
"test_eq(t.func1(2), 3) # we add 2 to `t`, so 2 + 1 = 3"
]
},
Expand All @@ -5331,7 +5331,7 @@
"@patch_to(_T4)\n",
"def greet(self, x): return self.g + x\n",
" \n",
"t = _T4('hello ') # this sets self.g = 'helllo '\n",
"t = _T4('hello ') # this sets self.g = 'hello '\n",
"test_eq(t.greet('world'), 'hello world') #t.greet('world') will append 'world' to 'hello '"
]
},
Expand Down
45 changes: 31 additions & 14 deletions nbs/11_xml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@
"\n",
" def __getattr__(self, k):\n",
" if k.startswith('__') or k not in self.attrs: raise AttributeError(k)\n",
" return self.attrs[k.lstrip('_').replace('_', '-')]"
" return self.get(k)\n",
"\n",
" def get(self, k, default=None): return self[2].get(k.lstrip('_').replace('_', '-'), default)"
]
},
{
Expand All @@ -88,8 +90,10 @@
"outputs": [],
"source": [
"#| export\n",
"_specials = set('@.-!~:[](){}$%^&*+=|/?<>,`')\n",
"\n",
"def attrmap(o):\n",
" if o=='_': return o\n",
" if o=='_' or (_specials & set(o)): return o\n",
" o = dict(htmlClass='class', cls='class', _class='class', klass='class',\n",
" _for='for', fr='for', htmlFor='for').get(o, o)\n",
" return o.lstrip('_').replace('_', '-')"
Expand Down Expand Up @@ -266,7 +270,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"newid\n"
"newid newid missing\n"
]
},
{
Expand All @@ -282,10 +286,22 @@
],
"source": [
"elem.id = 'newid'\n",
"print(elem.id)\n",
"print(elem.id, elem.get('id'), elem.get('foo', 'missing'))\n",
"elem"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "116c886e",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"class Safe(str):\n",
" def __html__(self): return self"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -325,13 +341,13 @@
"outputs": [],
"source": [
"#| export\n",
"def to_xml(elm, lvl=0, indent:bool=True):\n",
" \"Convert `ft` element tree into an XML string\"\n",
"def _to_xml(elm, lvl, indent:bool):\n",
" nl = '\\n'\n",
" if not indent: lvl,nl = 0,''\n",
" if elm is None: return ''\n",
" if hasattr(elm, '__ft__'): elm = elm.__ft__()\n",
" if isinstance(elm, tuple): return f'{nl}'.join(to_xml(o, indent=indent) for o in elm)\n",
" if isinstance(elm, bytes): return elm.decode('utf-8')\n",
" sp = ' ' * lvl\n",
" if not isinstance(elm, list): return f'{_escape(elm)}{nl}'\n",
"\n",
Expand All @@ -349,7 +365,11 @@
" res = f'{sp}<{stag}>{nl}'\n",
" res += ''.join(to_xml(c, lvl=lvl+2, indent=indent) for c in cs)\n",
" if not isvoid: res += f'{sp}{cltag}{nl}'\n",
" return res\n",
" return Safe(res)\n",
"\n",
"def to_xml(elm, lvl=0, indent:bool=True):\n",
" \"Convert `ft` element tree into an XML string\"\n",
" return Safe(_to_xml(elm, lvl, indent))\n",
"\n",
"FT.__html__ = to_xml"
]
Expand Down Expand Up @@ -441,7 +461,7 @@
"id": "4713bd8d",
"metadata": {},
"source": [
"Interoperability both directions with Django and Jinja using the [__html__() protocol](https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.escape)."
"Interoperability both directions with Django and Jinja using the [__html__() protocol](https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.escape):"
]
},
{
Expand All @@ -464,14 +484,11 @@
}
],
"source": [
"class _SafeString(str):\n",
" def __html__(self): return self\n",
"\n",
"def _escape(s): return s.__html__() if hasattr(s, '__html__') else _SafeString(escape(s))\n",
"def _esc(s): return s.__html__() if hasattr(s, '__html__') else Safe(escape(s))\n",
"\n",
"r = _SafeString('<b>Hello from Django</b>')\n",
"r = Safe('<b>Hello from Django</b>')\n",
"print(to_xml(Div(r)))\n",
"print(_escape(Div(P('Hello from fastcore <3'))))"
"print(_esc(Div(P('Hello from fastcore <3'))))"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ author = Jeremy Howard and Sylvain Gugger
author_email = [email protected]
copyright = fast.ai
branch = master
version = 1.6.6
version = 1.6.8
min_python = 3.8
audience = Developers
language = English
Expand Down

0 comments on commit cde87a7

Please sign in to comment.