From 75f7bb9c79fc8172a5eb732f4c6ccc7d62553a29 2013-07-25 17:41:08 From: Matthias Bussonnier Date: 2013-07-25 17:41:08 Subject: [PATCH] Merge pull request #3758 from jdfreder/filter_refact nbconvert: Filter names cleanup highlight -> highlight2foo markdown -> markdown2html python_comment -> comment_lines rm_ansi -> strip_ansi rm_dollars -> strip_dollars rm_fake -> strip_files_prefix html_text -> html2text rm_math_space -> strip_math_space wrap -> wrap_text --- diff --git a/IPython/nbconvert/exporters/exporter.py b/IPython/nbconvert/exporters/exporter.py index 921312a..641b297 100755 --- a/IPython/nbconvert/exporters/exporter.py +++ b/IPython/nbconvert/exporters/exporter.py @@ -48,24 +48,24 @@ JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] default_filters = { 'indent': indent, - 'markdown': filters.markdown2html, + 'markdown2html': filters.markdown2html, 'ansi2html': filters.ansi2html, 'filter_data_type': filters.DataTypeFilter, 'get_lines': filters.get_lines, - 'highlight': filters.highlight, - 'highlight2html': filters.highlight, + 'highlight2html': filters.highlight2html, 'highlight2latex': filters.highlight2latex, 'markdown2latex': filters.markdown2latex, 'markdown2rst': filters.markdown2rst, - 'pycomment': filters.python_comment, - 'rm_ansi': filters.remove_ansi, - 'rm_dollars': filters.strip_dollars, - 'rm_fake': filters.rm_fake, - 'html_text' : filters.html_text, + 'comment_lines': filters.comment_lines, + 'strip_ansi': filters.strip_ansi, + 'strip_dollars': filters.strip_dollars, + 'strip_files_prefix': filters.strip_files_prefix, + 'html2text' : filters.html2text, 'add_anchor': filters.add_anchor, 'ansi2latex': filters.ansi2latex, - 'rm_math_space': filters.rm_math_space, - 'wrap': filters.wrap + 'strip_math_space': filters.strip_math_space, + 'wrap_text': filters.wrap_text, + 'escape_latex': filters.escape_latex } #----------------------------------------------------------------------------- diff --git a/IPython/nbconvert/exporters/latex.py b/IPython/nbconvert/exporters/latex.py index 920e831..66751ce 100755 --- a/IPython/nbconvert/exporters/latex.py +++ b/IPython/nbconvert/exporters/latex.py @@ -68,20 +68,6 @@ class LatexExporter(Exporter): #Extension that the template files use. template_extension = Unicode(".tplx", config=True) - - def _init_filters(self): - """ - Register all of the filters required for the exporter. - """ - - #Register the filters of the base class. - super(LatexExporter, self)._init_filters() - - #Add latex filters to the Jinja2 environment - self.register_filter('escape_tex', filters.escape_latex) - self.register_filter('highlight', filters.highlight2latex) - - @property def default_config(self): c = Config({ diff --git a/IPython/nbconvert/filters/ansi.py b/IPython/nbconvert/filters/ansi.py index 5098284..2139d7a 100644 --- a/IPython/nbconvert/filters/ansi.py +++ b/IPython/nbconvert/filters/ansi.py @@ -20,13 +20,13 @@ from IPython.utils import coloransi #----------------------------------------------------------------------------- __all__ = [ - 'remove_ansi', + 'strip_ansi', 'ansi2html', 'single_ansi2latex', 'ansi2latex' ] -def remove_ansi(source): +def strip_ansi(source): """ Remove ansi from text diff --git a/IPython/nbconvert/filters/highlight.py b/IPython/nbconvert/filters/highlight.py index b506130..afb05b5 100644 --- a/IPython/nbconvert/filters/highlight.py +++ b/IPython/nbconvert/filters/highlight.py @@ -33,12 +33,12 @@ MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] #----------------------------------------------------------------------------- __all__ = [ - 'highlight', + 'highlight2html', 'highlight2latex' ] -def highlight(source, language='ipython'): +def highlight2html(source, language='ipython'): """ Return a syntax-highlighted version of the input source as html output. diff --git a/IPython/nbconvert/filters/latex.py b/IPython/nbconvert/filters/latex.py index ca4b3f8..1942ffb 100755 --- a/IPython/nbconvert/filters/latex.py +++ b/IPython/nbconvert/filters/latex.py @@ -36,7 +36,7 @@ LATEX_SUBS = ( __all__ = [ 'escape_latex', - 'rm_math_space' + 'strip_math_space' ] @@ -55,7 +55,7 @@ def escape_latex(text): return return_text -def rm_math_space(text): +def strip_math_space(text): """ Remove the space between latex math commands and enclosing $ symbols. This filter is important because latex isn't as flexible as the notebook diff --git a/IPython/nbconvert/filters/strings.py b/IPython/nbconvert/filters/strings.py index 9ab2179..99b4464 100755 --- a/IPython/nbconvert/filters/strings.py +++ b/IPython/nbconvert/filters/strings.py @@ -26,17 +26,17 @@ from IPython.utils import py3compat #----------------------------------------------------------------------------- __all__ = [ - 'wrap', - 'html_text', + 'wrap_text', + 'html2text', 'add_anchor', 'strip_dollars', - 'rm_fake', - 'python_comment', + 'strip_files_prefix', + 'comment_lines', 'get_lines' ] -def wrap(text, width=100): +def wrap_text(text, width=100): """ Intelligently wrap text. Wrap text without breaking words if possible. @@ -55,7 +55,7 @@ def wrap(text, width=100): return '\n'.join(wrpd) -def html_text(element): +def html2text(element): """extract inner text from html Analog of jQuery's $(element).text() @@ -65,7 +65,7 @@ def html_text(element): text = element.text or "" for child in element: - text += html_text(child) + text += html2text(child) text += (element.tail or "") return text @@ -76,7 +76,7 @@ def add_anchor(html): For use in heading cells """ h = ElementTree.fromstring(py3compat.cast_bytes_py2(html)) - link = html_text(h).replace(' ', '-') + link = html2text(h).replace(' ', '-') h.set('id', link) a = ElementTree.Element("a", {"class" : "anchor-link", "href" : "#" + link}) a.text = u'ΒΆ' @@ -99,7 +99,7 @@ def strip_dollars(text): files_url_pattern = re.compile(r'(src|href)\=([\'"]?)files/') -def rm_fake(text): +def strip_files_prefix(text): """ Fix all fake URLs that start with `files/`, stripping out the `files/` prefix. @@ -112,7 +112,7 @@ def rm_fake(text): return files_url_pattern.sub(r"\1=\2", text) -def python_comment(text): +def comment_lines(text, prefix='# '): """ Build a Python comment line from input text. @@ -120,12 +120,14 @@ def python_comment(text): ---------- text : str Text to comment out. + prefix : str + Character to append to the start of each line. """ #Replace line breaks with line breaks and comment symbols. #Also add a comment symbol at the beginning to comment out #the first line. - return '# '+'\n# '.join(text.split('\n')) + return prefix + ('\n'+prefix).join(text.split('\n')) def get_lines(text, start=None,end=None): diff --git a/IPython/nbconvert/filters/tests/test_ansi.py b/IPython/nbconvert/filters/tests/test_ansi.py index 55fc192..9528347 100644 --- a/IPython/nbconvert/filters/tests/test_ansi.py +++ b/IPython/nbconvert/filters/tests/test_ansi.py @@ -28,9 +28,9 @@ class TestAnsi(TestsBase): """Contains test functions for ansi.py""" - def test_remove_ansi(self): + def test_strip_ansi(self): """ - remove_ansi test + strip_ansi test """ correct_outputs = { '%s%s%s' % (TermColors.Green, TermColors.White, TermColors.Red) : '', @@ -42,11 +42,11 @@ class TestAnsi(TestsBase): 'hello' : 'hello'} for inval, outval in correct_outputs.items(): - yield self._try_remove_ansi, inval, outval + yield self._try_strip_ansi, inval, outval - def _try_remove_ansi(self, inval, outval): - assert outval == remove_ansi(inval) + def _try_strip_ansi(self, inval, outval): + assert outval == strip_ansi(inval) def test_ansi2html(self): diff --git a/IPython/nbconvert/filters/tests/test_highlight.py b/IPython/nbconvert/filters/tests/test_highlight.py index 61a7076..d0b5c88 100644 --- a/IPython/nbconvert/filters/tests/test_highlight.py +++ b/IPython/nbconvert/filters/tests/test_highlight.py @@ -27,7 +27,7 @@ class TestHighlight(TestsBase): """Contains test functions for highlight.py""" - def test_highlight(source, language='ipython'): + def test_highlight2html(source, language='ipython'): pass diff --git a/IPython/nbconvert/templates/basichtml.tpl b/IPython/nbconvert/templates/basichtml.tpl index 2fd6f0e..e879c60 100644 --- a/IPython/nbconvert/templates/basichtml.tpl +++ b/IPython/nbconvert/templates/basichtml.tpl @@ -34,7 +34,7 @@ {% block input %}
-{{cell.input | highlight }} +{{cell.input | highlight2html }}
{%- endblock input %} @@ -53,13 +53,13 @@ Out[{{cell.prompt_number}}]: {% block markdowncell scoped %}
-{{ cell.source | markdown| rm_fake}} +{{ cell.source | strip_math_space | markdown2html | strip_files_prefix}}
{%- endblock markdowncell %} {% block headingcell scoped %}
- {{("#" * cell.level + cell.source) | replace('\n', ' ') | markdown | rm_fake | add_anchor }} + {{("#" * cell.level + cell.source) | replace('\n', ' ') | strip_math_space | markdown2html | strip_files_prefix | add_anchor }}
{% endblock headingcell %} diff --git a/IPython/nbconvert/templates/latex/base.tplx b/IPython/nbconvert/templates/latex/base.tplx index 5f2ebfb..f04bc20 100644 --- a/IPython/nbconvert/templates/latex/base.tplx +++ b/IPython/nbconvert/templates/latex/base.tplx @@ -30,7 +30,7 @@ it introduces a new line ((* endblock pyerr *)) ((* block traceback_line *)) -((( line |indent| rm_ansi )))((* endblock traceback_line *)) +((( line |indent| strip_ansi )))((* endblock traceback_line *)) ((= .... =)) @@ -79,7 +79,7 @@ it introduces a new line ((* block data_latex -*)) ((*- if output.latex.startswith('$'): -*)) \begin{equation*} - ((( output.latex | rm_dollars))) + ((( output.latex | strip_dollars))) \end{equation*} ((*- else -*)) ((( output.latex ))) ((*- endif *)) ((* endblock *)) @@ -98,7 +98,7 @@ it introduces a new line ((* endblock headingcell *)) ((* block rawcell scoped *)) -((( cell.source | pycomment ))) +((( cell.source | comment_lines ))) ((* endblock rawcell *)) ((* block unknowncell scoped *)) diff --git a/IPython/nbconvert/templates/latex/sphinx_base.tplx b/IPython/nbconvert/templates/latex/sphinx_base.tplx index f3b8268..1fd248b 100644 --- a/IPython/nbconvert/templates/latex/sphinx_base.tplx +++ b/IPython/nbconvert/templates/latex/sphinx_base.tplx @@ -145,11 +145,11 @@ Note: For best display, use latex syntax highlighting. =)) \sloppy % Document level variables - \title{((( resources.metadata.name | escape_tex )))} - \date{((( resources.sphinx.date | escape_tex )))} - \release{((( resources.sphinx.version | escape_tex )))} - \author{((( resources.sphinx.author | escape_tex )))} - \renewcommand{\releasename}{((( resources.sphinx.release | escape_tex )))} + \title{((( resources.metadata.name | escape_latex )))} + \date{((( resources.sphinx.date | escape_latex )))} + \release{((( resources.sphinx.version | escape_latex )))} + \author{((( resources.sphinx.author | escape_latex )))} + \renewcommand{\releasename}{((( resources.sphinx.release | escape_latex )))} % TODO: Add option for the user to specify a logo for his/her export. \newcommand{\sphinxlogo}{} @@ -240,7 +240,7 @@ Note: For best display, use latex syntax highlighting. =)) % will not get touched by the templating system. %============================================================================== ((*- block rawcell *)) - ((( cell.source | wrap(wrap_size) ))) + ((( cell.source | wrap_text(wrap_size) ))) ((* endblock rawcell -*)) %============================================================================== @@ -253,7 +253,7 @@ Note: For best display, use latex syntax highlighting. =)) ((* block unknowncell scoped*)) % Unsupported cell type, no formatting - ((( cell.source | wrap | escape_tex ))) + ((( cell.source | wrap_text | escape_latex ))) ((* endblock unknowncell *)) %============================================================================== @@ -273,7 +273,7 @@ Note: For best display, use latex syntax highlighting. =)) \vspace{-25pt} % Add contents below. - ((( cell.input | highlight ))) + ((( cell.input | highlight2latex ))) ((* elif resources.sphinx.outputstyle == 'notebook' *)) \vspace{6pt} @@ -281,7 +281,7 @@ Note: For best display, use latex syntax highlighting. =)) \vspace{-2.65\baselineskip} \begin{ColorVerbatim} \vspace{-0.7\baselineskip} - ((( cell.input | highlight ))) + ((( cell.input | highlight2latex ))) ((* if cell.input == None or cell.input == '' *)) \vspace{0.3\baselineskip} ((* else *)) @@ -340,7 +340,7 @@ Note: For best display, use latex syntax highlighting. =)) ((* endblock *)) ((* block traceback_line *)) - ((( conditionally_center_output(line | indent| rm_ansi) ))) + ((( conditionally_center_output(line | indent| strip_ansi) ))) ((* endblock traceback_line *)) %============================================================================== @@ -363,7 +363,7 @@ Note: For best display, use latex syntax highlighting. =)) ((*- endblock -*)) ((*- block data_latex *)) - ((* if resources.sphinx.centeroutput *))\begin{center}((* endif -*))((( output.latex | rm_math_space )))((*- if resources.sphinx.centeroutput *))\end{center} ((* endif -*)) + ((* if resources.sphinx.centeroutput *))\begin{center}((* endif -*))((( output.latex | strip_math_space )))((*- if resources.sphinx.centeroutput *))\end{center} ((* endif -*)) ((*- endblock -*)) %============================================================================== @@ -429,7 +429,7 @@ Note: For best display, use latex syntax highlighting. =)) ((* macro custom_verbatim(text) -*)) \begin{alltt} ((*- if resources.sphinx.centeroutput *))\begin{center} ((* endif -*)) -((( text | wrap(wrap_size) ))) +((( text | wrap_text(wrap_size) ))) ((*- if resources.sphinx.centeroutput *))\end{center}((* endif -*)) \end{alltt} ((*- endmacro *)) diff --git a/IPython/nbconvert/templates/markdown.tpl b/IPython/nbconvert/templates/markdown.tpl index 361df1a..5768462 100644 --- a/IPython/nbconvert/templates/markdown.tpl +++ b/IPython/nbconvert/templates/markdown.tpl @@ -16,7 +16,7 @@ In[{{cell.prompt_number if cell.prompt_number else ' '}}]:{% endblock in_prompt {% endblock pyerr %} {% block traceback_line %} -{{ line |indent| rm_ansi }}{% endblock traceback_line %} +{{ line |indent| strip_ansi }}{% endblock traceback_line %} {% block pyout %} {% block data_priority scoped %}{{ super()}}{% endblock %} @@ -56,7 +56,7 @@ $$ {% endblock data_text %} {% block markdowncell scoped %} -{{ cell.source | wrap(80)}} +{{ cell.source | wrap_text(80)}} {% endblock markdowncell %} {% block headingcell scoped %} diff --git a/IPython/nbconvert/templates/python.tpl b/IPython/nbconvert/templates/python.tpl index 484a64d..fe812c9 100644 --- a/IPython/nbconvert/templates/python.tpl +++ b/IPython/nbconvert/templates/python.tpl @@ -20,16 +20,16 @@ it introduces a new line {% endblock pyerr %} {% block traceback_line %} -{{ line |indent| rm_ansi }}{% endblock traceback_line %} +{{ line |indent| strip_ansi }}{% endblock traceback_line %} {# .... #} {% block pyout %} -{{ output.text| indent | pycomment}} +{{ output.text| indent | comment_lines }} {% endblock pyout %} {% block stream %} -{{ output.text| indent | pycomment}} +{{ output.text| indent | comment_lines }} {% endblock stream %} @@ -40,15 +40,15 @@ it introduces a new line {% endblock display_data %} {% block markdowncell scoped %} -{{ cell.source | pycomment }} +{{ cell.source | comment_lines }} {% endblock markdowncell %} {% block headingcell scoped %} -{{ '#' * cell.level }}{{ cell.source | replace('\n', ' ') | pycomment}} +{{ '#' * cell.level }}{{ cell.source | replace('\n', ' ') | comment_lines }} {% endblock headingcell %} {% block rawcell scoped %} -{{ cell.source | pycomment }} +{{ cell.source | comment_lines }} {% endblock rawcell %} {% block unknowncell scoped %} diff --git a/IPython/nbconvert/templates/rst.tpl b/IPython/nbconvert/templates/rst.tpl index f7a2d16..e3b582d 100644 --- a/IPython/nbconvert/templates/rst.tpl +++ b/IPython/nbconvert/templates/rst.tpl @@ -18,7 +18,7 @@ Out[{{cell.prompt_number}}]:{% endif %}{% endblock output_prompt %} {% endblock pyerr %} {% block traceback_line %} -{{ line |indent| rm_ansi }}{% endblock traceback_line %} +{{ line |indent| strip_ansi }}{% endblock traceback_line %} {% block pyout %} {% block data_priority scoped %}{{ super()}}{% endblock %} diff --git a/IPython/nbconvert/transformers/latex.py b/IPython/nbconvert/transformers/latex.py index be1f5ac..2940d7e 100755 --- a/IPython/nbconvert/transformers/latex.py +++ b/IPython/nbconvert/transformers/latex.py @@ -49,5 +49,5 @@ class LatexTransformer(Transformer): #if spaces exist between the ampersands and the math content. #See filters.latex.rm_math_space for more information. if hasattr(cell, "source") and cell.cell_type == "markdown": - cell.source = filters.rm_math_space(cell.source) + cell.source = filters.strip_math_space(cell.source) return cell, resources