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