Show More
@@ -0,0 +1,65 | |||
|
1 | #!/usr/bin/env python | |
|
2 | #----------------------------------------------------------------------------- | |
|
3 | # Imports | |
|
4 | #----------------------------------------------------------------------------- | |
|
5 | from __future__ import print_function | |
|
6 | import sys | |
|
7 | import io | |
|
8 | ||
|
9 | from converters.template import * | |
|
10 | from converters.template import ConverterTemplate | |
|
11 | from converters.html import ConverterHTML | |
|
12 | # From IPython | |
|
13 | ||
|
14 | # All the stuff needed for the configurable things | |
|
15 | from IPython.config.application import Application | |
|
16 | from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum | |
|
17 | ||
|
18 | ||
|
19 | class NbconvertApp(Application): | |
|
20 | ||
|
21 | ||
|
22 | def __init__(self, **kwargs): | |
|
23 | super(NbconvertApp, self).__init__(**kwargs) | |
|
24 | self.classes.insert(0,ConverterTemplate) | |
|
25 | # ensure those are registerd | |
|
26 | ||
|
27 | ||
|
28 | def initialize(self, argv=None): | |
|
29 | self.parse_command_line(argv) | |
|
30 | cl_config = self.config | |
|
31 | self.update_config(cl_config) | |
|
32 | ||
|
33 | ||
|
34 | ||
|
35 | def run(self): | |
|
36 | """Convert a notebook to html in one step""" | |
|
37 | template_file = (self.extra_args or [None])[0] | |
|
38 | ipynb_file = (self.extra_args or [None])[1] | |
|
39 | ||
|
40 | template_file = sys.argv[1] | |
|
41 | ||
|
42 | if template_file.startswith('latex'): | |
|
43 | tex_environement=True | |
|
44 | else: | |
|
45 | tex_environement=False | |
|
46 | ||
|
47 | C = ConverterTemplate(tplfile=sys.argv[1], tex_environement=tex_environement, config=self.config) | |
|
48 | C.read(ipynb_file) | |
|
49 | ||
|
50 | output,rest = C.convert() | |
|
51 | ||
|
52 | print(output.encode('utf-8')) | |
|
53 | ||
|
54 | def main(): | |
|
55 | """Convert a notebook to html in one step""" | |
|
56 | app = NbconvertApp.instance() | |
|
57 | app.initialize() | |
|
58 | app.start() | |
|
59 | app.run() | |
|
60 | #----------------------------------------------------------------------------- | |
|
61 | # Script main | |
|
62 | #----------------------------------------------------------------------------- | |
|
63 | ||
|
64 | if __name__ == '__main__': | |
|
65 | main() |
@@ -37,7 +37,7 texenv = Environment( | |||
|
37 | 37 | # IPython imports |
|
38 | 38 | from IPython.nbformat import current as nbformat |
|
39 | 39 | from IPython.config.configurable import Configurable |
|
40 | from IPython.utils.traitlets import ( Unicode, Any) | |
|
40 | from IPython.utils.traitlets import ( Unicode, Any, List) | |
|
41 | 41 | |
|
42 | 42 | # Our own imports |
|
43 | 43 | from IPython.utils.text import indent |
@@ -96,13 +96,10 inlining = {} | |||
|
96 | 96 | inlining['css'] = header_body() |
|
97 | 97 | |
|
98 | 98 | |
|
99 | def filter_data_type(output): | |
|
100 | for fmt in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text']: | |
|
101 | if fmt in output: | |
|
102 | return [fmt] | |
|
103 | 99 | |
|
104 | 100 | |
|
105 | env.filters['filter_data_type'] = filter_data_type | |
|
101 | ||
|
102 | ||
|
106 | 103 | env.filters['pycomment'] = python_comment |
|
107 | 104 | env.filters['indent'] = indent |
|
108 | 105 | env.filters['rm_fake'] = rm_fake |
@@ -136,7 +133,6 texenv.comment_start_string = '((=' | |||
|
136 | 133 | texenv.comment_end_string = '=))' |
|
137 | 134 | texenv.filters['escape_tex'] = escape_tex |
|
138 | 135 | |
|
139 | texenv.filters['filter_data_type'] = filter_data_type | |
|
140 | 136 | texenv.filters['pycomment'] = python_comment |
|
141 | 137 | texenv.filters['indent'] = indent |
|
142 | 138 | texenv.filters['rm_fake'] = rm_fake |
@@ -164,7 +160,13 def haspyout_transformer(nb,_): | |||
|
164 | 160 | class ConverterTemplate(Configurable): |
|
165 | 161 | """ A Jinja2 base converter templates""" |
|
166 | 162 | |
|
167 | display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text'] | |
|
163 | display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'], | |
|
164 | config=True, | |
|
165 | help= """ | |
|
166 | A list of ast.NodeTransformer subclass instances, which will be applied | |
|
167 | to user input before code is run. | |
|
168 | """ | |
|
169 | ) | |
|
168 | 170 | #------------------------------------------------------------------------- |
|
169 | 171 | # Instance-level attributes that are set in the constructor for this |
|
170 | 172 | # class. |
@@ -173,6 +175,13 class ConverterTemplate(Configurable): | |||
|
173 | 175 | |
|
174 | 176 | |
|
175 | 177 | infile_dir = Unicode() |
|
178 | def display_data_priority_changed(self, name, old, new): | |
|
179 | print('== changed', name,old,new) | |
|
180 | ||
|
181 | def filter_data_type(self,output): | |
|
182 | for fmt in self.display_data_priority: | |
|
183 | if fmt in output: | |
|
184 | return [fmt] | |
|
176 | 185 | |
|
177 | 186 | def __init__(self, tplfile='fullhtml', preprocessors=[], config=None,tex_environement=False, **kw): |
|
178 | 187 | """ |
@@ -186,11 +195,13 class ConverterTemplate(Configurable): | |||
|
186 | 195 | """ |
|
187 | 196 | self.env = texenv if tex_environement else env |
|
188 | 197 | self.ext = '.tplx' if tex_environement else '.tpl' |
|
189 | self.template = self.env.get_template(tplfile+self.ext) | |
|
190 | 198 | self.nb = None |
|
191 | 199 | self.preprocessors = preprocessors |
|
192 | 200 | self.preprocessors.append(haspyout_transformer) |
|
193 | 201 | super(ConverterTemplate, self).__init__(config=config, **kw) |
|
202 | self.env.filters['filter_data_type'] = self.filter_data_type | |
|
203 | self.template = self.env.get_template(tplfile+self.ext) | |
|
204 | ||
|
194 | 205 | |
|
195 | 206 | |
|
196 | 207 | def process(self): |
@@ -6,6 +6,7 import sys | |||
|
6 | 6 | import io |
|
7 | 7 | from converters.template import * |
|
8 | 8 | |
|
9 | ||
|
9 | 10 | template_file = sys.argv[1] |
|
10 | 11 | |
|
11 | 12 | if template_file.startswith('latex'): |
@@ -14,6 +14,7 tex/%.tplx: %.tpl | |||
|
14 | 14 | -e 's/}}/)))/g' \ |
|
15 | 15 | -e 's/{#/((=/g' \ |
|
16 | 16 | -e 's/#}/=))/g' \ |
|
17 | -e "s/tpl'/tplx'/g" \ | |
|
17 | 18 | $^ >> $@ |
|
18 | 19 | |
|
19 | 20 |
@@ -1,5 +1,5 | |||
|
1 | 1 | ((= autogenerated file do not edit =)) |
|
2 | ((*- extends 'null.tpl' -*)) | |
|
2 | ((*- extends 'null.tplx' -*)) | |
|
3 | 3 | |
|
4 | 4 | ((=display data priority=)) |
|
5 | 5 |
@@ -1,15 +1,10 | |||
|
1 |
((*- extends ' |
|
|
1 | ((*- extends 'display_priority.tplx' -*)) | |
|
2 | 2 | |
|
3 | ((* block in_prompt *)) | |
|
4 | # In[(((cell.prompt_number if cell.prompt_number else ' ')))]: | |
|
5 | ((* endblock in_prompt *)) | |
|
3 | ((* block in_prompt *))((* endblock in_prompt *)) | |
|
6 | 4 | |
|
7 | ((* block output_prompt *)) | |
|
8 | # Out[(((cell.prompt_number)))]:((* endblock output_prompt *)) | |
|
5 | ((* block output_prompt *))((* endblock output_prompt *)) | |
|
9 | 6 | |
|
10 | ((* block codecell *)) | |
|
11 | \begin{codecell} | |
|
12 | ((( super() ))) | |
|
7 | ((* block codecell *))\begin{codecell}((( super() ))) | |
|
13 | 8 | \end{codecell} |
|
14 | 9 | ((* endblock *)) |
|
15 | 10 | |
@@ -35,23 +30,32 it introduces a new line | |||
|
35 | 30 | ((= .... =)) |
|
36 | 31 | |
|
37 | 32 | |
|
33 | ((*- block output_group -*)) | |
|
34 | \begin{codeoutput} | |
|
35 | ((( super() ))) | |
|
36 | \end{codeoutput}((* endblock *)) | |
|
37 | ||
|
38 | ||
|
38 | 39 | ((* block pyout *)) |
|
39 |
((( output.text |
|
|
40 | ((( output.text))) | |
|
40 | 41 | ((* endblock pyout *)) |
|
41 | 42 | |
|
43 | ((* block data_text *)) | |
|
44 | \begin{verbatim} | |
|
45 | ((( output.text ))) | |
|
46 | \end{verbatim} | |
|
47 | ((* endblock *)) | |
|
48 | ||
|
42 | 49 | ((* block stream *)) |
|
43 | ((( output.text| indent | pycomment))) | |
|
50 | \begin{verbatim} | |
|
51 | ((( output.text))) | |
|
52 | \end{verbatim} | |
|
44 | 53 | ((* endblock stream *)) |
|
45 | 54 | |
|
46 | 55 | |
|
47 | 56 | |
|
48 | 57 | |
|
49 | ((* block display_data scoped *)) | |
|
50 | # image file: | |
|
51 | ((* endblock display_data *)) | |
|
52 | ||
|
53 | ((* block markdowncell scoped *)) | |
|
54 | ((( cell.source | markdown2latex ))) | |
|
58 | ((* block markdowncell scoped *))((( cell.source | markdown2latex ))) | |
|
55 | 59 | ((* endblock markdowncell *)) |
|
56 | 60 | |
|
57 | 61 | ((* block headingcell scoped *)) |
@@ -68,8 +72,7 unknown type (((cell.type))) | |||
|
68 | 72 | |
|
69 | 73 | |
|
70 | 74 | |
|
71 | ((* block body *)) | |
|
72 | \begin{document} | |
|
75 | ((* block body *))\begin{document} | |
|
73 | 76 | ((( super() ))) |
|
74 | 77 | \end{document} |
|
75 | 78 | ((* endblock*)) |
General Comments 0
You need to be logged in to leave comments.
Login now