Test pidgy.tangle
¶
import pidgy, ast
tangle = pidgy.tangle.Tangle()
s = """---
a: front matter
---
This is a paragraph.
* a list
def f():
A docstring
print
"""
Unnormalized tokens.
tangle.parse(s, normalize=False)
[Token(type='front_matter', tag='', nesting=0, attrs=None, map=[0, 3], level=0, children=None, content='---\na: front matter\n---', markup='---', info='', meta='a: front matter', block=True, hidden=True),
Token(type='paragraph_open', tag='p', nesting=1, attrs=None, map=[4, 5], level=0, children=None, content='', markup='', info='', meta={}, block=True, hidden=False),
Token(type='inline', tag='', nesting=0, attrs=None, map=[4, 5], level=1, children=[Token(type='text', tag='', nesting=0, attrs=None, map=None, level=0, children=None, content='This is a paragraph.', markup='', info='', meta={}, block=False, hidden=False)], content='This is a paragraph.', markup='', info='', meta={}, block=True, hidden=False),
Token(type='paragraph_close', tag='p', nesting=-1, attrs=None, map=None, level=0, children=None, content='', markup='', info='', meta={}, block=True, hidden=False),
Token(type='bullet_list_open', tag='ul', nesting=1, attrs=None, map=[6, 10], level=0, children=None, content='', markup='*', info='', meta={}, block=True, hidden=False),
Token(type='list_item_open', tag='li', nesting=1, attrs=None, map=[6, 10], level=1, children=None, content='', markup='*', info='', meta={}, block=True, hidden=False),
Token(type='paragraph_open', tag='p', nesting=1, attrs=None, map=[6, 7], level=2, children=None, content='', markup='', info='', meta={}, block=True, hidden=False),
Token(type='inline', tag='', nesting=0, attrs=None, map=[6, 7], level=3, children=[Token(type='text', tag='', nesting=0, attrs=None, map=None, level=0, children=None, content='a list', markup='', info='', meta={}, block=False, hidden=False)], content='a list', markup='', info='', meta={}, block=True, hidden=False),
Token(type='paragraph_close', tag='p', nesting=-1, attrs=None, map=None, level=2, children=None, content='', markup='', info='', meta={}, block=True, hidden=False),
Token(type='paragraph_open', tag='p', nesting=1, attrs=None, map=[8, 9], level=2, children=None, content='', markup='', info='', meta={}, block=True, hidden=False),
Token(type='inline', tag='', nesting=0, attrs=None, map=[8, 9], level=3, children=[Token(type='text', tag='', nesting=0, attrs=None, map=None, level=0, children=None, content='def f():', markup='', info='', meta={}, block=False, hidden=False)], content='def f():', markup='', info='', meta={}, block=True, hidden=False),
Token(type='paragraph_close', tag='p', nesting=-1, attrs=None, map=None, level=2, children=None, content='', markup='', info='', meta={}, block=True, hidden=False),
Token(type='list_item_close', tag='li', nesting=-1, attrs=None, map=None, level=1, children=None, content='', markup='*', info='', meta={}, block=True, hidden=False),
Token(type='bullet_list_close', tag='ul', nesting=-1, attrs=None, map=None, level=0, children=None, content='', markup='*', info='', meta={}, block=True, hidden=False),
Token(type='paragraph_open', tag='p', nesting=1, attrs=None, map=[10, 11], level=0, children=None, content='', markup='', info='', meta={}, block=True, hidden=False),
Token(type='inline', tag='', nesting=0, attrs=None, map=[10, 11], level=1, children=[Token(type='text', tag='', nesting=0, attrs=None, map=None, level=0, children=None, content='A docstring', markup='', info='', meta={}, block=False, hidden=False)], content='A docstring', markup='', info='', meta={}, block=True, hidden=False),
Token(type='paragraph_close', tag='p', nesting=-1, attrs=None, map=None, level=0, children=None, content='', markup='', info='', meta={}, block=True, hidden=False),
Token(type='code_block', tag='code', nesting=0, attrs=None, map=[12, 13], level=0, children=None, content=' print\n', markup='', info='', meta={}, block=True, hidden=False)]
Normalized block tokens
tangle.parse(s, normalize=True)
[Token(type='front_matter', tag='', nesting=0, attrs=None, map=[0, 3], level=0, children=None, content='---\na: front matter\n---', markup='---', info='', meta='a: front matter', block=True, hidden=True),
Token(type='bullet_list_open', tag='ul', nesting=1, attrs=None, map=[6, 10], level=0, children=None, content='', markup='*', info='', meta={}, block=True, hidden=False),
Token(type='code_block', tag='code', nesting=0, attrs=None, map=[12, 13], level=0, children=None, content=' print\n', markup='', info='', meta={}, block=True, hidden=False)]
Normalized block tokens
print(tangle.render(s))
locals().update(__import__('ruamel.yaml').yaml.safe_load("""---
a: front matter
---
""".partition('---')[2].rpartition('---')[0]))
"""* a list
def f():""";
"""A docstring"""
print
transform = pidgy.tangle.pidgyManager().transform_cell
print(transform(s))
locals().update(__import__('ruamel.yaml').yaml.safe_load("""---
a: front matter
---
""".partition('---')[2].rpartition('---')[0]))
"""* a list
def f():""";
"""A docstring"""
print
print(pidgy.tangle.demojize("""
🤖🐼 = 10
"""))
_robot_face__panda_face_ = 10
ast.parse(transform("""
return 100
""")).body
[<_ast.Return at 0x7f65a425df50>]
pidgy.tangle.ExtraSyntax().visit(ast.parse(transform("""
return 100
"""))).body[0].value
<_ast.Call at 0x7f65a4313e50>
print(tangle.render("""This is a string \\
.lower()"""))
"""This is a string """\
\
\
\
\
.lower()
print(tangle.render("""---
a: 10
---
a=\\
[a]: xxx
[b]: ttt
"""))
locals().update(__import__('ruamel.yaml').yaml.safe_load("""---
a: 10
---
""".partition('---')[2].rpartition('---')[0]))
a=\
\
{x.group(1): x.group(2).rstrip() for x in __import__('pidgy').util.link_item.finditer("""[a]: xxx
[b]: ttt""")}