Show More
@@ -0,0 +1,103 b'' | |||
|
1 | """Base classes for the notebook conversion pipeline. | |
|
2 | ||
|
3 | This module defines Converter, from which all objects designed to implement | |
|
4 | a conversion of IPython notebooks to some other format should inherit. | |
|
5 | """ | |
|
6 | #----------------------------------------------------------------------------- | |
|
7 | # Copyright (c) 2012, the IPython Development Team. | |
|
8 | # | |
|
9 | # Distributed under the terms of the Modified BSD License. | |
|
10 | # | |
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | ||
|
14 | #----------------------------------------------------------------------------- | |
|
15 | # Imports | |
|
16 | #----------------------------------------------------------------------------- | |
|
17 | ||
|
18 | from __future__ import print_function, absolute_import | |
|
19 | ||
|
20 | # Stdlib imports | |
|
21 | import jinja2 | |
|
22 | import codecs | |
|
23 | import io | |
|
24 | import logging | |
|
25 | import os | |
|
26 | import pprint | |
|
27 | import re | |
|
28 | from types import FunctionType | |
|
29 | ||
|
30 | from jinja2 import Environment, PackageLoader, FileSystemLoader | |
|
31 | env = Environment(loader=FileSystemLoader('./templates/')) | |
|
32 | ||
|
33 | # IPython imports | |
|
34 | from IPython.nbformat import current as nbformat | |
|
35 | from IPython.config.configurable import Configurable, SingletonConfigurable | |
|
36 | from IPython.utils.traitlets import (List, Unicode, Type, Bool, Dict, CaselessStrEnum, | |
|
37 | Any) | |
|
38 | ||
|
39 | # Our own imports | |
|
40 | from IPython.utils.text import indent | |
|
41 | from .utils import remove_ansi | |
|
42 | #----------------------------------------------------------------------------- | |
|
43 | # Class declarations | |
|
44 | #----------------------------------------------------------------------------- | |
|
45 | def rm_fake(strng): | |
|
46 | return strng.replace('/files/', '') | |
|
47 | ||
|
48 | class ConversionException(Exception): | |
|
49 | pass | |
|
50 | ||
|
51 | ||
|
52 | def python_comment(string): | |
|
53 | return '# '+'\n# '.join(string.split('\n')) | |
|
54 | ||
|
55 | env.filters['pycomment'] = python_comment | |
|
56 | env.filters['indent'] = indent | |
|
57 | env.filters['rm_fake'] = rm_fake | |
|
58 | env.filters['rm_ansi'] = remove_ansi | |
|
59 | ||
|
60 | class ConverterTemplate(Configurable): | |
|
61 | ||
|
62 | display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text'] | |
|
63 | #------------------------------------------------------------------------- | |
|
64 | # Instance-level attributes that are set in the constructor for this | |
|
65 | # class. | |
|
66 | #------------------------------------------------------------------------- | |
|
67 | infile = Any() | |
|
68 | ||
|
69 | ||
|
70 | infile_dir = Unicode() | |
|
71 | ||
|
72 | def __init__(self, config=None, **kw): | |
|
73 | self.template = env.get_template('python.tpl') | |
|
74 | super(ConverterTemplate,self).__init__(config=config) | |
|
75 | ||
|
76 | def _get_prompt_number(self, cell): | |
|
77 | return cell.prompt_number if hasattr(cell, 'prompt_number') \ | |
|
78 | else self.blank_symbol | |
|
79 | ||
|
80 | ||
|
81 | def process(self): | |
|
82 | converted_cells = [] | |
|
83 | for worksheet in self.nb.worksheets: | |
|
84 | for cell in worksheet.cells: | |
|
85 | cell.type = cell.cell_type | |
|
86 | converted_cells.append(cell) | |
|
87 | continue | |
|
88 | if cell.cell_type in ('code'): | |
|
89 | converted_cells.append({'type':'code','source':cell.input}) | |
|
90 | else : | |
|
91 | converted_cells.append({'type':cell.cell_type,'source': python_comment(cell.source)}) | |
|
92 | ||
|
93 | return converted_cells | |
|
94 | ||
|
95 | def convert(self, cell_separator='\n'): | |
|
96 | return self.template.render(cells=self.process()) | |
|
97 | ||
|
98 | ||
|
99 | def read(self, filename): | |
|
100 | "read and parse notebook into NotebookNode called self.nb" | |
|
101 | with open(filename) as f: | |
|
102 | self.nb = nbformat.read(f, 'json') | |
|
103 |
@@ -0,0 +1,7 b'' | |||
|
1 | # coding: utf-8 | |
|
2 | from converters.template import * | |
|
3 | C = ConverterTemplate() | |
|
4 | C.read('tests/ipynbref/IntroNumPy.orig.ipynb') | |
|
5 | S = C.convert() | |
|
6 | with open('temp.txt','w') as f: | |
|
7 | f.write(S) |
@@ -0,0 +1,25 b'' | |||
|
1 | {%- block header -%} | |
|
2 | {%- endblock header -%} | |
|
3 | {%- block body -%} | |
|
4 | {%- for cell in cells -%} | |
|
5 | {%- if cell.type in ['code'] -%} | |
|
6 | {%- block codecell scoped-%} | |
|
7 | {%- endblock codecell-%} | |
|
8 | {%- elif cell.type in ['markdown'] -%} | |
|
9 | {%- block markdowncell scoped-%} | |
|
10 | {%- endblock markdowncell -%} | |
|
11 | {%- elif cell.type in ['heading'] -%} | |
|
12 | {%- block headingcell scoped-%} | |
|
13 | {%- endblock headingcell -%} | |
|
14 | {%- elif cell.type in ['raw'] -%} | |
|
15 | {%- block rawcell scoped-%} | |
|
16 | {%- endblock rawcell -%} | |
|
17 | {%- else -%} | |
|
18 | {%- block unknowncell scoped-%} | |
|
19 | {%- endblock unknowncell -%} | |
|
20 | {%- endif -%} | |
|
21 | {%- endfor -%} | |
|
22 | {%- endblock body -%} | |
|
23 | ||
|
24 | {%- block footer -%} | |
|
25 | {%- endblock footer -%} |
@@ -0,0 +1,37 b'' | |||
|
1 | {%- extends 'basic.tpl' -%} | |
|
2 | ||
|
3 | {% block codecell scoped %} | |
|
4 | # In[{{cell.prompt_number if cell.prompt_number else ' '}}]: | |
|
5 | {{ cell.input }} | |
|
6 | {% if cell.outputs %} | |
|
7 | # Out[{{cell.prompt_number}}]: | |
|
8 | {%- for output in cell.outputs -%} | |
|
9 | {% if output.output_type in ['pyout','stream']%} | |
|
10 | {{ output.text| indent | pycomment}} | |
|
11 | {% elif output.output_type in ['display_data'] %} | |
|
12 | {{"# fucking display_data"}} | |
|
13 | {% elif output.output_type in ['pyerr'] %} | |
|
14 | {% for line in output.traceback %} | |
|
15 | {{ line |indent| rm_ansi}} | |
|
16 | {%- endfor -%} | |
|
17 | {%- endif -%} | |
|
18 | {%- endfor -%} | |
|
19 | {%endif%} | |
|
20 | ||
|
21 | {% endblock codecell %} | |
|
22 | ||
|
23 | {% block markdowncell scoped %} | |
|
24 | {{ cell.source | pycomment | rm_fake}} | |
|
25 | {% endblock markdowncell %} | |
|
26 | ||
|
27 | {% block headingcell scoped %} | |
|
28 | {{ '#' * cell.level }}{{ cell.source | pycomment}} | |
|
29 | {% endblock headingcell %} | |
|
30 | ||
|
31 | {% block rawcell scoped %} | |
|
32 | {{ cell.source | pycomment }} | |
|
33 | {% endblock rawcell %} | |
|
34 | ||
|
35 | {% block unknowncell scoped %} | |
|
36 | unknown type {{cell.type}} | |
|
37 | {% endblock unknowncell %} |
General Comments 0
You need to be logged in to leave comments.
Login now