From cd10e113a02e60724c1d7bd05f66158daf96ae6f Mon Sep 17 00:00:00 2001 From: ozpau Date: Thu, 6 Feb 2025 09:05:08 -0800 Subject: [PATCH] properly escape json and yaml on `nbdev_new` --- nbdev/cli.py | 4 ++-- nbdev/quarto.py | 4 ++-- nbs/api/13_cli.ipynb | 8 ++++---- nbs/api/14_quarto.ipynb | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/nbdev/cli.py b/nbdev/cli.py index c306677c..d03bfe16 100644 --- a/nbdev/cli.py +++ b/nbdev/cli.py @@ -27,7 +27,7 @@ from urllib.error import HTTPError from contextlib import redirect_stdout -import os, tarfile, sys +import os, tarfile, sys, json # %% auto 0 __all__ = ['mapping', 'nbdev_filter', 'extract_tgz', 'nbdev_new', 'nbdev_update_license', 'nb_export_cli', 'watch_export', @@ -66,7 +66,7 @@ def _render_nb(fn, cfg): "Render templated values like `{{lib_name}}` in notebook at `fn` from `cfg`" txt = fn.read_text() txt = txt.replace('from your_lib.core', f'from {cfg.lib_path}.core') # for compatibility with old templates - for k,v in cfg.d.items(): txt = txt.replace('{{'+k+'}}', v) + for k,v in cfg.d.items(): txt = txt.replace('{{'+k+'}}', json.dumps(v)[1:-1]) fn.write_text(txt) # %% ../nbs/api/13_cli.ipynb diff --git a/nbdev/quarto.py b/nbdev/quarto.py index 2c60764f..e38d1715 100644 --- a/nbdev/quarto.py +++ b/nbdev/quarto.py @@ -17,7 +17,7 @@ from fastcore.meta import delegates from .serve import proc_nbs,_proc_file from . import serve_drv -import yaml +import yaml, json # %% auto 0 __all__ = ['BASE_QUARTO_URL', 'install_quarto', 'install', 'IndentDumper', 'nbdev_sidebar', 'refresh_quarto_yml', @@ -179,7 +179,7 @@ def refresh_quarto_yml(): "Generate `_quarto.yml` from `settings.ini`." cfg = get_config() ny = cfg.nbs_path/'nbdev.yml' - vals = {k:cfg[k] for k in ['title', 'description', 'branch', 'git_url', 'doc_host', 'doc_baseurl']} + vals = {k:json.dumps(cfg[k])[1:-1] for k in ['title', 'description', 'branch', 'git_url', 'doc_host', 'doc_baseurl']} vals['doc_path'] = cfg.doc_path.name if 'title' not in vals: vals['title'] = vals['lib_name'] ny.write_text(_nbdev_yml.format(**vals)) diff --git a/nbs/api/13_cli.ipynb b/nbs/api/13_cli.ipynb index ac6f68b7..9326549a 100644 --- a/nbs/api/13_cli.ipynb +++ b/nbs/api/13_cli.ipynb @@ -52,7 +52,7 @@ "\n", "from urllib.error import HTTPError\n", "from contextlib import redirect_stdout\n", - "import os, tarfile, sys" + "import os, tarfile, sys, json" ] }, { @@ -152,7 +152,7 @@ " \"Render templated values like `{{lib_name}}` in notebook at `fn` from `cfg`\"\n", " txt = fn.read_text()\n", " txt = txt.replace('from your_lib.core', f'from {cfg.lib_path}.core') # for compatibility with old templates\n", - " for k,v in cfg.d.items(): txt = txt.replace('{{'+k+'}}', v)\n", + " for k,v in cfg.d.items(): txt = txt.replace('{{'+k+'}}', json.dumps(v)[1:-1])\n", " fn.write_text(txt)" ] }, @@ -372,7 +372,7 @@ { "cell_type": "code", "execution_count": null, - "id": "b50d27fb-c343-47e2-a008-7ebdd02e1356", + "id": "7b05d8d8-3323-4fa1-bf1c-04ec3d5dfae1", "metadata": {}, "outputs": [ { @@ -410,7 +410,7 @@ "\u001b[1m\u001b[94mnbdev_trust\u001b[22m\u001b[39m Trust notebooks matching `fname`\n", "\u001b[1m\u001b[94mnbdev_update\u001b[22m\u001b[39m Propagate change in modules matching `fname` to notebooks that created them\n", "\u001b[1m\u001b[94mnbdev_update_license\u001b[22m\u001b[39m Allows you to update the license of your project.\n", - "\u001b[1m\u001b[94mwatch_export\u001b[22m\u001b[39m None\n" + "\u001b[1m\u001b[94mwatch_export\u001b[22m\u001b[39m Use `nb_export` on ipynb files in `nbs` directory on changes using nbdev config if available\n" ] } ], diff --git a/nbs/api/14_quarto.ipynb b/nbs/api/14_quarto.ipynb index 7cfb1ed5..50f8246c 100644 --- a/nbs/api/14_quarto.ipynb +++ b/nbs/api/14_quarto.ipynb @@ -51,7 +51,7 @@ "from fastcore.meta import delegates\n", "from nbdev.serve import proc_nbs,_proc_file\n", "from nbdev import serve_drv\n", - "import yaml" + "import yaml, json" ] }, { @@ -326,7 +326,7 @@ " \"Generate `_quarto.yml` from `settings.ini`.\"\n", " cfg = get_config()\n", " ny = cfg.nbs_path/'nbdev.yml'\n", - " vals = {k:cfg[k] for k in ['title', 'description', 'branch', 'git_url', 'doc_host', 'doc_baseurl']}\n", + " vals = {k:json.dumps(cfg[k])[1:-1] for k in ['title', 'description', 'branch', 'git_url', 'doc_host', 'doc_baseurl']}\n", " vals['doc_path'] = cfg.doc_path.name\n", " if 'title' not in vals: vals['title'] = vals['lib_name']\n", " ny.write_text(_nbdev_yml.format(**vals))\n",