{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Test `pidgy.tangle`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ " import pidgy, ast" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['BlockLexer',\n", " 'ExtraSyntax',\n", " 'IPython',\n", " 'Tangle',\n", " '__all__',\n", " '__builtins__',\n", " '__doc__',\n", " '__file__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__spec__',\n", " '__test__',\n", " 'ast',\n", " 'demojize',\n", " 'init_json',\n", " 'mistune',\n", " 'normalizer',\n", " 'pidgy',\n", " 'pidgyManager',\n", " 'tangle',\n", " 'typing']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ " dir(pidgy.tangle)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ " tangle = pidgy.tangle.Tangle()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ " s = \"\"\"---\n", " a: front matter\n", " ---\n", " \n", " This is a paragraph.\n", " \n", " * a list\n", " \n", " def f():\n", " \n", " A docstring\n", " \n", " print\n", " \n", " \"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unnormalized tokens." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'type': 'front_matter', 'text': '\\na: front matter\\n'},\n", " {'type': 'paragraph', 'text': 'This is a paragraph.'},\n", " {'type': 'list_start', 'ordered': False},\n", " {'type': 'loose_item_start'},\n", " {'type': 'text', 'text': 'a list'},\n", " {'type': 'newline'},\n", " {'type': 'text', 'text': ' def f():'},\n", " {'type': 'list_item_end'},\n", " {'type': 'list_end'},\n", " {'type': 'paragraph', 'text': 'A docstring'},\n", " {'type': 'code', 'lang': None, 'text': ' print'}]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ " \n", " tangle.parse(s, normalize=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Normalized block tokens " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'type': 'front_matter', 'text': '\\na: front matter\\n'},\n", " {'type': 'paragraph',\n", " 'text': '\\nThis is a paragraph.\\n\\n* a list\\n\\n def f():\\n \\nA docstring'},\n", " {'type': 'code', 'lang': None, 'text': '\\n \\n print'}]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ " tangle.parse(s, normalize=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Normalized block tokens " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " locals().update(__import__('ruamel.yaml').yaml.safe_load(\n", " \"\"\"a: front matter\"\"\"\n", " ))\n", "\n", " \"\"\"This is a paragraph.\n", "\n", " * a list\n", "\n", " def f():\n", " \n", " A docstring\"\"\"\n", " \n", " print\n" ] } ], "source": [ " print(tangle.stringify(tangle.parse(s)))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ " transform = pidgy.tangle.pidgyManager().transform_cell" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "locals().update(__import__('ruamel.yaml').yaml.safe_load(\n", "\"\"\"a: front matter\"\"\"\n", "))\n", "\n", "\"\"\"This is a paragraph.\n", "\n", "* a list\n", "\n", " def f():\n", " \n", "A docstring\"\"\"\n", " \n", "print\n", "\n" ] } ], "source": [ " print(transform(s))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " _robot_face__panda_face_ = 10\n", "\n" ] } ], "source": [ " print(pidgy.tangle.demojize(\"\"\"\n", " 🤖🐼 = 10\n", " \"\"\"))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[<_ast.Return at 0x10b3afeb8>]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ " ast.parse(transform(\"\"\"\n", " return 100\n", " \"\"\")).body" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<_ast.Call at 0x109043080>" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ " pidgy.tangle.ExtraSyntax().visit(ast.parse(transform(\"\"\"\n", " return 100\n", " \"\"\"))).body[0].value" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }