Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e8f2cf

Browse files
committed
Add argument parsing, and ability to convert an HTML file from command line
1 parent 0d33781 commit 0e8f2cf

3 files changed

Lines changed: 62 additions & 27 deletions

File tree

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ The latest development version of doctest is required. This can be installed via
2424
$ curl http://docutils.svn.sourceforge.net/viewvc/docutils/trunk/docutils/?view=tar > docutils.gz
2525
$ pip install -U docutils.gz
2626

27+
For conversion to HTML, pygments is also required
28+
::
29+
30+
$ pip install pygments
31+
2732
Running Tests
2833
=============
2934
::

nbconvert.py

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import os
1414
import subprocess
1515
import sys
16-
16+
import argparse
1717
from IPython.nbformat import current as nbformat
1818
from IPython.utils.text import indent
1919

@@ -36,16 +36,17 @@ def rst_directive(directive, text=''):
3636

3737
# Converters for parts of a cell.
3838

39+
3940
class ConversionException(Exception):
4041
pass
4142

4243

4344
class Converter(object):
4445
default_encoding = 'utf-8'
4546

46-
def __init__(self, fname):
47-
self.fname = fname
48-
self.dirpath = os.path.dirname(fname)
47+
def __init__(self, infile):
48+
self.infile = infile
49+
self.dirpath = os.path.dirname(infile)
4950

5051
@property
5152
def extension(self):
@@ -66,25 +67,25 @@ def convert(self):
6667
return '\n'.join(lines)
6768

6869
def render(self):
69-
"read, convert, and save self.fname"
70+
"read, convert, and save self.infile"
7071
self.read()
7172
self.output = self.convert()
7273
return self.save()
7374

7475
def read(self):
7576
"read and parse notebook into NotebookNode called self.nb"
76-
with open(self.fname) as f:
77+
with open(self.infile) as f:
7778
self.nb = nbformat.read(f, 'json')
7879

79-
def save(self, fname=None, encoding=None):
80+
def save(self, infile=None, encoding=None):
8081
"read and parse notebook into self.nb"
81-
if fname is None:
82-
fname = os.path.splitext(self.fname)[0] + '.' + self.extension
82+
if infile is None:
83+
infile = os.path.splitext(self.infile)[0] + '.' + self.extension
8384
if encoding is None:
8485
encoding = self.default_encoding
85-
with open(fname, 'w') as f:
86+
with open(infile, 'w') as f:
8687
f.write(self.output.encode(encoding))
87-
return fname
88+
return infile
8889

8990
def render_heading(self, cell):
9091
raise NotImplementedError
@@ -170,13 +171,13 @@ def render_display_data(self, output):
170171
lines = []
171172

172173
if 'png' in output:
173-
fname = 'nb_figure_%s.png' % self.figures_counter
174-
fullname = os.path.join(self.dirpath, fname)
174+
infile = 'nb_figure_%s.png' % self.figures_counter
175+
fullname = os.path.join(self.dirpath, infile)
175176
with open(fullname, 'w') as f:
176177
f.write(output.png.decode('base64'))
177178

178179
self.figures_counter += 1
179-
lines.append('.. image:: %s' % fname)
180+
lines.append('.. image:: %s' % infile)
180181
lines.append('')
181182

182183
return lines
@@ -194,7 +195,7 @@ def render_stream(self, output):
194195
return lines
195196

196197

