{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "d35ddef5", "metadata": {}, "outputs": [], "source": [ "from fastcore.all import *\n", "from execnb.nbio import *" ] }, { "cell_type": "code", "execution_count": null, "id": "891eb357", "metadata": {}, "outputs": [], "source": [ "from bs4 import BeautifulSoup\n", "from bs4.formatter import HTMLFormatter" ] }, { "cell_type": "code", "execution_count": null, "id": "8f3176d9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Natural'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = BeautifulSoup('hi \"Natural\"')\n", "im = b.img\n", "im['alt']" ] }, { "cell_type": "code", "execution_count": null, "id": "9fa51767", "metadata": {}, "outputs": [], "source": [ "def img2md(im):\n", " r = '!['\n", " a = im.get('caption', None)\n", " if a: r+=a\n", " r+=f']({im[\"src\"]}'\n", " r+=')'\n", " d = {}\n", " a = im.get('width', None)\n", " if a: d['width'] = a\n", " a = im.get('id', None)\n", " if a: d['id'] = 'fig-'+re.sub('_', '-', a)\n", " a = im.get('alt', None)\n", " if a: d['fig-alt'] = a\n", " if d: r+='{' + ' '.join(f'{k}=\"{v}\"' for k,v in d.items()) + '}'\n", " return r" ] }, { "cell_type": "code", "execution_count": null, "id": "ac9430d1", "metadata": {}, "outputs": [], "source": [ "class UnsortedAttributes(HTMLFormatter):\n", " def attributes(self, tag): yield from tag.attrs.items()\n", "\n", "def enc(t): return t.encode_contents(formatter=UnsortedAttributes()).decode('utf-8')" ] }, { "cell_type": "code", "execution_count": null, "id": "232f3673", "metadata": {}, "outputs": [], "source": [ "def nb_images(nb):\n", " for c in nb.cells:\n", " o = c.source\n", " if c.cell_type=='markdown' and '>', fix_sec, o)\n", " o = re.sub(r'<<(\\w+)>>', fix_fig, o)\n", " c.source = o\n", " # print(o)" ] }, { "cell_type": "code", "execution_count": null, "id": "3b24c7a7", "metadata": {}, "outputs": [], "source": [ "def fix_lab(o): return '#| label: fig-' + re.sub('_', '-', o.group(1))" ] }, { "cell_type": "code", "execution_count": null, "id": "35df4b8f", "metadata": {}, "outputs": [], "source": [ "def nb_comput(nb):\n", " for c in nb.cells:\n", " o = c.source\n", " if c.cell_type=='code' and '#id' in o:\n", " o = re.sub(r'^#id +(\\w+)', fix_lab, o, flags=re.MULTILINE)\n", " o = re.sub(r'^#caption +(.+)', r'#| fig-cap: \"\\1\"', o, flags=re.MULTILINE)\n", " o = re.sub(r'^#alt +(.+)', r'#| fig-alt: \"\\1\"', o, flags=re.MULTILINE)\n", " c.source = o\n", " # print(o)" ] }, { "cell_type": "code", "execution_count": null, "id": "762e5dee", "metadata": {}, "outputs": [], "source": [ "def fix_nb(path):\n", " nb = read_nb(path)\n", " nb_images(nb)\n", " nb_xrefs(nb)\n", " nb_comput(nb)\n", " write_nb(nb, path)" ] }, { "cell_type": "code", "execution_count": null, "id": "5351c94f", "metadata": {}, "outputs": [], "source": [ "# fix_nb('../mnist_basics.ipynb')" ] }, { "cell_type": "code", "execution_count": null, "id": "591ffa9f", "metadata": {}, "outputs": [], "source": [ "# for path in Path('..').ls(file_exts=['.ipynb']): fix_nb(path)" ] }, { "cell_type": "code", "execution_count": null, "id": "68b7c95a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }