-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
826 additions
and
498 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
__version__ = "0.0.2" | ||
from .core import * | ||
from .helpers import * | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# AUTOGENERATED! DO NOT EDIT! File to edit: ../helpers.ipynb. | ||
|
||
# %% auto 0 | ||
__all__ = ['g', 'tags', 'doctype', 'xt', 'hl_md', 'to_xml', 'json_to_xml', 'mk_doctype'] | ||
|
||
# %% ../helpers.ipynb 3 | ||
import hashlib,xml.etree.ElementTree as ET | ||
from collections import namedtuple | ||
|
||
from .core import * | ||
from fastcore.utils import * | ||
from IPython import display | ||
|
||
# %% ../helpers.ipynb 6 | ||
def xt(tag:str, # XML tag name | ||
c:Optional[list]=None, # Children | ||
**kw): | ||
"Helper to create appropriate data structure for `to_xml`." | ||
kw = {k.lstrip('_'):str(v) for k,v in kw.items()} | ||
return tag,c,kw | ||
|
||
# %% ../helpers.ipynb 9 | ||
g = globals() | ||
tags = 'div img h1 h2 h3 h4 h5 p hr span html'.split() | ||
for o in tags: g[o] = partial(xt, o) | ||
|
||
# %% ../helpers.ipynb 12 | ||
def hl_md(s, lang='xml'): | ||
"Syntax highlight `s` using `lang`." | ||
if display: return display.Markdown(f'```{lang}\n{s}\n```') | ||
print(s) | ||
|
||
# %% ../helpers.ipynb 15 | ||
def to_xml(node:tuple, # XML structure in `xt` format | ||
hl=False # Syntax highlight response? | ||
): | ||
"Convert `node` to an XML string." | ||
def mk_el(tag, cs, attrs): | ||
el = ET.Element(tag, attrib=attrs) | ||
if isinstance(cs, list): el.extend([mk_el(*o) for o in cs]) | ||
elif cs is not None: el.text = str(cs) | ||
return el | ||
|
||
root = mk_el(*node) | ||
ET.indent(root, space=' ' if hl else '') | ||
res = ET.tostring(root, encoding='unicode') | ||
return hl_md(res) if hl else res | ||
|
||
# %% ../helpers.ipynb 18 | ||
def json_to_xml(d:dict, # JSON dictionary to convert | ||
rnm:str # Root name | ||
)->str: | ||
"Convert `d` to XML." | ||
root = ET.Element(rnm) | ||
def build_xml(data, parent): | ||
if isinstance(data, dict): | ||
for key, value in data.items(): build_xml(value, ET.SubElement(parent, key)) | ||
elif isinstance(data, list): | ||
for item in data: build_xml(item, ET.SubElement(parent, 'item')) | ||
else: parent.text = str(data) | ||
build_xml(d, root) | ||
ET.indent(root) | ||
return ET.tostring(root, encoding='unicode') | ||
|
||
# %% ../helpers.ipynb 23 | ||
doctype = namedtuple('doctype', ['source', 'content']) | ||
|
||
# %% ../helpers.ipynb 26 | ||
def mk_doctype(content:str, # The document content | ||
source:Optional[str]=None # URL, filename, etc; defaults to `md5(content)` if not provided | ||
) -> namedtuple: | ||
"Create a `doctype` named tuple" | ||
if source is None: source = hashlib.md5(content.encode()).hexdigest()[:8] | ||
return doctype(add_nls(str(source).strip()), add_nls(content.strip())) |
Oops, something went wrong.