{ "cells": [ { "cell_type": "markdown", "metadata": { "isTOC": true }, "source": [ "***\n", "TOC\n", "***\n", "- [head](#head)\n", " - [1.1](#1.1)\n", " - [1.2](#1.2)\n", " - [1.2.1](#1.2.1)\n", "- [h1](#h1)\n", " - [h2](#h2)\n", " - [h3](#h3)\n", " - [h4](#h4)\n", " - [h5](#h5)\n", " - [h6](#h6)\n", "***\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To update TOC:\n", " - save the file\n", " - run the cell bellow\n", " - do one \n", " - try to save again (prompt will appear)\n", " - select revert\n", " - OR reload file from disk" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#configs\n", "ID='toc-1@3$'\n", "file_basename='sample-notebook'\n", "insert_toc_relative_to_this_cell=False\n", "insert_toc_at_cell_index=0\n", "\n", "# run to update TOC\n", "import json, re\n", "file_name=file_basename+'.ipynb'\n", "\n", "\n", "with open(file_name,'rb') as f:\n", " # read binary or encode err !!\n", " nb=json.load(f)\n", "\n", "TOC={\n", " 'cell_type':'markdown',\n", " 'metadata':{\n", " 'isTOC':True\n", " },\n", " 'source':[\n", " \"***\\n\",\n", " \"TOC\\n\",\n", " \"***\\n\",\n", " # here\n", " \"***\\n\",\n", " \"\\n\"\n", " ]\n", " }\n", "\n", "insert_at=3\n", "lines=[]\n", "this_cell=None\n", "TOC_cell=None\n", "for i in range(0,len(nb['cells'])):\n", " cell=nb['cells'][i]\n", " if cell['cell_type']=='markdown':\n", " if not TOC_cell and cell['metadata'].get('isTOC'):\n", " TOC_cell=i\n", " continue\n", " source=\"\".join(cell['source'])\n", " fhs=source.find('#')\n", " if fhs==-1: continue\n", " fhe=source.find('\\n',fhs)\n", " if fhe==-1: fhe=len(source)\n", "# replace chars/parts which do not appear in link #ids\n", " line=re.sub(r'([`])|(<.*?>)','',source[fhs:fhe]).split(' ')\n", " lines.append(\n", " f\"{' '*(len(line[0])-1)*2}\"+ # spaces\n", " f\"- [{' '.join(line[1:])}]\"+ # - [ link text ]\n", " f\"(#{'-'.join(line[1:])})\\n\") # (#link-location)\n", " elif not this_cell and cell['cell_type']=='code':\n", " source=\"\".join(cell['source'])\n", " if source.find(ID)!=-1:\n", " this_cell=i\n", "\n", "TOC['source'][insert_at:insert_at]=lines\n", "# print(TOC_cell)\n", "if TOC_cell != None:\n", " nb['cells'].pop(TOC_cell);\n", " if this_cell>=TOC_cell:this_cell-=1\n", " \n", "TOC_cell=insert_toc_at_cell_index\n", "if insert_toc_relative_to_this_cell:\n", " TOC_cell=this_cell+TOC_cell\n", " \n", "nb['cells'][this_cell]['metadata']['jupyter']={\"source_hidden\": True} # overwitten lol\n", "\n", "nb['cells'].insert(TOC_cell,TOC)\n", "\n", "\n", "with open(file_name,'w') as f:\n", " json.dump(nb,f)\n", "# print(nb['cells'][0])\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# head\n", "hello \n", "world" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.2\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.2.1\n", "bb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# h1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## h2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### h3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### h4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### h5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###### h6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.1" } }, "nbformat": 4, "nbformat_minor": 4 }