-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblohg_converter.py
71 lines (61 loc) · 2.55 KB
/
blohg_converter.py
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
"""
This module creates a converter to the particular ReST format that works best
with the Blohg (blohg.org) blogging engine
"""
import os
import copy
import nbconvert as nb
import argparse
import shutil
from yt.utilities.minimal_representation import MinimalNotebook
class BlohgConverter(nb.ConverterRST):
def __init__(self, post_name, infile):
self.infile = infile
self.infile_dir, infile_root = os.path.split(infile)
if post_name is None: post_name = infile_root
self.post_name = post_name
infile_root = os.path.splitext(infile_root)[0]
sanitized_root = infile_root.replace(" ", "_")
files_dir = os.path.join("content", "attachments", sanitized_root + '_files')
if not os.path.isdir(files_dir):
os.mkdir(files_dir)
self.infile_root = sanitized_root
self.files_dir = files_dir
self.outbase = os.path.join("content", "post", sanitized_root)
# Now we copy the entire notebook source over
outfile = os.path.join("content", "attachments",
sanitized_root + ".ipynb")
shutil.copyfile(self.infile, outfile) # Store a backup here
mn = MinimalNotebook(self.infile, self.post_name)
rv = mn.upload()
self.hub_url = rv['url']
def render_code(self, cell):
if not cell.input:
return []
lines = ['In[%s]:' % cell.prompt_number, '']
lines.extend(nb.rst_directive('.. sourcecode:: python', cell.input))
if len(cell.outputs) == 0: return lines
for output in nb.coalesce_streams(cell.outputs):
conv_fn = self.dispatch(output.output_type)
lines.extend(conv_fn(output))
return lines
def _img_lines(self, img_file):
prefix = "content/attachments/"
if img_file.startswith(prefix):
img_file = img_file[len(prefix):]
return ['.. attachment-image:: %s' % img_file, '']
def optional_header(self):
header = [self.post_name + "\n" + "=" * len(self.post_name)]
header += ["", "", "`Notebook Download <%s>`_\n\n" % (self.hub_url)]
return header
if __name__ == '__main__':
cwd = os.getcwd()
if not os.path.exists("content"):
raise RuntimeError
parser = argparse.ArgumentParser()
parser.add_argument('infile', nargs=1)
parser.add_argument('-p', '--post-name', default=None,
dest = "post_name", help="The name of the blog post")
args = parser.parse_args()
converter = BlohgConverter(args.post_name, args.infile[0])
converter.render()