Skip to content

Commit

Permalink
hl
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Apr 26, 2024
1 parent a3c2c07 commit a441f8b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
13 changes: 13 additions & 0 deletions 00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@
"c = AnthClient(models[-1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f445e7a",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def hl_md(s, lang='xml'):\n",
" \"Syntax highlight `s` using `lang`\"\n",
" return Markdown(f'```{lang}\\n{s}\\n```')"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
1 change: 1 addition & 0 deletions claudia/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'claudia.core.AnthClient.stream': ('core.html#anthclient.stream', 'claudia/core.py'),
'claudia.core.contents': ('core.html#contents', 'claudia/core.py'),
'claudia.core.first_match': ('core.html#first_match', 'claudia/core.py'),
'claudia.core.hl_md': ('core.html#hl_md', 'claudia/core.py'),
'claudia.core.json_to_xml': ('core.html#json_to_xml', 'claudia/core.py'),
'claudia.core.last_match': ('core.html#last_match', 'claudia/core.py'),
'claudia.core.mk_msg': ('core.html#mk_msg', 'claudia/core.py'),
Expand Down
17 changes: 11 additions & 6 deletions claudia/core.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../00_core.ipynb.

# %% auto 0
__all__ = ['models', 'g', 'tags', 'mk_msg', 'mk_msgs', 'contents', 'AnthClient', 'to_xml', 'xt', 'json_to_xml', 'first_match',
'last_match']
__all__ = ['models', 'g', 'tags', 'mk_msg', 'mk_msgs', 'contents', 'AnthClient', 'hl_md', 'to_xml', 'xt', 'json_to_xml',
'first_match', 'last_match']

# %% ../00_core.ipynb 5
import xml.etree.ElementTree as ET, json
Expand Down Expand Up @@ -45,6 +45,11 @@ def stream(self, msgs, sp='', temp=0, maxtok=4096, stop=None):
yield from stream.text_stream

# %% ../00_core.ipynb 14
def hl_md(s, lang='xml'):
"Syntax highlight `s` using `lang`"
return Markdown(f'```{lang}\n{s}\n```')

# %% ../00_core.ipynb 15
def to_xml(node, hl=False):
"Convert `node` to an XML string"
def mk_el(tag, cs, attrs):
Expand All @@ -58,18 +63,18 @@ def mk_el(tag, cs, attrs):
res = ET.tostring(root, encoding='unicode')
return hl_md(res) if hl else res

# %% ../00_core.ipynb 15
# %% ../00_core.ipynb 16
def xt(tag, c=None, **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

# %% ../00_core.ipynb 16
# %% ../00_core.ipynb 17
g = globals()
tags = 'div','img','h1','h2','h3','h4','h5','p','hr','span','html'
for o in tags: g[o] = partial(xt, o)

# %% ../00_core.ipynb 19
# %% ../00_core.ipynb 20
def json_to_xml(d, rnm):
root = ET.Element(rnm)
def build_xml(data, parent):
Expand All @@ -82,7 +87,7 @@ def build_xml(data, parent):
ET.indent(root)
return ET.tostring(root, encoding='unicode')

# %% ../00_core.ipynb 20
# %% ../00_core.ipynb 21
def first_match(lst, f, default=None):
"First element of `lst` matching predicate `f`, or `default` if none"
return next((i for i,o in enumerate(lst) if f(o)), default)
Expand Down

0 comments on commit a441f8b

Please sign in to comment.