diff --git a/COPYING.txt b/COPYING.txt new file mode 100644 index 0000000..7274fbf --- /dev/null +++ b/COPYING.txt @@ -0,0 +1,3 @@ +See COPYING.txt distributed with iPython. + +#TODO, give NBCONVERT its own license \ No newline at end of file diff --git a/api/exporter.py b/api/exporter.py index bfdd495..17f1367 100755 --- a/api/exporter.py +++ b/api/exporter.py @@ -38,22 +38,16 @@ from markdown import markdown # local import (pre-transformers) from exceptions import ConversionException from . import transformers as trans #TODO -from .latex_transformer import (LatexTransformer) #TODO -from .utils import markdown2rst #TODO -from .utils import markdown2latex #TODO from .utils import highlight2latex #TODO from .utils import get_lines #TODO from .utils import remove_ansi #TODO from .utils import highlight, ansi2html #TODO -from .latex_transformer import rm_math_space #TODO -import .utils.strings as strings -#Jinja2 filters -from .jinja_filters import (python_comment, - rm_fake, - escape_tex, FilterDataType, - rm_dollars - ) +from .transformers.latex import LatexTransformer, rm_math_space + +import .utils.strings as strings +import .utils.markdown as markdown_utils +import .utils.datatypefilter.DataTypeFilter as DataTypeFilter #Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled #on his/her machine, fail silently. @@ -223,15 +217,15 @@ class Exporter(Configurable): self.preprocessors.append(SphinxTransformer(config=config)) #Add filters to the Jinja2 environment - self.env.filters['filter_data_type'] = FilterDataType(config=config) + self.env.filters['filter_data_type'] = DataTypeFilter(config=config) self.env.filters['pycomment'] = _python_comment self.env.filters['indent'] = indent self.env.filters['rm_fake'] = _rm_fake self.env.filters['rm_ansi'] = remove_ansi self.env.filters['markdown'] = markdown self.env.filters['ansi2html'] = ansi2html - self.env.filters['markdown2latex'] = markdown2latex - self.env.filters['markdown2rst'] = markdown2rst + self.env.filters['markdown2latex'] = markdown_utils.markdown2latex + self.env.filters['markdown2rst'] = markdown_utils.markdown2rst self.env.filters['get_lines'] = get_lines self.env.filters['wrap'] = strings.wrap self.env.filters['rm_dollars'] = strings.strip_dollars diff --git a/transformers/activatable.py b/transformers/activatable.py new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/transformers/activatable.py @@ -0,0 +1 @@ + diff --git a/nbconvert1/converters/transformers.py b/transformers/base.py similarity index 100% rename from nbconvert1/converters/transformers.py rename to transformers/base.py diff --git a/transformers/csshtmlheader.py b/transformers/csshtmlheader.py new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/transformers/csshtmlheader.py @@ -0,0 +1 @@ + diff --git a/transformers/extractfigure.py b/transformers/extractfigure.py new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/transformers/extractfigure.py @@ -0,0 +1 @@ + diff --git a/transformers/latex.py b/transformers/latex.py index 5412b2c..998621d 100644 --- a/transformers/latex.py +++ b/transformers/latex.py @@ -1,14 +1,28 @@ -""" +"""Latex transformer. + Module that allows latex output notebooks to be conditioned before they are converted. """ -from __future__ import absolute_import +#----------------------------------------------------------------------------- +# Copyright (c) 2013, the IPython Development Team. +# +# Distributed under the terms of the Modified BSD License. +# +# The full license is in the file COPYING.txt, distributed with this software. +#----------------------------------------------------------------------------- -# Configurable traitlets +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- +from __future__ import print_function, absolute_import +# Our own imports # Needed to override transformer -from .transformers import (ActivatableTransformer) +from .transformers import (ActivatableTransformer) #TODO +#----------------------------------------------------------------------------- +# Classes +#----------------------------------------------------------------------------- class LatexTransformer(ActivatableTransformer): """ Converter for latex destined documents. @@ -26,6 +40,9 @@ class LatexTransformer(ActivatableTransformer): cell.source = rm_math_space(cell.source) return cell, other +#----------------------------------------------------------------------------- +# Functions +#----------------------------------------------------------------------------- def rm_math_space(text): """ Remove the space between latex math commands and enclosing $ symbols. diff --git a/transformers/reavealhelp.py b/transformers/reavealhelp.py new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/transformers/reavealhelp.py @@ -0,0 +1 @@ + diff --git a/transformers/sphinx.py b/transformers/sphinx.py index fd4c99d..84f9bb4 100644 --- a/transformers/sphinx.py +++ b/transformers/sphinx.py @@ -1,9 +1,20 @@ -""" -Module that allows custom Sphinx parameters to be set on the notebook and +"""Module that allows custom Sphinx parameters to be set on the notebook and on the 'other' object passed into Jinja. """ -from __future__ import absolute_import +#----------------------------------------------------------------------------- +# Copyright (c) 2013, the IPython Development Team. +# +# Distributed under the terms of the Modified BSD License. +# +# The full license is in the file COPYING.txt, distributed with this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- +from __future__ import print_function, absolute_import +# Stdlib imports # Used to find Sphinx package location import sphinx import os.path @@ -14,15 +25,20 @@ import sys # Used to set the default date to today's date from datetime import date -# Configurable traitlets -from IPython.utils.traitlets import Unicode, Bool - +# Third-party imports # Needed for Pygments latex definitions. from pygments.formatters import LatexFormatter +# Our own imports +# Configurable traitlets +from IPython.utils.traitlets import Unicode, Bool + # Needed to override transformer -from .transformers import (ActivatableTransformer) +from .transformers import (ActivatableTransformer) #TODO +#----------------------------------------------------------------------------- +# Classes and functions +#----------------------------------------------------------------------------- class SphinxTransformer(ActivatableTransformer): """ Sphinx utility transformer. diff --git a/utils/datatypefilter.py b/utils/datatypefilter.py new file mode 100644 index 0000000..7ac1239 --- /dev/null +++ b/utils/datatypefilter.py @@ -0,0 +1,31 @@ +"""Filter used to select the first prefered output format available. + +The filter contained in the file allows the converter templates to select +the output format that is most valuable to the active export format. The +value of the different formats is set via +GlobalConfigurable.display_data_priority +""" +#----------------------------------------------------------------------------- +# Copyright (c) 2013, the IPython Development Team. +# +# Distributed under the terms of the Modified BSD License. +# +# The full license is in the file COPYING.txt, distributed with this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Classes and functions +#----------------------------------------------------------------------------- +class DataTypeFilter(GlobalConfigurable): + """ Returns the prefered display format """ + + def __init__(self, config=None, **kw): + super(FilterDataType, self).__init__(config=config, **kw) + + def __call__(self, output): + """ Return the first available format in the priority """ + + for fmt in self.display_data_priority: + if fmt in output: + return [fmt] + return [] \ No newline at end of file diff --git a/utils/markdown.py b/utils/markdown.py new file mode 100644 index 0000000..7f0e75f --- /dev/null +++ b/utils/markdown.py @@ -0,0 +1,75 @@ +"""Markdown utilities + +This file contains a collection of utility functions for dealing with +markdown. +""" +#----------------------------------------------------------------------------- +# Copyright (c) 2013, the IPython Development Team. +# +# Distributed under the terms of the Modified BSD License. +# +# The full license is in the file COPYING.txt, distributed with this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- +from __future__ import print_function + +# Stdlib imports +import subprocess + +#----------------------------------------------------------------------------- +# Functions +#----------------------------------------------------------------------------- +# Pandoc-dependent code +def markdown2latex(src): + """Convert a markdown string to LaTeX via pandoc. + + This function will raise an error if pandoc is not installed. + + Any error messages generated by pandoc are printed to stderr. + + Parameters + ---------- + src : string + Input string, assumed to be valid markdown. + + Returns + ------- + out : string + Output as returned by pandoc. + """ + p = subprocess.Popen('pandoc -f markdown -t latex'.split(), + stdin=subprocess.PIPE, stdout=subprocess.PIPE) + out, err = p.communicate(src.encode('utf-8')) + if err: + print(err, file=sys.stderr) + #print('*'*20+'\n', out, '\n'+'*'*20) # dbg + return unicode(out, 'utf-8') + + +def markdown2rst(src): + """Convert a markdown string to LaTeX via pandoc. + + This function will raise an error if pandoc is not installed. + + Any error messages generated by pandoc are printed to stderr. + + Parameters + ---------- + src : string + Input string, assumed to be valid markdown. + + Returns + ------- + out : string + Output as returned by pandoc. + """ + p = subprocess.Popen('pandoc -f markdown -t rst'.split(), + stdin=subprocess.PIPE, stdout=subprocess.PIPE) + out, err = p.communicate(src.encode('utf-8')) + if err: + print(err, file=sys.stderr) + #print('*'*20+'\n', out, '\n'+'*'*20) # dbg + return unicode(out, 'utf-8') \ No newline at end of file