{ "cells": [ { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2020-05-01T09:27:13.438054Z", "start_time": "2020-05-01T09:27:13.191491Z" } }, "source": [ "# conf.py" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-11-03T20:26:50.212009Z", "start_time": "2020-11-03T20:26:50.202152Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting manual/source/conf.py\n" ] } ], "source": [ "%%writefile manual/source/conf.py\n", "# Configuration file for the Sphinx documentation builder.\n", "#\n", "# This file only contains a selection of the most common options. For a full\n", "# list see the documentation:\n", "# http://www.sphinx-doc.org/en/master/config\n", "\n", "# -- Path setup --------------------------------------------------------------\n", "\n", "# If extensions (or modules to document with autodoc) are in another directory,\n", "# add these directories to sys.path here. If the directory is relative to the\n", "# documentation root, use os.path.abspath to make it absolute, like shown here.\n", "#\n", "import os\n", "# import sys\n", "# sys.path.insert(0, os.path.abspath('.'))\n", "\n", "#import sphinx_rtd_theme\n", "\n", "from sphinx.transforms import SphinxTransform\n", "from docutils import nodes\n", "from sphinx import addnodes\n", "\n", "# -- Project information -----------------------------------------------------\n", "\n", "project = 'The ulab book'\n", "copyright = '2019-2020, Zoltán Vörös and contributors'\n", "author = 'Zoltán Vörös'\n", "\n", "# The full version, including alpha/beta/rc tags\n", "release = '1.2.1'\n", "\n", "\n", "# -- General configuration ---------------------------------------------------\n", "\n", "# Add any Sphinx extension module names here, as strings. They can be\n", "# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom\n", "# ones.\n", "extensions = [\n", "]\n", "\n", "# Add any paths that contain templates here, relative to this directory.\n", "templates_path = ['_templates']\n", "\n", "# List of patterns, relative to source directory, that match files and\n", "# directories to ignore when looking for source files.\n", "# This pattern also affects html_static_path and html_extra_path.\n", "exclude_patterns = []\n", "\n", "\n", "# Add any paths that contain custom static files (such as style sheets) here,\n", "# relative to this directory. They are copied after the builtin static files,\n", "# so a file named \"default.css\" will overwrite the builtin \"default.css\".\n", "html_static_path = ['_static']\n", "\n", "latex_maketitle = r'''\n", "\\begin{titlepage}\n", "\\begin{flushright}\n", "\\Huge\\textbf{The $\\mu$lab book}\n", "\\vskip 0.5em\n", "\\LARGE\n", "\\textbf{Release 1.2.1}\n", "\\vskip 5em\n", "\\huge\\textbf{Zoltán Vörös}\n", "\\end{flushright}\n", "\\begin{flushright}\n", "\\LARGE\n", "\\vskip 2em\n", "with contributions by\n", "\\vskip 2em\n", "\\textbf{Roberto Colistete Jr.}\n", "\\vskip 0.2em\n", "\\textbf{Jeff Epler}\n", "\\vskip 0.2em\n", "\\textbf{Taku Fukada}\n", "\\vskip 0.2em\n", "\\textbf{Diego Elio Pettenò}\n", "\\vskip 0.2em\n", "\\textbf{Scott Shawcroft}\n", "\\vskip 5em\n", "\\today\n", "\\end{flushright}\n", "\\end{titlepage}\n", "'''\n", "\n", "latex_elements = {\n", " 'maketitle': latex_maketitle\n", "}\n", "\n", "\n", "master_doc = 'index'\n", "\n", "author=u'Zoltán Vörös'\n", "copyright=author\n", "language='en'\n", "\n", "latex_documents = [\n", "(master_doc, 'the-ulab-book.tex', 'The $\\mu$lab book',\n", "'Zoltán Vörös', 'manual'),\n", "]\n", "\n", "# sphinx-autoapi\n", "extensions.append('autoapi.extension')\n", "autoapi_type = 'python'\n", "autoapi_keep_files = True\n", "autoapi_dirs = [\"ulab\"]\n", "autoapi_add_toctree_entry = False\n", "autoapi_options = ['members', 'undoc-members', 'private-members', 'show-inheritance', 'special-members']\n", "autoapi_template_dir = '../autoapi/templates'\n", "autoapi_python_class_content = \"both\"\n", "autoapi_python_use_implicit_namespaces = True\n", "autoapi_root = \".\"\n", "\n", "\n", "# Read the docs theme\n", "on_rtd = os.environ.get('READTHEDOCS', None) == 'True'\n", "if not on_rtd:\n", " try:\n", " import sphinx_rtd_theme\n", " html_theme = 'sphinx_rtd_theme'\n", " html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), '.']\n", " except ImportError:\n", " html_theme = 'default'\n", " html_theme_path = ['.']\n", "else:\n", " html_theme_path = ['.']\n", "\n", "\n", "class UlabTransform(SphinxTransform):\n", " default_priority = 870\n", "\n", " def _convert_first_paragraph_into_title(self):\n", " title = self.document.next_node(nodes.title)\n", " paragraph = self.document.next_node(nodes.paragraph)\n", " if not title or not paragraph:\n", " return\n", " if isinstance(paragraph[0], nodes.paragraph):\n", " paragraph = paragraph[0]\n", " if all(isinstance(child, nodes.Text) for child in paragraph.children):\n", " for child in paragraph.children:\n", " title.append(nodes.Text(\" \\u2013 \"))\n", " title.append(child)\n", " paragraph.parent.remove(paragraph)\n", "\n", " def _enable_linking_to_nonclass_targets(self):\n", " for desc in self.document.traverse(addnodes.desc):\n", " for xref in desc.traverse(addnodes.pending_xref):\n", " if xref.attributes.get(\"reftype\") == \"class\":\n", " xref.attributes.pop(\"refspecific\", None)\n", "\n", " def apply(self, **kwargs):\n", " docname = self.env.docname\n", " if docname.startswith(\"ulab/\"):\n", " self._convert_first_paragraph_into_title()\n", " self._enable_linking_to_nonclass_targets()\n", "\n", "\n", "def setup(app):\n", " app.add_transform(UlabTransform)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "ExecuteTime": { "end_time": "2020-10-25T21:49:23.781697Z", "start_time": "2020-10-25T21:49:23.768534Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting manual/source/index.rst\n" ] } ], "source": [ "%%writefile manual/source/index.rst\n", "\n", ".. ulab-manual documentation master file, created by\n", " sphinx-quickstart on Sat Oct 19 12:48:00 2019.\n", " You can adapt this file completely to your liking, but it should at least\n", " contain the root `toctree` directive.\n", "\n", "Welcome to the ulab book!\n", "=======================================\n", "\n", "\n", ".. toctree::\n", " :maxdepth: 3\n", " :caption: Contents:\n", "\n", " ulab-intro\n", " ulab-ndarray\n", " ulab-approx\n", " ulab-compare\n", " ulab-fft\n", " ulab-filter\n", " ulab-linalg\n", " ulab-numerical\n", " ulab-poly\n", " ulab-vectorise\n", " ulab-programming\n", "\n", "Indices and tables\n", "==================\n", "\n", "* :ref:`genindex`\n", "* :ref:`modindex`\n", "* :ref:`search`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Notebook conversion" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-11-03T20:26:54.904105Z", "start_time": "2020-11-03T20:26:53.219731Z" } }, "outputs": [], "source": [ "import nbformat as nb\n", "import nbformat.v4.nbbase as nb4\n", "from nbconvert import RSTExporter\n", "\n", "from jinja2 import FileSystemLoader\n", "rstexporter = RSTExporter(\n", " extra_loaders=[FileSystemLoader('./templates')],\n", " template_file = './templates/manual.tpl'\n", ")\n", "\n", "def convert_notebook(fn):\n", " source = nb.read(fn+'.ipynb', nb.NO_CONVERT)\n", " notebook = nb4.new_notebook()\n", " append_cell = False\n", " for cell in source['cells']:\n", " if append_cell:\n", " notebook.cells.append(cell)\n", " else:\n", " if cell.cell_type == 'markdown':\n", " if cell.source == '__END_OF_DEFS__':\n", " append_cell = True\n", " \n", " (rst, resources) = rstexporter.from_notebook_node(notebook)\n", " with open('./manual/source/' + fn + '.rst', 'w') as fout:\n", " fout.write(rst)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-11-03T20:27:00.291103Z", "start_time": "2020-11-03T20:26:54.934703Z" } }, "outputs": [], "source": [ "files = ['ulab-intro',\n", " 'ulab-ndarray',\n", " 'ulab-approx', \n", " 'ulab-compare',\n", " 'ulab-fft',\n", " 'ulab-filter',\n", " 'ulab-linalg',\n", " 'ulab-numerical',\n", " 'ulab-poly',\n", " 'ulab-vectorise',\n", " 'ulab-programming']\n", "\n", "for file in files:\n", " convert_notebook(file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Template" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-10-30T19:04:50.295563Z", "start_time": "2020-10-30T19:04:50.227535Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting ./templates/manual.tpl\n" ] } ], "source": [ "%%writefile ./templates/manual.tpl\n", "\n", "{%- extends 'display_priority.tpl' -%}\n", "\n", "\n", "{% block in_prompt %}\n", "{% endblock in_prompt %}\n", "\n", "{% block output_prompt %}\n", "{% endblock output_prompt %}\n", "\n", "{% block input scoped%}\n", "\n", "{%- if cell.source.split('\\n')[0].startswith('%%micropython') -%}\n", ".. code::\n", " \n", "{{ '\\n'.join(['# code to be run in micropython'] + cell.source.strip().split('\\n')[1:]) | indent}}\n", "\n", "{%- else -%}\n", ".. code::\n", "\n", "{{ '\\n'.join(['# code to be run in CPython\\n'] + cell.source.strip().split('\\n')) | indent}}\n", "{%- endif -%}\n", "{% endblock input %}\n", "\n", "{% block error %}\n", "::\n", "\n", "{{ super() }}\n", "{% endblock error %}\n", "\n", "{% block traceback_line %}\n", "{{ line | indent | strip_ansi }}\n", "{% endblock traceback_line %}\n", "\n", "{% block execute_result %}\n", "{% block data_priority scoped %}\n", "{{ super() }}\n", "{% endblock %}\n", "{% endblock execute_result %}\n", "\n", "{% block stream %}\n", ".. parsed-literal::\n", "\n", "{{ output.text | indent }}\n", "{% endblock stream %}\n", "\n", "{% block data_svg %}\n", ".. image:: {{ output.metadata.filenames['image/svg+xml'] | urlencode }}\n", "{% endblock data_svg %}\n", "\n", "{% block data_png %}\n", ".. image:: {{ output.metadata.filenames['image/png'] | urlencode }}\n", "{%- set width=output | get_metadata('width', 'image/png') -%}\n", "{%- if width is not none %}\n", " :width: {{ width }}px\n", "{%- endif %}\n", "{%- set height=output | get_metadata('height', 'image/png') -%}\n", "{%- if height is not none %}\n", " :height: {{ height }}px\n", "{%- endif %}\n", "{% endblock data_png %}\n", "\n", "{% block data_jpg %}\n", ".. image:: {{ output.metadata.filenames['image/jpeg'] | urlencode }}\n", "{%- set width=output | get_metadata('width', 'image/jpeg') -%}\n", "{%- if width is not none %}\n", " :width: {{ width }}px\n", "{%- endif %}\n", "{%- set height=output | get_metadata('height', 'image/jpeg') -%}\n", "{%- if height is not none %}\n", " :height: {{ height }}px\n", "{%- endif %}\n", "{% endblock data_jpg %}\n", "\n", "{% block data_markdown %}\n", "{{ output.data['text/markdown'] | convert_pandoc(\"markdown\", \"rst\") }}\n", "{% endblock data_markdown %}\n", "\n", "{% block data_latex %}\n", ".. math::\n", "\n", "{{ output.data['text/latex'] | strip_dollars | indent }}\n", "{% endblock data_latex %}\n", "\n", "{% block data_text scoped %}\n", ".. parsed-literal::\n", "\n", "{{ output.data['text/plain'] | indent }}\n", "{% endblock data_text %}\n", "\n", "{% block data_html scoped %}\n", ".. raw:: html\n", "\n", "{{ output.data['text/html'] | indent }}\n", "{% endblock data_html %}\n", "\n", "{% block markdowncell scoped %}\n", "{{ cell.source | convert_pandoc(\"markdown\", \"rst\") }}\n", "{% endblock markdowncell %}\n", "\n", "{%- block rawcell scoped -%}\n", "{%- if cell.metadata.get('raw_mimetype', '').lower() in resources.get('raw_mimetypes', ['']) %}\n", "{{cell.source}}\n", "{% endif -%}\n", "{%- endblock rawcell -%}\n", "\n", "{% block headingcell scoped %}\n", "{{ (\"#\" * cell.level + cell.source) | replace('\\n', ' ') | convert_pandoc(\"markdown\", \"rst\") }}\n", "{% endblock headingcell %}\n", "\n", "{% block unknowncell scoped %}\n", "unknown type {{cell.type}}\n", "{% endblock unknowncell %}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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.7.6" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "382.797px" }, "toc_section_display": true, "toc_window_display": true }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }