1 import base64
, os
, re
, sys
2 from IPython
.nbformat
.current
import reads
, write
, new_output
4 filename
= os
.path
.splitext(sys
.argv
[1])[0]
7 title
, description
= open("{}.txt".format(filename
), encoding
="utf-8").read().split('\n\n', 1)
9 title
, description
= filename
, ""
10 description
= description
.replace("...", "").replace("'''", "**").replace("''", "*")
11 bendpattern
= re
.compile("^!+", re
.MULTILINE
)
12 bendcode
= "![bend](http://pyx.sourceforge.net/bend.png)"
13 description
= re
.sub(bendpattern
, lambda m
: "![bend](http://pyx.sourceforge.net/bend.png)"*(m
.end()-m
.start()), description
)
14 code
= open("{}.py".format(filename
), encoding
="utf-8").read()
15 code
= re
.sub('\.writeEPSfile\(("[a-z]+")?\)\n.*writePDFfile\(("[a-z]+")?\)', "", code
)
16 input = """# <headingcell level=1>
23 """.format(title
=title
, code
=code
, description
=description
)
25 nb
= reads(input, "py")
26 codecell
, = [cell
for ws
in nb
.worksheets
for cell
in ws
.cells
if cell
.cell_type
== "code"]
27 codecell
.outputs
.append(new_output(output_type
="pyout",
28 output_png
=base64
.encodebytes(open("{}.png".format(filename
), "rb").read())))
29 write(nb
, open("{}.ipynb".format(filename
), "w"), "ipynb")