Show More
@@ -48,24 +48,24 JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] | |||
|
48 | 48 | |
|
49 | 49 | default_filters = { |
|
50 | 50 | 'indent': indent, |
|
51 | 'markdown': filters.markdown2html, | |
|
51 | 'markdown2html': filters.markdown2html, | |
|
52 | 52 | 'ansi2html': filters.ansi2html, |
|
53 | 53 | 'filter_data_type': filters.DataTypeFilter, |
|
54 | 54 | 'get_lines': filters.get_lines, |
|
55 | 'highlight': filters.highlight, | |
|
56 | 'highlight2html': filters.highlight, | |
|
55 | 'highlight2html': filters.highlight2html, | |
|
57 | 56 | 'highlight2latex': filters.highlight2latex, |
|
58 | 57 | 'markdown2latex': filters.markdown2latex, |
|
59 | 58 | 'markdown2rst': filters.markdown2rst, |
|
60 |
' |
|
|
61 |
' |
|
|
62 |
' |
|
|
63 |
' |
|
|
64 |
'html |
|
|
59 | 'comment_lines': filters.comment_lines, | |
|
60 | 'strip_ansi': filters.strip_ansi, | |
|
61 | 'strip_dollars': filters.strip_dollars, | |
|
62 | 'strip_files_prefix': filters.strip_files_prefix, | |
|
63 | 'html2text' : filters.html2text, | |
|
65 | 64 | 'add_anchor': filters.add_anchor, |
|
66 | 65 | 'ansi2latex': filters.ansi2latex, |
|
67 |
' |
|
|
68 | 'wrap': filters.wrap | |
|
66 | 'strip_math_space': filters.strip_math_space, | |
|
67 | 'wrap_text': filters.wrap_text, | |
|
68 | 'escape_tex': filters.escape_latex | |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | #----------------------------------------------------------------------------- |
@@ -68,20 +68,6 class LatexExporter(Exporter): | |||
|
68 | 68 | #Extension that the template files use. |
|
69 | 69 | template_extension = Unicode(".tplx", config=True) |
|
70 | 70 | |
|
71 | ||
|
72 | def _init_filters(self): | |
|
73 | """ | |
|
74 | Register all of the filters required for the exporter. | |
|
75 | """ | |
|
76 | ||
|
77 | #Register the filters of the base class. | |
|
78 | super(LatexExporter, self)._init_filters() | |
|
79 | ||
|
80 | #Add latex filters to the Jinja2 environment | |
|
81 | self.register_filter('escape_tex', filters.escape_latex) | |
|
82 | self.register_filter('highlight', filters.highlight2latex) | |
|
83 | ||
|
84 | ||
|
85 | 71 | @property |
|
86 | 72 | def default_config(self): |
|
87 | 73 | c = Config({ |
@@ -20,13 +20,13 from IPython.utils import coloransi | |||
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | 22 | __all__ = [ |
|
23 |
' |
|
|
23 | 'strip_ansi', | |
|
24 | 24 | 'ansi2html', |
|
25 | 25 | 'single_ansi2latex', |
|
26 | 26 | 'ansi2latex' |
|
27 | 27 | ] |
|
28 | 28 | |
|
29 |
def |
|
|
29 | def strip_ansi(source): | |
|
30 | 30 | """ |
|
31 | 31 | Remove ansi from text |
|
32 | 32 |
@@ -33,12 +33,12 MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] | |||
|
33 | 33 | #----------------------------------------------------------------------------- |
|
34 | 34 | |
|
35 | 35 | __all__ = [ |
|
36 | 'highlight', | |
|
36 | 'highlight2html', | |
|
37 | 37 | 'highlight2latex' |
|
38 | 38 | ] |
|
39 | 39 | |
|
40 | 40 | |
|
41 | def highlight(source, language='ipython'): | |
|
41 | def highlight2html(source, language='ipython'): | |
|
42 | 42 | """ |
|
43 | 43 | Return a syntax-highlighted version of the input source as html output. |
|
44 | 44 |
@@ -36,7 +36,7 LATEX_SUBS = ( | |||
|
36 | 36 | |
|
37 | 37 | __all__ = [ |
|
38 | 38 | 'escape_latex', |
|
39 |
' |
|
|
39 | 'strip_math_space' | |
|
40 | 40 | ] |
|
41 | 41 | |
|
42 | 42 | |
@@ -55,7 +55,7 def escape_latex(text): | |||
|
55 | 55 | return return_text |
|
56 | 56 | |
|
57 | 57 | |
|
58 |
def |
|
|
58 | def strip_math_space(text): | |
|
59 | 59 | """ |
|
60 | 60 | Remove the space between latex math commands and enclosing $ symbols. |
|
61 | 61 | This filter is important because latex isn't as flexible as the notebook |
@@ -26,17 +26,17 from IPython.utils import py3compat | |||
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | |
|
28 | 28 | __all__ = [ |
|
29 | 'wrap', | |
|
30 |
'html |
|
|
29 | 'wrap_text', | |
|
30 | 'html2text', | |
|
31 | 31 | 'add_anchor', |
|
32 | 32 | 'strip_dollars', |
|
33 | 'rm_fake', | |
|
34 |
' |
|
|
33 | 'strip_files_prefix', | |
|
34 | 'comment_lines', | |
|
35 | 35 | 'get_lines' |
|
36 | 36 | ] |
|
37 | 37 | |
|
38 | 38 | |
|
39 | def wrap(text, width=100): | |
|
39 | def wrap_text(text, width=100): | |
|
40 | 40 | """ |
|
41 | 41 | Intelligently wrap text. |
|
42 | 42 | Wrap text without breaking words if possible. |
@@ -55,7 +55,7 def wrap(text, width=100): | |||
|
55 | 55 | return '\n'.join(wrpd) |
|
56 | 56 | |
|
57 | 57 | |
|
58 |
def html |
|
|
58 | def html2text(element): | |
|
59 | 59 | """extract inner text from html |
|
60 | 60 | |
|
61 | 61 | Analog of jQuery's $(element).text() |
@@ -65,7 +65,7 def html_text(element): | |||
|
65 | 65 | |
|
66 | 66 | text = element.text or "" |
|
67 | 67 | for child in element: |
|
68 |
text += html |
|
|
68 | text += html2text(child) | |
|
69 | 69 | text += (element.tail or "") |
|
70 | 70 | return text |
|
71 | 71 | |
@@ -76,7 +76,7 def add_anchor(html): | |||
|
76 | 76 | For use in heading cells |
|
77 | 77 | """ |
|
78 | 78 | h = ElementTree.fromstring(py3compat.cast_bytes_py2(html)) |
|
79 |
link = html |
|
|
79 | link = html2text(h).replace(' ', '-') | |
|
80 | 80 | h.set('id', link) |
|
81 | 81 | a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link}) |
|
82 | 82 | a.text = u'¶' |
@@ -99,7 +99,7 def strip_dollars(text): | |||
|
99 | 99 | |
|
100 | 100 | files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/') |
|
101 | 101 | |
|
102 |
def |
|
|
102 | def strip_files_prefix(text): | |
|
103 | 103 | """ |
|
104 | 104 | Fix all fake URLs that start with `files/`, |
|
105 | 105 | stripping out the `files/` prefix. |
@@ -112,7 +112,7 def rm_fake(text): | |||
|
112 | 112 | return files_url_pattern.sub(r"\1=\2", text) |
|
113 | 113 | |
|
114 | 114 | |
|
115 | def python_comment(text): | |
|
115 | def comment_lines(text, prefix='# '): | |
|
116 | 116 | """ |
|
117 | 117 | Build a Python comment line from input text. |
|
118 | 118 | |
@@ -120,12 +120,14 def python_comment(text): | |||
|
120 | 120 | ---------- |
|
121 | 121 | text : str |
|
122 | 122 | Text to comment out. |
|
123 | prefix : str | |
|
124 | Character to append to the start of each line. | |
|
123 | 125 | """ |
|
124 | 126 | |
|
125 | 127 | #Replace line breaks with line breaks and comment symbols. |
|
126 | 128 | #Also add a comment symbol at the beginning to comment out |
|
127 | 129 | #the first line. |
|
128 |
return ' |
|
|
130 | return prefix + ('\n'+prefix).join(text.split('\n')) | |
|
129 | 131 | |
|
130 | 132 | |
|
131 | 133 | def get_lines(text, start=None,end=None): |
@@ -28,9 +28,9 class TestAnsi(TestsBase): | |||
|
28 | 28 | """Contains test functions for ansi.py""" |
|
29 | 29 | |
|
30 | 30 | |
|
31 |
def test_ |
|
|
31 | def test_strip_ansi(self): | |
|
32 | 32 | """ |
|
33 |
|
|
|
33 | strip_ansi test | |
|
34 | 34 | """ |
|
35 | 35 | correct_outputs = { |
|
36 | 36 | '%s%s%s' % (TermColors.Green, TermColors.White, TermColors.Red) : '', |
@@ -42,11 +42,11 class TestAnsi(TestsBase): | |||
|
42 | 42 | 'hello' : 'hello'} |
|
43 | 43 | |
|
44 | 44 | for inval, outval in correct_outputs.items(): |
|
45 |
yield self._try_ |
|
|
45 | yield self._try_strip_ansi, inval, outval | |
|
46 | 46 | |
|
47 | 47 | |
|
48 |
def _try_ |
|
|
49 |
assert outval == |
|
|
48 | def _try_strip_ansi(self, inval, outval): | |
|
49 | assert outval == strip_ansi(inval) | |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | def test_ansi2html(self): |
@@ -27,7 +27,7 class TestHighlight(TestsBase): | |||
|
27 | 27 | """Contains test functions for highlight.py""" |
|
28 | 28 | |
|
29 | 29 | |
|
30 | def test_highlight(source, language='ipython'): | |
|
30 | def test_highlight2html(source, language='ipython'): | |
|
31 | 31 | pass |
|
32 | 32 | |
|
33 | 33 |
@@ -34,7 +34,7 | |||
|
34 | 34 | |
|
35 | 35 | {% block input %} |
|
36 | 36 | <div class="input_area box-flex1"> |
|
37 | {{cell.input | highlight }} | |
|
37 | {{cell.input | highlight2html }} | |
|
38 | 38 | </div> |
|
39 | 39 | {%- endblock input %} |
|
40 | 40 | |
@@ -53,13 +53,13 Out[{{cell.prompt_number}}]: | |||
|
53 | 53 | |
|
54 | 54 | {% block markdowncell scoped %} |
|
55 | 55 | <div class="text_cell_render border-box-sizing rendered_html"> |
|
56 |
{{ cell.source | |
|
|
56 | {{ cell.source | strip_math_space | markdown2html | strip_files_prefix}} | |
|
57 | 57 | </div> |
|
58 | 58 | {%- endblock markdowncell %} |
|
59 | 59 | |
|
60 | 60 | {% block headingcell scoped %} |
|
61 | 61 | <div class="text_cell_render border-box-sizing rendered_html"> |
|
62 |
{{("#" * cell.level + cell.source) | replace('\n', ' ') | |
|
|
62 | {{("#" * cell.level + cell.source) | replace('\n', ' ') | strip_math_space | markdown2html | strip_files_prefix | add_anchor }} | |
|
63 | 63 | </div> |
|
64 | 64 | {% endblock headingcell %} |
|
65 | 65 |
@@ -30,7 +30,7 it introduces a new line | |||
|
30 | 30 | ((* endblock pyerr *)) |
|
31 | 31 | |
|
32 | 32 | ((* block traceback_line *)) |
|
33 |
((( line |indent| |
|
|
33 | ((( line |indent| strip_ansi )))((* endblock traceback_line *)) | |
|
34 | 34 | ((= .... =)) |
|
35 | 35 | |
|
36 | 36 | |
@@ -79,7 +79,7 it introduces a new line | |||
|
79 | 79 | |
|
80 | 80 | ((* block data_latex -*)) |
|
81 | 81 | ((*- if output.latex.startswith('$'): -*)) \begin{equation*} |
|
82 |
((( output.latex | |
|
|
82 | ((( output.latex | strip_dollars))) | |
|
83 | 83 | \end{equation*} |
|
84 | 84 | ((*- else -*)) ((( output.latex ))) ((*- endif *)) |
|
85 | 85 | ((* endblock *)) |
@@ -98,7 +98,7 it introduces a new line | |||
|
98 | 98 | ((* endblock headingcell *)) |
|
99 | 99 | |
|
100 | 100 | ((* block rawcell scoped *)) |
|
101 |
((( cell.source | |
|
|
101 | ((( cell.source | comment_lines ))) | |
|
102 | 102 | ((* endblock rawcell *)) |
|
103 | 103 | |
|
104 | 104 | ((* block unknowncell scoped *)) |
@@ -240,7 +240,7 Note: For best display, use latex syntax highlighting. =)) | |||
|
240 | 240 | % will not get touched by the templating system. |
|
241 | 241 | %============================================================================== |
|
242 | 242 | ((*- block rawcell *)) |
|
243 | ((( cell.source | wrap(wrap_size) ))) | |
|
243 | ((( cell.source | wrap_text(wrap_size) ))) | |
|
244 | 244 | ((* endblock rawcell -*)) |
|
245 | 245 | |
|
246 | 246 | %============================================================================== |
@@ -253,7 +253,7 Note: For best display, use latex syntax highlighting. =)) | |||
|
253 | 253 | ((* block unknowncell scoped*)) |
|
254 | 254 | |
|
255 | 255 | % Unsupported cell type, no formatting |
|
256 | ((( cell.source | wrap | escape_tex ))) | |
|
256 | ((( cell.source | wrap_text | escape_tex ))) | |
|
257 | 257 | ((* endblock unknowncell *)) |
|
258 | 258 | |
|
259 | 259 | %============================================================================== |
@@ -273,7 +273,7 Note: For best display, use latex syntax highlighting. =)) | |||
|
273 | 273 | \vspace{-25pt} |
|
274 | 274 | |
|
275 | 275 | % Add contents below. |
|
276 | ((( cell.input | highlight ))) | |
|
276 | ((( cell.input | highlight2latex ))) | |
|
277 | 277 | |
|
278 | 278 | ((* elif resources.sphinx.outputstyle == 'notebook' *)) |
|
279 | 279 | \vspace{6pt} |
@@ -281,7 +281,7 Note: For best display, use latex syntax highlighting. =)) | |||
|
281 | 281 | \vspace{-2.65\baselineskip} |
|
282 | 282 | \begin{ColorVerbatim} |
|
283 | 283 | \vspace{-0.7\baselineskip} |
|
284 | ((( cell.input | highlight ))) | |
|
284 | ((( cell.input | highlight2latex ))) | |
|
285 | 285 | ((* if cell.input == None or cell.input == '' *)) |
|
286 | 286 | \vspace{0.3\baselineskip} |
|
287 | 287 | ((* else *)) |
@@ -340,7 +340,7 Note: For best display, use latex syntax highlighting. =)) | |||
|
340 | 340 | ((* endblock *)) |
|
341 | 341 | |
|
342 | 342 | ((* block traceback_line *)) |
|
343 |
((( conditionally_center_output(line | indent| |
|
|
343 | ((( conditionally_center_output(line | indent| strip_ansi) ))) | |
|
344 | 344 | ((* endblock traceback_line *)) |
|
345 | 345 | |
|
346 | 346 | %============================================================================== |
@@ -363,7 +363,7 Note: For best display, use latex syntax highlighting. =)) | |||
|
363 | 363 | ((*- endblock -*)) |
|
364 | 364 | |
|
365 | 365 | ((*- block data_latex *)) |
|
366 |
((* if resources.sphinx.centeroutput *))\begin{center}((* endif -*))((( output.latex | |
|
|
366 | ((* if resources.sphinx.centeroutput *))\begin{center}((* endif -*))((( output.latex | strip_math_space )))((*- if resources.sphinx.centeroutput *))\end{center} ((* endif -*)) | |
|
367 | 367 | ((*- endblock -*)) |
|
368 | 368 | |
|
369 | 369 | %============================================================================== |
@@ -429,7 +429,7 Note: For best display, use latex syntax highlighting. =)) | |||
|
429 | 429 | ((* macro custom_verbatim(text) -*)) |
|
430 | 430 | \begin{alltt} |
|
431 | 431 | ((*- if resources.sphinx.centeroutput *))\begin{center} ((* endif -*)) |
|
432 | ((( text | wrap(wrap_size) ))) | |
|
432 | ((( text | wrap_text(wrap_size) ))) | |
|
433 | 433 | ((*- if resources.sphinx.centeroutput *))\end{center}((* endif -*)) |
|
434 | 434 | \end{alltt} |
|
435 | 435 | ((*- endmacro *)) |
@@ -16,7 +16,7 In[{{cell.prompt_number if cell.prompt_number else ' '}}]:{% endblock in_prompt | |||
|
16 | 16 | {% endblock pyerr %} |
|
17 | 17 | |
|
18 | 18 | {% block traceback_line %} |
|
19 |
{{ line |indent| |
|
|
19 | {{ line |indent| strip_ansi }}{% endblock traceback_line %} | |
|
20 | 20 | |
|
21 | 21 | {% block pyout %} |
|
22 | 22 | {% block data_priority scoped %}{{ super()}}{% endblock %} |
@@ -56,7 +56,7 $$ | |||
|
56 | 56 | {% endblock data_text %} |
|
57 | 57 | |
|
58 | 58 | {% block markdowncell scoped %} |
|
59 | {{ cell.source | wrap(80)}} | |
|
59 | {{ cell.source | wrap_text(80)}} | |
|
60 | 60 | {% endblock markdowncell %} |
|
61 | 61 | |
|
62 | 62 | {% block headingcell scoped %} |
@@ -20,16 +20,16 it introduces a new line | |||
|
20 | 20 | {% endblock pyerr %} |
|
21 | 21 | |
|
22 | 22 | {% block traceback_line %} |
|
23 |
{{ line |indent| |
|
|
23 | {{ line |indent| strip_ansi }}{% endblock traceback_line %} | |
|
24 | 24 | {# .... #} |
|
25 | 25 | |
|
26 | 26 | |
|
27 | 27 | {% block pyout %} |
|
28 |
{{ output.text| indent | |
|
|
28 | {{ output.text| indent | comment_lines }} | |
|
29 | 29 | {% endblock pyout %} |
|
30 | 30 | |
|
31 | 31 | {% block stream %} |
|
32 |
{{ output.text| indent | |
|
|
32 | {{ output.text| indent | comment_lines }} | |
|
33 | 33 | {% endblock stream %} |
|
34 | 34 | |
|
35 | 35 | |
@@ -40,15 +40,15 it introduces a new line | |||
|
40 | 40 | {% endblock display_data %} |
|
41 | 41 | |
|
42 | 42 | {% block markdowncell scoped %} |
|
43 |
{{ cell.source | |
|
|
43 | {{ cell.source | comment_lines }} | |
|
44 | 44 | {% endblock markdowncell %} |
|
45 | 45 | |
|
46 | 46 | {% block headingcell scoped %} |
|
47 |
{{ '#' * cell.level }}{{ cell.source | replace('\n', ' ') | |
|
|
47 | {{ '#' * cell.level }}{{ cell.source | replace('\n', ' ') | comment_lines }} | |
|
48 | 48 | {% endblock headingcell %} |
|
49 | 49 | |
|
50 | 50 | {% block rawcell scoped %} |
|
51 |
{{ cell.source | |
|
|
51 | {{ cell.source | comment_lines }} | |
|
52 | 52 | {% endblock rawcell %} |
|
53 | 53 | |
|
54 | 54 | {% block unknowncell scoped %} |
@@ -18,7 +18,7 Out[{{cell.prompt_number}}]:{% endif %}{% endblock output_prompt %} | |||
|
18 | 18 | {% endblock pyerr %} |
|
19 | 19 | |
|
20 | 20 | {% block traceback_line %} |
|
21 |
{{ line |indent| |
|
|
21 | {{ line |indent| strip_ansi }}{% endblock traceback_line %} | |
|
22 | 22 | |
|
23 | 23 | {% block pyout %} |
|
24 | 24 | {% block data_priority scoped %}{{ super()}}{% endblock %} |
@@ -49,5 +49,5 class LatexTransformer(Transformer): | |||
|
49 | 49 | #if spaces exist between the ampersands and the math content. |
|
50 | 50 | #See filters.latex.rm_math_space for more information. |
|
51 | 51 | if hasattr(cell, "source") and cell.cell_type == "markdown": |
|
52 |
cell.source = filters. |
|
|
52 | cell.source = filters.strip_math_space(cell.source) | |
|
53 | 53 | return cell, resources |
General Comments 0
You need to be logged in to leave comments.
Login now