forked from stsievert/rst-to-ipynb
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrst2ipynb-sageblock-filter
executable file
·124 lines (96 loc) · 3.58 KB
/
rst2ipynb-sageblock-filter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
import json
import warnings
"""
Pandoc filter to convert all regular text to uppercase.
Code, link URLs, etc. are not affected.
"""
from pandocfilters import toJSONFilter, CodeBlock, Math
def python_input_format(text):
return CodeBlock([u'', [u'python', u'input'], []], text)
def python_doctest_input_format(text):
return python_input_format(text)
def sage_input_format(text):
return python_input_format(text)
def output_format(text):
return CodeBlock([u'', [u'json', u'output'], []],
json.dumps([{'data': {'text/plain': text},
'execution_count': 1,
'metadata': {},
'output_type': 'execute_result'}]))
class Block:
pass
math_characters = r'\^_'
def reformat_math(key, value, format, meta):
if key != 'Str':
return
if not any(c in value for c in math_characters):
return
return Math([], value)
def reformat_sage_block(key, value, format, meta):
if key != 'CodeBlock':
return
format, string = value
if "\t" in string:
warnings.warn("String contain <tab> character:\n"+string)
exit(0)
lines = string.split('\n')
result = []
current = Block()
current.format = None
current.value = ""
def push():
if current.format:
result.append(current.format(current.value))
current.format = None
current.value = ""
for line in lines:
if line.startswith("sage: "):
if current.format == sage_input_format:
# Concatenate together contiguous sage inputs
current.value += "\n"+line[6:]
else:
push()
current.format = sage_input_format
current.value = line[6:]
elif line.startswith(">>> "):
if current.format == python_doctest_input_format:
# Concatenate together contiguous python
current.value += "\n" + line[4:]
else:
push()
current.format = python_doctest_input_format
current.value = line[4:]
elif line.startswith("....: "):
if current.format != sage_input_format:
msg = "continuation prompt not following a sage prompt: %s"
warnings.warn(msg % line)
current.value += "\n" + line[6:]
# the final "..." may not have a trailing whitespace
elif ((line.startswith("... ") or line == "...") and
current.format != output_format):
if current.format != python_doctest_input_format:
msg = "continuation prompt not following a python prompt: %s"
warnings.warn(msg % line)
else:
current.value += "\n" + line[4:]
else: # doesn't have leading prompts
if current.format is None: # first line of a code block
current.format = python_input_format
elif (current.format == sage_input_format or
current.format == python_doctest_input_format):
# subsequent lines in block with prompts
push() # reset format
if line:
current.format = output_format
else:
continue
if current.value:
current.value += "\n" + line
else:
current.value = line
push()
return result
if __name__ == "__main__":
toJSONFilter(reformat_sage_block)
# toJSONFilter(reformat_math)