197-
def rst2simplehtml(fname):
198+
def rst2simplehtml(infile):
198199
"""Convert a rst file to simplified html suitable for blogger.
199200
200201
This just runs rst2html with certain parameters to produce really simple
@@ -211,7 +212,7 @@ def rst2simplehtml(fname):
211212
"--no-toc-backlinks --no-section-numbering "
212213
"--strip-comments ")
213214

214-
cmd = "%s %s" % (cmd_template, fname)
215+
cmd = "%s %s" % (cmd_template, infile)
215216
proc = subprocess.Popen(cmd,
216217
stdout=subprocess.PIPE,
217218
stderr=subprocess.PIPE,
@@ -232,7 +233,7 @@ def rst2simplehtml(fname):
232233
if line.startswith('<body>'):
233234
break
234235

235-
newfname = os.path.splitext(fname)[0] + '.html'
236+
newfname = os.path.splitext(infile)[0] + '.html'
236237
with open(newfname, 'w') as f:
237238
for line in walker:
238239
if line.startswith('</body>'):
@@ -243,11 +244,27 @@ def rst2simplehtml(fname):
243244
return newfname
244245

245246

246-
def main(fname):
247+
def main(infile, format='rst'):
247248
"""Convert a notebook to html in one step"""
248-
converter = ConverterRST(fname)
249-
converter.render()
249+
if format == 'rst':
250+
converter = ConverterRST(infile)
251+
converter.render()
252+
elif format == 'html':
253+
#Currently, conversion to html is a 2 step process, nb->rst->html
254+
converter = ConverterRST(infile)
255+
rstfname = converter.render()
256+
rst2simplehtml(rstfname)
250257

251258

252259
if __name__ == '__main__':
253-
main(sys.argv[1])
260+
parser = argparse.ArgumentParser(description='nbconvert: Convert IPython notebooks to other formats')
261+
262+
# TODO: consider passing file like object around, rather than filenames
263+
# would allow us to process stdin, or even http streams
264+
#parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin)
265+
266+
#Require a filename as a positional argument
267+
parser.add_argument('infile', nargs=1)
268+
parser.add_argument('-f', '--format', default='rst')
269+
args = parser.parse_args()
270+
main(infile=args.infile[0], format=args.format)

tests/test_simple.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ def clean_dir():
1313
"Remove .rst files created during conversion"
1414
map(os.remove, glob.glob("./tests/*.rst"))
1515
map(os.remove, glob.glob("./tests/*.png"))
16+
map(os.remove, glob.glob("./tests/*.html"))
17+
1618

1719
@nt.with_setup(clean_dir, clean_dir)
1820
def test_simple():
1921
c = ConverterRST(fname)
2022
f = c.render()
2123
nt.assert_true('rst' in f, 'changed file extension to rst')
2224

25+
2326
@nt.with_setup(clean_dir, clean_dir)
2427
def test_main():
2528
"""
@@ -28,14 +31,15 @@ def test_main():
2831
main(fname)
2932
nt.assert_true(os.path.exists(out_fname))
3033

34+
3135
def test_render_heading():
3236
""" Unit test for cell type "heading" """
3337
# Generate and test heading cells level 1-6
34-
for level in xrange(1,7):
38+
for level in xrange(1, 7):
3539
cell = {
3640
'cell_type': 'heading',
3741
'level' : level,
38-
'source' : ['Test for heading type H{0}'.format(level)]
42+
'source' : ['Test for heading type H{0}'.format(level)]
3943
}
4044
# Convert cell dictionaries to NotebookNode
4145
cell_nb = nbformat.NotebookNode(cell)
@@ -48,10 +52,19 @@ def test_render_heading():
4852
# Render to rst
4953
c = ConverterRST('')
5054
rst_list = c.render_heading(cell_nb)
51-
nt.assert_true(isinstance(rst_list,list)) # render should return a list
55+
nt.assert_true(isinstance(rst_list, list)) # render should return a list
5256
rst_str = "".join(rst_list)
5357
# Confirm rst content
54-
heading_level = {1:'=', 2:'-', 3:'`', 4:'\'', 5:'.',6:'~'}
58+
heading_level = {1: '=', 2: '-', 3: '`', 4: '\'', 5: '.', 6: '~'}
5559
chk_str = "Test for heading type H{0}\n{1}\n".format(
56-
level,heading_level[level]*24)
57-
nt.assert_equal(rst_str,chk_str)
60+
level, heading_level[level] * 24)
61+
nt.assert_equal(rst_str, chk_str)
62+
63+
64+
@nt.with_setup(clean_dir, clean_dir)
65+
def test_main_html():
66+
"""
67+
Test main entry point
68+
"""
69+
main(fname, format='html')
70+
nt.assert_true(os.path.exists('tests/test.html'))

0 commit comments

Comments
 (0)