diff --git a/api/exporter.py b/api/exporter.py index 18ede5e..7bfa1bf 100755 --- a/api/exporter.py +++ b/api/exporter.py @@ -1,14 +1,12 @@ -"""Base classes for the notebook conversion pipeline. +"""Exporter for the notebook conversion pipeline. -This module defines ConverterTemplate, a highly configurable converter -that uses Jinja2 to convert notebook files into different format. +This module defines Exporter, a highly configurable converter +that uses Jinja2 to export notebook files into different format. You can register both pre-transformers that will act on the notebook format befor conversion and jinja filter that would then be availlable in the templates """ -from __future__ import absolute_import - #----------------------------------------------------------------------------- # Copyright (c) 2013, the IPython Development Team. # @@ -20,33 +18,29 @@ from __future__ import absolute_import #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- - -from __future__ import print_function -from __future__ import absolute_import +from __future__ import print_function, absolute_import # Stdlib imports import io import os # IPython imports -from IPython.utils.traitlets import MetaHasTraits -from IPython.utils.traitlets import (Unicode, List, Bool) from IPython.config.configurable import Configurable from IPython.nbformat import current as nbformat - +from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Bool # other libs/dependencies from jinja2 import Environment, FileSystemLoader - # local import (pre-transformers) -from . import transformers as trans +from . import transformers as trans #TODO + try: - from .sphinx_transformer import (SphinxTransformer) + from .sphinx_transformer import (SphinxTransformer) #TODO except ImportError: SphinxTransformer = None -from .latex_transformer import (LatexTransformer) +from .latex_transformer import (LatexTransformer) #TODO # some jinja filters from .jinja_filters import (python_comment, indent, @@ -55,9 +49,9 @@ from .jinja_filters import (python_comment, indent, rm_dollars, rm_math_space ) -from .utils import markdown2rst +from .utils import markdown2rst #TODO -import textwrap +import textwrap #TODO def wrap(text, width=100): """ try to detect and wrap paragraph""" @@ -106,7 +100,7 @@ texenv.filters['escape_tex'] = escape_tex class ConversionException(Exception): pass -class ConverterTemplate(Configurable): +class Exporter(Configurable): """ A Jinja2 base converter templates Preprocess the ipynb files, feed it throug jinja templates, diff --git a/nbconvert.py b/nbconvert.py index 10a1eb3..fc8959c 100755 --- a/nbconvert.py +++ b/nbconvert.py @@ -6,41 +6,54 @@ Commandline interface for the NBConvert conversion utility. Read the readme.rst for usage information """ #----------------------------------------------------------------------------- -# Copyright (c) 2013, the IPython Development Team. +#Copyright (c) 2013, the IPython Development Team. # -# Distributed under the terms of the Modified BSD License. +#Distributed under the terms of the Modified BSD License. # -# The full license is in the file COPYING.txt, distributed with this software. +#The full license is in the file COPYING.txt, distributed with this software. #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- -# Imports +#Imports #----------------------------------------------------------------------------- -# Stdlib imports +#Stdlib imports from __future__ import print_function import sys import io import os -# From IPython -# All the stuff needed for the configurable things +#From IPython +#All the stuff needed for the configurable things from IPython.config.application import Application from IPython.config.loader import ConfigFileNotFound from IPython.utils.traitlets import Unicode, Bool -# Local imports -from converters.template import ConverterTemplate #TODO +#Local imports +from api.exporter import Exporter from converters.config import GlobalConfigurable #TODO from converters.transformers import (ExtractFigureTransformer) #TODO #----------------------------------------------------------------------------- -# Globals and constants +#Globals and constants #----------------------------------------------------------------------------- NBCONVERT_DIR = os.path.abspath(os.path.realpath(os.path.dirname(__file__))) + +#'Keys in resources' user prompt. +KEYS_PROMPT_HEAD = "====================== Keys in Resources ==================================" +KEYS_PROMPT_BODY = """ +=========================================================================== +You are responsible for writting these files into the appropriate +directorie(s) if need be. If you do not want to see this message, enable +the 'write' (boolean) flag of the converter. +=========================================================================== +""" + +#Error Messages ERROR_CONFIG_NOT_FOUND = "Config file for profile '%s' not found, giving up." + #----------------------------------------------------------------------------- -# Classes and functions +#Classes and functions #----------------------------------------------------------------------------- class NbconvertApp(Application): """A basic application to convert ipynb files""" @@ -128,23 +141,31 @@ class NbconvertApp(Application): #The last arguments in chain of arguments will be used as conversion ipynb_file = (self.extra_args or [None])[2] - # If you are writting a custom transformer, append it to the dictionary - # below. + #If you are writting a custom transformer, append it to the dictionary + #below. userpreprocessors = {} - # Create the Jinja template converter TODO, refactor this - C = ConverterTemplate(config=self.config, preprocessors=userpreprocessors) + #Create the Jinja template exporter. TODO: Add ability to + #import in IPYNB aswell + exporter = Exporter(config=self.config, preprocessors=userpreprocessors) - output, resources = C.from_filename(ipynb_file) + #Export + output, resources = exporter.from_filename(ipynb_file) if self.stdout : print(output.encode('utf-8')) + #Get the file name without the '.ipynb' (6 chars) extension and then + #remove any addition periods and spaces. The resulting name will + #be used to create the directory that the files will be exported + #into. out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_') + #Write file output from conversion. if self.write : with io.open(os.path.join(out_root+'.'+self.fileext), 'w') as f: f.write(output) + #Output any associate figures into the same "root" directory. binkeys = resources.get('figures', {}).get('binary',{}).keys() textkeys = resources.get('figures', {}).get('text',{}).keys() if binkeys or textkeys : @@ -159,22 +180,19 @@ class NbconvertApp(Application): with io.open(os.path.join(files_dir, key), 'w') as f: f.write(resources['figures']['text'][key]) + #Figures that weren't exported which will need to be created by the + #user. Tell the user what figures these are. elif self.stdout: - print('''====================== Keys in Resources ==================================''') + print(KEYS_PROMPT_HEAD) print(resources['figures'].keys()) - print(""" -=========================================================================== -you are responsible from writing those data do a file in the right place if -they need to be. -=========================================================================== - """) + print(KEYS_PROMPT_BODY) #----------------------------------------------------------------------------- -# Script main +#Script main #----------------------------------------------------------------------------- def main(): """Convert a notebook in one step""" - + app = NbconvertApp.instance() app.description = __doc__ app.run(argv=sys.argv)