From 902758e2e605d57285c7ff99205826afcf57fbd3 2014-01-08 21:12:36 From: Min RK Date: 2014-01-08 21:12:36 Subject: [PATCH] Merge pull request #4727 from takluyver/nbconvert-template-magic Remove Nbconvert template loading magic based on module name. A little more explicit now, plus a few traitlets cleaned up. --- diff --git a/IPython/nbconvert/exporters/html.py b/IPython/nbconvert/exporters/html.py index 8cb8675..aca6b97 100644 --- a/IPython/nbconvert/exporters/html.py +++ b/IPython/nbconvert/exporters/html.py @@ -12,7 +12,7 @@ # Imports #----------------------------------------------------------------------------- -from IPython.utils.traitlets import Unicode, List +import os from IPython.nbconvert import preprocessors from IPython.config import Config @@ -31,17 +31,14 @@ class HTMLExporter(TemplateExporter): filters, just change the 'template_file' config option. """ - file_extension = Unicode( - 'html', config=True, - help="Extension of the file that should be written to disk" - ) + def _file_extension_default(self): + return 'html' - mime_type = Unicode('text/html', config=True, - help="MIME type of the result file, for HTTP response headers." - ) + def _default_template_path_default(self): + return os.path.join("..", "templates", "html") - default_template = Unicode('full', config=True, help="""Flavor of the data - format to use. I.E. 'full' or 'basic'""") + def _template_file_default(self): + return 'full' output_mimetype = 'text/html' diff --git a/IPython/nbconvert/exporters/latex.py b/IPython/nbconvert/exporters/latex.py index 0a94b9d..46d8e01 100644 --- a/IPython/nbconvert/exporters/latex.py +++ b/IPython/nbconvert/exporters/latex.py @@ -16,7 +16,7 @@ import os # IPython imports -from IPython.utils.traitlets import Unicode, List +from IPython.utils.traitlets import Unicode from IPython.config import Config from IPython.nbconvert import filters, preprocessors @@ -35,22 +35,19 @@ class LatexExporter(TemplateExporter): 'template_file' config option. Place your template in the special "/latex" subfolder of the "../templates" folder. """ - - file_extension = Unicode( - 'tex', config=True, - help="Extension of the file that should be written to disk") - default_template = Unicode('article', config=True, help="""Template of the - data format to use. I.E. 'article' or 'report'""") + def _file_extension_default(self): + return 'tex' + + def _template_file_default(self): + return 'article' #Latex constants - default_template_path = Unicode( - os.path.join("..", "templates", "latex"), config=True, - help="Path where the template files are located.") + def _default_template_path_default(self): + return os.path.join("..", "templates", "latex") - template_skeleton_path = Unicode( - os.path.join("..", "templates", "latex", "skeleton"), config=True, - help="Path where the template skeleton files are located.") + def _template_skeleton_path_default(self): + return os.path.join("..", "templates", "latex", "skeleton") #Special Jinja2 syntax that will not conflict when exporting latex. jinja_comment_block_start = Unicode("((=", config=True) diff --git a/IPython/nbconvert/exporters/markdown.py b/IPython/nbconvert/exporters/markdown.py index 6a888ae..28708ae 100644 --- a/IPython/nbconvert/exporters/markdown.py +++ b/IPython/nbconvert/exporters/markdown.py @@ -13,7 +13,6 @@ #----------------------------------------------------------------------------- from IPython.config import Config -from IPython.utils.traitlets import Unicode from .templateexporter import TemplateExporter @@ -26,9 +25,11 @@ class MarkdownExporter(TemplateExporter): Exports to a markdown document (.md) """ - file_extension = Unicode( - 'md', config=True, - help="Extension of the file that should be written to disk") + def _file_extension_default(self): + return 'md' + + def _template_file_default(self): + return 'markdown' output_mimetype = 'text/markdown' diff --git a/IPython/nbconvert/exporters/python.py b/IPython/nbconvert/exporters/python.py index b5a4293..5c7dbf4 100644 --- a/IPython/nbconvert/exporters/python.py +++ b/IPython/nbconvert/exporters/python.py @@ -12,8 +12,6 @@ # Imports #----------------------------------------------------------------------------- -from IPython.utils.traitlets import Unicode - from .templateexporter import TemplateExporter #----------------------------------------------------------------------------- @@ -24,9 +22,10 @@ class PythonExporter(TemplateExporter): """ Exports a Python code file. """ - - file_extension = Unicode( - 'py', config=True, - help="Extension of the file that should be written to disk") + def _file_extension_default(self): + return 'py' + + def _template_file_default(self): + return 'python' output_mimetype = 'text/x-python' diff --git a/IPython/nbconvert/exporters/rst.py b/IPython/nbconvert/exporters/rst.py index e401c99..eaae267 100644 --- a/IPython/nbconvert/exporters/rst.py +++ b/IPython/nbconvert/exporters/rst.py @@ -12,7 +12,6 @@ # Imports #----------------------------------------------------------------------------- -from IPython.utils.traitlets import Unicode from IPython.config import Config from .templateexporter import TemplateExporter @@ -26,9 +25,11 @@ class RSTExporter(TemplateExporter): Exports restructured text documents. """ - file_extension = Unicode( - 'rst', config=True, - help="Extension of the file that should be written to disk") + def _file_extension_default(self): + return 'rst' + + def _template_file_default(self): + return 'rst' output_mimetype = 'text/restructuredtext' diff --git a/IPython/nbconvert/exporters/slides.py b/IPython/nbconvert/exporters/slides.py index 1a4ebec..8f4545e 100644 --- a/IPython/nbconvert/exporters/slides.py +++ b/IPython/nbconvert/exporters/slides.py @@ -12,8 +12,6 @@ # Imports #----------------------------------------------------------------------------- -from IPython.utils.traitlets import Unicode - from IPython.nbconvert import preprocessors from IPython.config import Config @@ -26,15 +24,13 @@ from .html import HTMLExporter class SlidesExporter(HTMLExporter): """Exports HTML slides with reveal.js""" - file_extension = Unicode( - 'slides.html', config=True, - help="Extension of the file that should be written to disk" - ) + def _file_extension_default(self): + return 'slides.html' - output_mimetype = 'text/html' + def _template_file_default(self): + return 'slides_reveal' - default_template = Unicode('reveal', config=True, help="""Template of the - data format to use. I.E. 'reveal'""") + output_mimetype = 'text/html' @property def default_config(self): diff --git a/IPython/nbconvert/exporters/templateexporter.py b/IPython/nbconvert/exporters/templateexporter.py index e127ab9..f68bfea 100644 --- a/IPython/nbconvert/exporters/templateexporter.py +++ b/IPython/nbconvert/exporters/templateexporter.py @@ -173,15 +173,12 @@ class TemplateExporter(Exporter): # template by name with extension added, then try loading the template # as if the name is explicitly specified, then try the name as a # 'flavor', and lastly just try to load the template by module name. - module_name = self.__module__.rsplit('.', 1)[-1] try_names = [] if self.template_file: try_names.extend([ self.template_file + self.template_extension, self.template_file, - module_name + '_' + self.template_file + self.template_extension, ]) - try_names.append(module_name + self.template_extension) for try_name in try_names: self.log.debug("Attempting to load template %s", try_name) try: diff --git a/IPython/nbconvert/exporters/tests/test_slides.py b/IPython/nbconvert/exporters/tests/test_slides.py index 7c7d008..86197b6 100644 --- a/IPython/nbconvert/exporters/tests/test_slides.py +++ b/IPython/nbconvert/exporters/tests/test_slides.py @@ -47,5 +47,5 @@ class TestSlidesExporter(ExportersTestsBase): """ Can a SlidesExporter export using the 'reveal' template? """ - (output, resources) = SlidesExporter(template_file='reveal').from_filename(self._get_notebook()) + (output, resources) = SlidesExporter(template_file='slides_reveal').from_filename(self._get_notebook()) assert len(output) > 0 diff --git a/IPython/nbconvert/templates/html_basic.tpl b/IPython/nbconvert/templates/html/basic.tpl similarity index 100% rename from IPython/nbconvert/templates/html_basic.tpl rename to IPython/nbconvert/templates/html/basic.tpl diff --git a/IPython/nbconvert/templates/html_full.tpl b/IPython/nbconvert/templates/html/full.tpl similarity index 98% rename from IPython/nbconvert/templates/html_full.tpl rename to IPython/nbconvert/templates/html/full.tpl index cfc4516..21ab93c 100644 --- a/IPython/nbconvert/templates/html_full.tpl +++ b/IPython/nbconvert/templates/html/full.tpl @@ -1,4 +1,4 @@ -{%- extends 'html_basic.tpl' -%} +{%- extends 'basic.tpl' -%} {%- block header -%} @@ -67,4 +67,4 @@ init_mathjax(); {% block footer %} -{% endblock footer %} \ No newline at end of file +{% endblock footer %} diff --git a/IPython/nbconvert/templates/reveal_internals/reveal_cells.tpl b/IPython/nbconvert/templates/html/reveal_internals/reveal_cells.tpl similarity index 94% rename from IPython/nbconvert/templates/reveal_internals/reveal_cells.tpl rename to IPython/nbconvert/templates/html/reveal_internals/reveal_cells.tpl index c733f06..51fa997 100644 --- a/IPython/nbconvert/templates/reveal_internals/reveal_cells.tpl +++ b/IPython/nbconvert/templates/html/reveal_internals/reveal_cells.tpl @@ -1,4 +1,4 @@ -{%- extends 'html_basic.tpl' -%} +{%- extends 'basic.tpl' -%} {%- block any_cell scoped -%} diff --git a/IPython/nbconvert/templates/reveal_internals/slides.tpl b/IPython/nbconvert/templates/html/reveal_internals/slides.tpl similarity index 100% rename from IPython/nbconvert/templates/reveal_internals/slides.tpl rename to IPython/nbconvert/templates/html/reveal_internals/slides.tpl diff --git a/IPython/nbconvert/templates/reveal_internals/subslides.tpl b/IPython/nbconvert/templates/html/reveal_internals/subslides.tpl similarity index 100% rename from IPython/nbconvert/templates/reveal_internals/subslides.tpl rename to IPython/nbconvert/templates/html/reveal_internals/subslides.tpl diff --git a/IPython/nbconvert/templates/slides_reveal.tpl b/IPython/nbconvert/templates/html/slides_reveal.tpl similarity index 100% rename from IPython/nbconvert/templates/slides_reveal.tpl rename to IPython/nbconvert/templates/html/slides_reveal.tpl diff --git a/IPython/nbconvert/templates/latex/latex_article.tplx b/IPython/nbconvert/templates/latex/article.tplx similarity index 100% rename from IPython/nbconvert/templates/latex/latex_article.tplx rename to IPython/nbconvert/templates/latex/article.tplx diff --git a/IPython/nbconvert/templates/latex/latex_base.tplx b/IPython/nbconvert/templates/latex/base.tplx similarity index 100% rename from IPython/nbconvert/templates/latex/latex_base.tplx rename to IPython/nbconvert/templates/latex/base.tplx diff --git a/IPython/nbconvert/templates/latex/latex_report.tplx b/IPython/nbconvert/templates/latex/report.tplx similarity index 100% rename from IPython/nbconvert/templates/latex/latex_report.tplx rename to IPython/nbconvert/templates/latex/report.tplx diff --git a/IPython/nbconvert/templates/latex/style_bw_ipython.tplx b/IPython/nbconvert/templates/latex/style_bw_ipython.tplx index 3df4556..84859ab 100644 --- a/IPython/nbconvert/templates/latex/style_bw_ipython.tplx +++ b/IPython/nbconvert/templates/latex/style_bw_ipython.tplx @@ -1,6 +1,6 @@ ((= Black&white ipython input/output style =)) -((*- extends 'latex_base.tplx' -*)) +((*- extends 'base.tplx' -*)) %=============================================================================== % Input diff --git a/IPython/nbconvert/templates/latex/style_bw_python.tplx b/IPython/nbconvert/templates/latex/style_bw_python.tplx index 5371025..e10d4a2 100644 --- a/IPython/nbconvert/templates/latex/style_bw_python.tplx +++ b/IPython/nbconvert/templates/latex/style_bw_python.tplx @@ -1,6 +1,6 @@ ((= Black&white Python input/output style =)) -((*- extends 'latex_base.tplx' -*)) +((*- extends 'base.tplx' -*)) %=============================================================================== % Input diff --git a/IPython/nbconvert/templates/latex/style_ipython.tplx b/IPython/nbconvert/templates/latex/style_ipython.tplx index 77bc0b9..7799d18 100644 --- a/IPython/nbconvert/templates/latex/style_ipython.tplx +++ b/IPython/nbconvert/templates/latex/style_ipython.tplx @@ -1,6 +1,6 @@ ((= IPython input/output style =)) -((*- extends 'latex_base.tplx' -*)) +((*- extends 'base.tplx' -*)) % Custom definitions ((* block definitions *)) diff --git a/IPython/nbconvert/templates/latex/style_python.tplx b/IPython/nbconvert/templates/latex/style_python.tplx index f6f6e87..f950af9 100644 --- a/IPython/nbconvert/templates/latex/style_python.tplx +++ b/IPython/nbconvert/templates/latex/style_python.tplx @@ -1,6 +1,6 @@ ((= Python input/output style =)) -((*- extends 'latex_base.tplx' -*)) +((*- extends 'base.tplx' -*)) % Custom definitions ((* block definitions *)) diff --git a/IPython/nbconvert/tests/test_nbconvertapp.py b/IPython/nbconvert/tests/test_nbconvertapp.py index 4df503e..9a33a53 100644 --- a/IPython/nbconvert/tests/test_nbconvertapp.py +++ b/IPython/nbconvert/tests/test_nbconvertapp.py @@ -135,7 +135,7 @@ class TestNbConvertApp(TestsBase): """ with self.create_temp_cwd(['notebook2.ipynb']): self.call('nbconvert --log-level 0 --to slides ' - 'notebook2.ipynb --template reveal') + 'notebook2.ipynb') assert os.path.isfile('notebook2.slides.html') with open('notebook2.slides.html') as f: assert '/reveal.css' in f.read() diff --git a/setupbase.py b/setupbase.py index be98bf0..5afbdfe 100644 --- a/setupbase.py +++ b/setupbase.py @@ -146,6 +146,11 @@ def find_package_data(): os.chdir(os.path.join('tests',)) js_tests = glob('casperjs/*.*') + glob('casperjs/*/*') + + os.chdir(os.path.join(cwd, 'IPython', 'nbconvert')) + nbconvert_templates = [os.path.join(dirpath, '*.*') + for dirpath, _, _ in os.walk('templates')] + os.chdir(cwd) package_data = { @@ -157,10 +162,8 @@ def find_package_data(): 'IPython.html' : ['templates/*'] + static_data, 'IPython.html.tests' : js_tests, 'IPython.qt.console' : ['resources/icon/*.svg'], - 'IPython.nbconvert' : ['templates/*.tpl', 'templates/latex/*.tplx', - 'templates/latex/skeleton/*.tplx', 'templates/skeleton/*', - 'templates/reveal_internals/*.tpl', 'tests/files/*.*', - 'exporters/tests/files/*.*'], + 'IPython.nbconvert' : nbconvert_templates + + ['tests/files/*.*', 'exporters/tests/files/*.*'], 'IPython.nbformat' : ['tests/*.ipynb'] } return package_data