examples of tangled pidgy input¤
this document is used to demonstrate how pidgy tangles code (ie translates markdown to python). each cell contains markdown
input and the corresponding python code is revealed below.
indented code vs fenced code¤
the commonmark spec has two conventions before code, indented code and fenced code. literate programming languages generally choose on or the other. literate coffeescript prefers indented code while rmarkdown, myst, org-mode rely on code fences.
code fences always add markdown semantics at the interface of code and narrative. indented code on the other hand can seamlessly flow between narrative. pidgy uses the indented code opinion and creates new affordances for both markdown and python as a single metalanguage.
try:
import piplite
await piplite.install("pidgy")
except ImportError:
pass
%load_ext pidgy.tangle
%unload_ext pidgy.tangle
markdown as block strings¤
a single line of markdown is wrapped in block quotes.
%%tangle
markdown is just a string; the semi-colon is appended to suppress output.
markdown blocks, not containing indented code, are block strings.
%%tangle
`pidgy`s tangle scheme only requires block level parsing of markdown,
and all non-indented markdown blocks are represented as strings.
this approach breaks convention relative to other markdown-forward literate programming
implementations that rely on code fences to indicate executable code.
```python
print("in `pidgy`, code fences are ignored.")
```
what follows is a large python block string
%%tangle
quotations are ''escaped' for safety'
adding IPython code¤
%%tangle
code is acknowledged the first moment that an indented code block is found like the print expression below.
print("this is the first line of real code")
the result is valid python code with the markdown chunks represented as block strings
%%tangle
line continuations can be used to connection python expressions to the markdown blocks.
a_block_of_markdown=\
\
we can explicitly use line continuations to connection indented python to markdown.
this block of code will define `a_block_or_markdown`
another tactic capturing blocks of markdown using parenthesis. in this form, methods can be applied to contents of the parenthesis. parenthesis are needed to combine multiple non-code markdown blocks.
%%tangle
parenthesized_markdown_block = (
one line
+
another line
).upper()
%%tangle
explicit strings, even explicit regular formatted strings, are invoked when the preceding code block ends with triple quotes.
explicit_string = rf"""
this markdown string is formatted and stripped.
""".strip()
docstrings¤
on of the most convenient affordances of pidgy is the ability to naturally write docstrings in markdown.
%%tangle
def my_function():
`my_function` doesn't do anything!
return # the position of the first line of code controls the aligned of the quotations
classes can be composed with docstrings for the type and methods.
%%tangle
class MyClass:
this is `MyClass`, i'm object oriented!
def my_method(self):
`MyClass.my_method` has knowledge of self
pass
conflicting opinions¤
- magic functions - specific to ipython not python
- truncating markdown blocks with null operators
...,pass,"",# comment - hiding output during the weave step
- line continuations