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