From a441f8b5403acc24c2b1b420da1b45b8f316cb86 Mon Sep 17 00:00:00 2001 From: Jeremy Howard Date: Sat, 27 Apr 2024 04:21:52 +1000 Subject: [PATCH] hl --- 00_core.ipynb | 13 +++++++++++++ claudia/_modidx.py | 1 + claudia/core.py | 17 +++++++++++------ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/00_core.ipynb b/00_core.ipynb index 11d4db9..0c8b1e1 100644 --- a/00_core.ipynb +++ b/00_core.ipynb @@ -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, diff --git a/claudia/_modidx.py b/claudia/_modidx.py index 2f3c572..a59f6da 100644 --- a/claudia/_modidx.py +++ b/claudia/_modidx.py @@ -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'), diff --git a/claudia/core.py b/claudia/core.py index 8138426..ba8bb84 100644 --- a/claudia/core.py +++ b/claudia/core.py @@ -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 @@ -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): @@ -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): @@ -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)