{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Putting things together.\n", "\n", "Put together the Markdown tangle, weave, and template actions. " ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [], "source": [ " import ast, types, textwrap, IPython, jinja2, string\n", " import deathbeds.__String_Template_Ipython_formatter as strings, \\\n", " deathbeds.__Ipython_aware_jinja_templates as templates, \\\n", " deathbeds.__Markdown_to_vdom_with_mistletoe as weave, \\\n", " deathbeds.__Markdown_to_Python as tangle" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [], "source": [ " ip = IPython.get_ipython()" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [], "source": [ " class MarkdownShell(IPython.core.interactiveshell.InteractiveShell):\n", " \"\"\"Markdown Shell\"\"\"\n", " def run_cell(self, raw, store_history=False, silent=False, shell_futures=True, **user_expressions):\n", " self._last_traceback = None\n", " source = tangle.markdown_to_python(raw)\n", " \n", " result = IPython.core.interactiveshell.InteractiveShell.run_cell(\n", " self, source, store_history=store_history, silent=silent, shell_futures=shell_futures)\n", " \n", " try: is_markdown = raw.strip() == ast.literal_eval(source).strip()\n", " except: is_markdown = False\n", "\n", " is_source = ''.join(map(str.rstrip, textwrap.dedent(raw).strip().splitlines(True))\n", " ) == ''.join(map(str.rstrip, source.strip().splitlines(True)))\n", " \n", " if not (is_markdown or is_source):\n", " stripped = raw.rstrip()\n", " blank_line = stripped and not raw.splitlines()[0].strip()\n", " if not blank_line: IPython.display.display(raw)\n", " \n", " return result" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [], "source": [ " def show_type(type):\n", " if hasattr(type, '__doc__'):\n", " ns = vars(importlib.import_module(type.__module__))\n", " type = type.__doc__\n", " if isinstance(type, str):\n", " type = weave.FlexRenderer()(template(type, ns))\n", " return ip.display_formatter.format(type)" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [], "source": [ " env = jinja2.Environment(finalize=templates.ipython_formatter)" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [], "source": [ " def template(str, dict):\n", " if ' $' in str: str = string.Template(str).safe_substitute(strings.IpythonChainMap(dict))\n", " if (\n", " (env.block_end_string in str ) and (env.block_start_string in str )\n", " ) or ((env.variable_end_string in str) and (env.variable_start_string in str)):\n", " str = env.from_string(str).render(dict)\n", " return str" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [], "source": [ " def load_ipython_extension(ip):\n", " ip.run_cell = types.MethodType(MarkdownShell.run_cell, ip)\n", " ip.display_formatter.mimebundle_formatter.for_type(\n", " str, lambda str: ip.display_formatter.format(weave.FlexRenderer()(template(str, ip.user_ns))))\n", " ip.display_formatter.mimebundle_formatter.for_type(type, show_type)\n", " __name__ == '__main__' and load_ipython_extension(IPython.get_ipython())" ] }, { "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }