Skip to content

Instantly share code, notes, and snippets.

Last active February 3, 2019 18:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Just add some css for `.interactive-block > table`


first(code, 'block')


>>> second(code, 'block')
this is not highlighted ('even if it looks like code')
>>> def test():
...     also(works, 'with', ...)


pandoc -s >! example.htm
#/usr/bin/env python3
import re
from pandocfilters import toJSONFilter, Div, Table, CodeBlock
re_cont = re.compile(r'^\.\.\. ?(.*)')
def interactive_session(key, value, format, meta):
if key == 'CodeBlock':
((ident, classes, keyvals), code) = value
if 'python' not in classes or not code.startswith('>>>'):
return CodeBlock((ident, classes, keyvals), code)
blocks = code.split('>>>')[1:]
rows = []
for block in blocks:
# remove initial space if available
block = block.lstrip(' ').split('\n')
input = [block[0]]
output = []
block_it = iter(block[1:])
for l in block_it:
m = re_cont.match(l)
# Only add lines until the first output
if m:
if l: output.append(l)
output += list(block_it)
prompts = '>>> \n' + '\n'.join('... ' for i in input[1:])
[CodeBlock(['', [], keyvals], prompts)],
[CodeBlock([ident, classes, keyvals], '\n'.join(input))],
if output:
[], # There’s no colspan so we need an empty cell here
[CodeBlock(['', [], keyvals], '\n'.join(output))],
ident = ''
kvs = []
caption = []
alignments = [dict(t='AlignDefault', c=[])] * 2
width = [0, 0]
headers = [[], []]
return Div(
[ident, ['interactive-block'], kvs],
[Table(caption, alignments, width, headers, rows)],
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment