Show More
@@ -1,145 +1,146 b'' | |||||
1 | """Base classes for the notebook conversion pipeline. |
|
1 | """Base classes for the notebook conversion pipeline. | |
2 |
|
2 | |||
3 | This module defines Converter, from which all objects designed to implement |
|
3 | This module defines Converter, from which all objects designed to implement | |
4 | a conversion of IPython notebooks to some other format should inherit. |
|
4 | a conversion of IPython notebooks to some other format should inherit. | |
5 | """ |
|
5 | """ | |
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (c) 2012, the IPython Development Team. |
|
7 | # Copyright (c) 2012, the IPython Development Team. | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the Modified BSD License. |
|
9 | # Distributed under the terms of the Modified BSD License. | |
10 | # |
|
10 | # | |
11 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | from __future__ import print_function, absolute_import |
|
18 | from __future__ import print_function, absolute_import | |
19 |
|
19 | |||
20 | # Stdlib imports |
|
20 | # Stdlib imports | |
21 | import jinja2 |
|
21 | import jinja2 | |
22 | import codecs |
|
22 | import codecs | |
23 | import io |
|
23 | import io | |
24 | import logging |
|
24 | import logging | |
25 | import os |
|
25 | import os | |
26 | from IPython.utils import path |
|
26 | from IPython.utils import path | |
27 | import pprint |
|
27 | import pprint | |
28 | import re |
|
28 | import re | |
29 | from types import FunctionType |
|
29 | from types import FunctionType | |
30 |
|
30 | |||
31 | from jinja2 import Environment, PackageLoader, FileSystemLoader |
|
31 | from jinja2 import Environment, PackageLoader, FileSystemLoader | |
32 | env = Environment(loader=FileSystemLoader('./templates/')) |
|
32 | env = Environment(loader=FileSystemLoader('./templates/')) | |
33 |
|
33 | |||
34 | # IPython imports |
|
34 | # IPython imports | |
35 | from IPython.nbformat import current as nbformat |
|
35 | from IPython.nbformat import current as nbformat | |
36 | from IPython.config.configurable import Configurable, SingletonConfigurable |
|
36 | from IPython.config.configurable import Configurable, SingletonConfigurable | |
37 | from IPython.utils.traitlets import (List, Unicode, Type, Bool, Dict, CaselessStrEnum, |
|
37 | from IPython.utils.traitlets import (List, Unicode, Type, Bool, Dict, CaselessStrEnum, | |
38 | Any) |
|
38 | Any) | |
39 |
|
39 | |||
40 | # Our own imports |
|
40 | # Our own imports | |
41 | from IPython.utils.text import indent |
|
41 | from IPython.utils.text import indent | |
42 | from .utils import remove_ansi |
|
42 | from .utils import remove_ansi | |
43 | from markdown import markdown |
|
43 | from markdown import markdown | |
44 | from .utils import highlight,ansi2html |
|
44 | from .utils import highlight,ansi2html | |
45 | #----------------------------------------------------------------------------- |
|
45 | #----------------------------------------------------------------------------- | |
46 | # Class declarations |
|
46 | # Class declarations | |
47 | #----------------------------------------------------------------------------- |
|
47 | #----------------------------------------------------------------------------- | |
48 | def rm_fake(strng): |
|
48 | def rm_fake(strng): | |
49 | return strng.replace('/files/', '') |
|
49 | return strng.replace('/files/', '') | |
50 |
|
50 | |||
51 | class ConversionException(Exception): |
|
51 | class ConversionException(Exception): | |
52 | pass |
|
52 | pass | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | def python_comment(string): |
|
55 | def python_comment(string): | |
56 | return '# '+'\n# '.join(string.split('\n')) |
|
56 | return '# '+'\n# '.join(string.split('\n')) | |
57 |
|
57 | |||
58 |
|
58 | |||
59 |
|
59 | |||
60 | def header_body(): |
|
60 | def header_body(): | |
61 | """Return the body of the header as a list of strings.""" |
|
61 | """Return the body of the header as a list of strings.""" | |
62 |
|
62 | |||
63 | from pygments.formatters import HtmlFormatter |
|
63 | from pygments.formatters import HtmlFormatter | |
64 |
|
64 | |||
65 | header = [] |
|
65 | header = [] | |
66 | static = os.path.join(path.get_ipython_package_dir(), |
|
66 | static = os.path.join(path.get_ipython_package_dir(), | |
67 | 'frontend', 'html', 'notebook', 'static', |
|
67 | 'frontend', 'html', 'notebook', 'static', | |
68 | ) |
|
68 | ) | |
69 | here = os.path.split(os.path.realpath(__file__))[0] |
|
69 | here = os.path.split(os.path.realpath(__file__))[0] | |
70 | css = os.path.join(static, 'css') |
|
70 | css = os.path.join(static, 'css') | |
71 | for sheet in [ |
|
71 | for sheet in [ | |
72 | # do we need jquery and prettify? |
|
72 | # do we need jquery and prettify? | |
73 | # os.path.join(static, 'jquery', 'css', 'themes', 'base', |
|
73 | # os.path.join(static, 'jquery', 'css', 'themes', 'base', | |
74 | # 'jquery-ui.min.css'), |
|
74 | # 'jquery-ui.min.css'), | |
75 | # os.path.join(static, 'prettify', 'prettify.css'), |
|
75 | # os.path.join(static, 'prettify', 'prettify.css'), | |
76 | os.path.join(css, 'boilerplate.css'), |
|
76 | os.path.join(css, 'boilerplate.css'), | |
77 | os.path.join(css, 'fbm.css'), |
|
77 | os.path.join(css, 'fbm.css'), | |
78 | os.path.join(css, 'notebook.css'), |
|
78 | os.path.join(css, 'notebook.css'), | |
79 | os.path.join(css, 'renderedhtml.css'), |
|
79 | os.path.join(css, 'renderedhtml.css'), | |
80 | # our overrides: |
|
80 | # our overrides: | |
81 | os.path.join(here, '..', 'css', 'static_html.css'), |
|
81 | os.path.join(here, '..', 'css', 'static_html.css'), | |
82 | ]: |
|
82 | ]: | |
83 |
|
83 | |||
84 | with io.open(sheet, encoding='utf-8') as f: |
|
84 | with io.open(sheet, encoding='utf-8') as f: | |
85 | s = f.read() |
|
85 | s = f.read() | |
86 |
header. |
|
86 | header.append(s) | |
87 |
|
87 | |||
88 | pygments_css = HtmlFormatter().get_style_defs('.highlight') |
|
88 | pygments_css = HtmlFormatter().get_style_defs('.highlight') | |
89 |
header. |
|
89 | header.append(pygments_css) | |
90 | return header |
|
90 | return header | |
91 |
|
91 | |||
92 | ecss = header_body() |
|
92 | inlining= {} | |
|
93 | inlining['css'] = header_body() | |||
93 |
|
94 | |||
94 | env.filters['pycomment'] = python_comment |
|
95 | env.filters['pycomment'] = python_comment | |
95 | env.filters['indent'] = indent |
|
96 | env.filters['indent'] = indent | |
96 | env.filters['rm_fake'] = rm_fake |
|
97 | env.filters['rm_fake'] = rm_fake | |
97 | env.filters['rm_ansi'] = remove_ansi |
|
98 | env.filters['rm_ansi'] = remove_ansi | |
98 | env.filters['markdown'] = markdown |
|
99 | env.filters['markdown'] = markdown | |
99 | env.filters['highlight'] = highlight |
|
100 | env.filters['highlight'] = highlight | |
100 | env.filters['ansi2html'] = ansi2html |
|
101 | env.filters['ansi2html'] = ansi2html | |
101 |
|
102 | |||
102 | class ConverterTemplate(Configurable): |
|
103 | class ConverterTemplate(Configurable): | |
103 |
|
104 | |||
104 | display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text'] |
|
105 | display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text'] | |
105 | #------------------------------------------------------------------------- |
|
106 | #------------------------------------------------------------------------- | |
106 | # Instance-level attributes that are set in the constructor for this |
|
107 | # Instance-level attributes that are set in the constructor for this | |
107 | # class. |
|
108 | # class. | |
108 | #------------------------------------------------------------------------- |
|
109 | #------------------------------------------------------------------------- | |
109 | infile = Any() |
|
110 | infile = Any() | |
110 |
|
111 | |||
111 |
|
112 | |||
112 | infile_dir = Unicode() |
|
113 | infile_dir = Unicode() | |
113 |
|
114 | |||
114 | def __init__(self, tplfile='fullhtml', config=None, **kw): |
|
115 | def __init__(self, tplfile='fullhtml', config=None, **kw): | |
115 | self.template = env.get_template(tplfile+'.tpl') |
|
116 | self.template = env.get_template(tplfile+'.tpl') | |
116 | super(ConverterTemplate,self).__init__(config=config) |
|
117 | super(ConverterTemplate,self).__init__(config=config) | |
117 |
|
118 | |||
118 | def _get_prompt_number(self, cell): |
|
119 | def _get_prompt_number(self, cell): | |
119 | return cell.prompt_number if hasattr(cell, 'prompt_number') \ |
|
120 | return cell.prompt_number if hasattr(cell, 'prompt_number') \ | |
120 | else self.blank_symbol |
|
121 | else self.blank_symbol | |
121 |
|
122 | |||
122 |
|
123 | |||
123 | def process(self): |
|
124 | def process(self): | |
124 | converted_cells = [] |
|
125 | converted_cells = [] | |
125 | for worksheet in self.nb.worksheets: |
|
126 | for worksheet in self.nb.worksheets: | |
126 | for cell in worksheet.cells: |
|
127 | for cell in worksheet.cells: | |
127 | cell.type = cell.cell_type |
|
128 | cell.type = cell.cell_type | |
128 | cell.haspyout = False |
|
129 | cell.haspyout = False | |
129 | for out in cell.get('outputs',[]): |
|
130 | for out in cell.get('outputs',[]): | |
130 | if out.output_type == 'pyout': |
|
131 | if out.output_type == 'pyout': | |
131 | cell.haspyout = True |
|
132 | cell.haspyout = True | |
132 | break |
|
133 | break | |
133 | converted_cells.append(worksheet) |
|
134 | converted_cells.append(worksheet) | |
134 |
|
135 | |||
135 | return converted_cells |
|
136 | return converted_cells | |
136 |
|
137 | |||
137 | def convert(self, cell_separator='\n'): |
|
138 | def convert(self, cell_separator='\n'): | |
138 |
return self.template.render(worksheets=self.process(), |
|
139 | return self.template.render(worksheets=self.process(), inlining=inlining) | |
139 |
|
140 | |||
140 |
|
141 | |||
141 | def read(self, filename): |
|
142 | def read(self, filename): | |
142 | "read and parse notebook into NotebookNode called self.nb" |
|
143 | "read and parse notebook into NotebookNode called self.nb" | |
143 | with io.open(filename) as f: |
|
144 | with io.open(filename) as f: | |
144 | self.nb = nbformat.read(f, 'json') |
|
145 | self.nb = nbformat.read(f, 'json') | |
145 |
|
146 |
@@ -1,43 +1,45 b'' | |||||
1 | {%- extends 'basichtml.tpl' -%} |
|
1 | {%- extends 'basichtml.tpl' -%} | |
2 |
|
2 | |||
3 | {%- block header -%} |
|
3 | {%- block header -%} | |
4 | <html> |
|
4 | <html> | |
5 | <head> |
|
5 | <head> | |
|
6 | <meta charset="UTF-8"> | |||
|
7 | {% for css in inlining.css -%} | |||
6 | <style type="text/css"> |
|
8 | <style type="text/css"> | |
7 | {% for css in ecss %}{{css}} |
|
9 | {{css}} | |
8 | {% endfor %} |
|
|||
9 | </style> |
|
10 | </style> | |
|
11 | {% endfor %} | |||
10 | <script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" type="text/javascript"> |
|
12 | <script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" type="text/javascript"> | |
11 |
|
13 | |||
12 | </script> |
|
14 | </script> | |
13 | <script type="text/javascript"> |
|
15 | <script type="text/javascript"> | |
14 | init_mathjax = function() { |
|
16 | init_mathjax = function() { | |
15 | if (window.MathJax) { |
|
17 | if (window.MathJax) { | |
16 | // MathJax loaded |
|
18 | // MathJax loaded | |
17 | MathJax.Hub.Config({ |
|
19 | MathJax.Hub.Config({ | |
18 | tex2jax: { |
|
20 | tex2jax: { | |
19 | inlineMath: [ ['$','$'], ["\\(","\\)"] ], |
|
21 | inlineMath: [ ['$','$'], ["\\(","\\)"] ], | |
20 | displayMath: [ ['$$','$$'], ["\\[","\\]"] ] |
|
22 | displayMath: [ ['$$','$$'], ["\\[","\\]"] ] | |
21 | }, |
|
23 | }, | |
22 | displayAlign: 'left', // Change this to 'center' to center equations. |
|
24 | displayAlign: 'left', // Change this to 'center' to center equations. | |
23 | "HTML-CSS": { |
|
25 | "HTML-CSS": { | |
24 | styles: {'.MathJax_Display': {"margin": 0}} |
|
26 | styles: {'.MathJax_Display': {"margin": 0}} | |
25 | } |
|
27 | } | |
26 | }); |
|
28 | }); | |
27 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
29 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | |
28 | } |
|
30 | } | |
29 | } |
|
31 | } | |
30 | init_mathjax(); |
|
32 | init_mathjax(); | |
31 | </script> |
|
33 | </script> | |
32 | </head> |
|
34 | </head> | |
33 | {%- endblock header -%} |
|
35 | {%- endblock header -%} | |
34 |
|
36 | |||
35 |
|
37 | |||
36 | {% block body %} |
|
38 | {% block body %} | |
37 | <body>{{ super() }} |
|
39 | <body>{{ super() }} | |
38 | </body> |
|
40 | </body> | |
39 | {%- endblock body %} |
|
41 | {%- endblock body %} | |
40 |
|
42 | |||
41 |
|
43 | |||
42 | {% block footer %} |
|
44 | {% block footer %} | |
43 | </html>{% endblock footer %} |
|
45 | </html>{% endblock footer %} |
General Comments 0
You need to be logged in to leave comments.
Login now