##// END OF EJS Templates
Transformer refactor
Jonathan Frederic -
Show More
@@ -0,0 +1,3 b''
1 See COPYING.txt distributed with iPython.
2
3 #TODO, give NBCONVERT its own license No newline at end of file
@@ -0,0 +1,1 b''
1
@@ -0,0 +1,1 b''
1
@@ -0,0 +1,1 b''
1
@@ -0,0 +1,1 b''
1
@@ -0,0 +1,31 b''
1 """Filter used to select the first prefered output format available.
2
3 The filter contained in the file allows the converter templates to select
4 the output format that is most valuable to the active export format. The
5 value of the different formats is set via
6 GlobalConfigurable.display_data_priority
7 """
8 #-----------------------------------------------------------------------------
9 # Copyright (c) 2013, the IPython Development Team.
10 #
11 # Distributed under the terms of the Modified BSD License.
12 #
13 # The full license is in the file COPYING.txt, distributed with this software.
14 #-----------------------------------------------------------------------------
15
16 #-----------------------------------------------------------------------------
17 # Classes and functions
18 #-----------------------------------------------------------------------------
19 class DataTypeFilter(GlobalConfigurable):
20 """ Returns the prefered display format """
21
22 def __init__(self, config=None, **kw):
23 super(FilterDataType, self).__init__(config=config, **kw)
24
25 def __call__(self, output):
26 """ Return the first available format in the priority """
27
28 for fmt in self.display_data_priority:
29 if fmt in output:
30 return [fmt]
31 return [] No newline at end of file
@@ -0,0 +1,75 b''
1 """Markdown utilities
2
3 This file contains a collection of utility functions for dealing with
4 markdown.
5 """
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2013, the IPython Development Team.
8 #
9 # Distributed under the terms of the Modified BSD License.
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
13
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17 from __future__ import print_function
18
19 # Stdlib imports
20 import subprocess
21
22 #-----------------------------------------------------------------------------
23 # Functions
24 #-----------------------------------------------------------------------------
25 # Pandoc-dependent code
26 def markdown2latex(src):
27 """Convert a markdown string to LaTeX via pandoc.
28
29 This function will raise an error if pandoc is not installed.
30
31 Any error messages generated by pandoc are printed to stderr.
32
33 Parameters
34 ----------
35 src : string
36 Input string, assumed to be valid markdown.
37
38 Returns
39 -------
40 out : string
41 Output as returned by pandoc.
42 """
43 p = subprocess.Popen('pandoc -f markdown -t latex'.split(),
44 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
45 out, err = p.communicate(src.encode('utf-8'))
46 if err:
47 print(err, file=sys.stderr)
48 #print('*'*20+'\n', out, '\n'+'*'*20) # dbg
49 return unicode(out, 'utf-8')
50
51
52 def markdown2rst(src):
53 """Convert a markdown string to LaTeX via pandoc.
54
55 This function will raise an error if pandoc is not installed.
56
57 Any error messages generated by pandoc are printed to stderr.
58
59 Parameters
60 ----------
61 src : string
62 Input string, assumed to be valid markdown.
63
64 Returns
65 -------
66 out : string
67 Output as returned by pandoc.
68 """
69 p = subprocess.Popen('pandoc -f markdown -t rst'.split(),
70 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
71 out, err = p.communicate(src.encode('utf-8'))
72 if err:
73 print(err, file=sys.stderr)
74 #print('*'*20+'\n', out, '\n'+'*'*20) # dbg
75 return unicode(out, 'utf-8') No newline at end of file
@@ -38,22 +38,16 b' from markdown import markdown'
38 # local import (pre-transformers)
38 # local import (pre-transformers)
39 from exceptions import ConversionException
39 from exceptions import ConversionException
40 from . import transformers as trans #TODO
40 from . import transformers as trans #TODO
41 from .latex_transformer import (LatexTransformer) #TODO
42 from .utils import markdown2rst #TODO
43 from .utils import markdown2latex #TODO
44 from .utils import highlight2latex #TODO
41 from .utils import highlight2latex #TODO
45 from .utils import get_lines #TODO
42 from .utils import get_lines #TODO
46 from .utils import remove_ansi #TODO
43 from .utils import remove_ansi #TODO
47 from .utils import highlight, ansi2html #TODO
44 from .utils import highlight, ansi2html #TODO
48 from .latex_transformer import rm_math_space #TODO
49 import .utils.strings as strings
50
45
51 #Jinja2 filters
46 from .transformers.latex import LatexTransformer, rm_math_space
52 from .jinja_filters import (python_comment,
47
53 rm_fake,
48 import .utils.strings as strings
54 escape_tex, FilterDataType,
49 import .utils.markdown as markdown_utils
55 rm_dollars
50 import .utils.datatypefilter.DataTypeFilter as DataTypeFilter
56 )
57
51
58 #Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled
52 #Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled
59 #on his/her machine, fail silently.
53 #on his/her machine, fail silently.
@@ -223,15 +217,15 b' class Exporter(Configurable):'
223 self.preprocessors.append(SphinxTransformer(config=config))
217 self.preprocessors.append(SphinxTransformer(config=config))
224
218
225 #Add filters to the Jinja2 environment
219 #Add filters to the Jinja2 environment
226 self.env.filters['filter_data_type'] = FilterDataType(config=config)
220 self.env.filters['filter_data_type'] = DataTypeFilter(config=config)
227 self.env.filters['pycomment'] = _python_comment
221 self.env.filters['pycomment'] = _python_comment
228 self.env.filters['indent'] = indent
222 self.env.filters['indent'] = indent
229 self.env.filters['rm_fake'] = _rm_fake
223 self.env.filters['rm_fake'] = _rm_fake
230 self.env.filters['rm_ansi'] = remove_ansi
224 self.env.filters['rm_ansi'] = remove_ansi
231 self.env.filters['markdown'] = markdown
225 self.env.filters['markdown'] = markdown
232 self.env.filters['ansi2html'] = ansi2html
226 self.env.filters['ansi2html'] = ansi2html
233 self.env.filters['markdown2latex'] = markdown2latex
227 self.env.filters['markdown2latex'] = markdown_utils.markdown2latex
234 self.env.filters['markdown2rst'] = markdown2rst
228 self.env.filters['markdown2rst'] = markdown_utils.markdown2rst
235 self.env.filters['get_lines'] = get_lines
229 self.env.filters['get_lines'] = get_lines
236 self.env.filters['wrap'] = strings.wrap
230 self.env.filters['wrap'] = strings.wrap
237 self.env.filters['rm_dollars'] = strings.strip_dollars
231 self.env.filters['rm_dollars'] = strings.strip_dollars
1 NO CONTENT: file renamed from nbconvert1/converters/transformers.py to transformers/base.py
NO CONTENT: file renamed from nbconvert1/converters/transformers.py to transformers/base.py
@@ -1,14 +1,28 b''
1 """
1 """Latex transformer.
2
2 Module that allows latex output notebooks to be conditioned before
3 Module that allows latex output notebooks to be conditioned before
3 they are converted.
4 they are converted.
4 """
5 """
5 from __future__ import absolute_import
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2013, the IPython Development Team.
8 #
9 # Distributed under the terms of the Modified BSD License.
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
6
13
7 # Configurable traitlets
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17 from __future__ import print_function, absolute_import
8
18
19 # Our own imports
9 # Needed to override transformer
20 # Needed to override transformer
10 from .transformers import (ActivatableTransformer)
21 from .transformers import (ActivatableTransformer) #TODO
11
22
23 #-----------------------------------------------------------------------------
24 # Classes
25 #-----------------------------------------------------------------------------
12 class LatexTransformer(ActivatableTransformer):
26 class LatexTransformer(ActivatableTransformer):
13 """
27 """
14 Converter for latex destined documents.
28 Converter for latex destined documents.
@@ -26,6 +40,9 b' class LatexTransformer(ActivatableTransformer):'
26 cell.source = rm_math_space(cell.source)
40 cell.source = rm_math_space(cell.source)
27 return cell, other
41 return cell, other
28
42
43 #-----------------------------------------------------------------------------
44 # Functions
45 #-----------------------------------------------------------------------------
29 def rm_math_space(text):
46 def rm_math_space(text):
30 """
47 """
31 Remove the space between latex math commands and enclosing $ symbols.
48 Remove the space between latex math commands and enclosing $ symbols.
@@ -1,9 +1,20 b''
1 """
1 """Module that allows custom Sphinx parameters to be set on the notebook and
2 Module that allows custom Sphinx parameters to be set on the notebook and
3 on the 'other' object passed into Jinja.
2 on the 'other' object passed into Jinja.
4 """
3 """
5 from __future__ import absolute_import
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2013, the IPython Development Team.
6 #
7 # Distributed under the terms of the Modified BSD License.
8 #
9 # The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
11
12 #-----------------------------------------------------------------------------
13 # Imports
14 #-----------------------------------------------------------------------------
15 from __future__ import print_function, absolute_import
6
16
17 # Stdlib imports
7 # Used to find Sphinx package location
18 # Used to find Sphinx package location
8 import sphinx
19 import sphinx
9 import os.path
20 import os.path
@@ -14,15 +25,20 b' import sys'
14 # Used to set the default date to today's date
25 # Used to set the default date to today's date
15 from datetime import date
26 from datetime import date
16
27
17 # Configurable traitlets
28 # Third-party imports
18 from IPython.utils.traitlets import Unicode, Bool
19
20 # Needed for Pygments latex definitions.
29 # Needed for Pygments latex definitions.
21 from pygments.formatters import LatexFormatter
30 from pygments.formatters import LatexFormatter
22
31
32 # Our own imports
33 # Configurable traitlets
34 from IPython.utils.traitlets import Unicode, Bool
35
23 # Needed to override transformer
36 # Needed to override transformer
24 from .transformers import (ActivatableTransformer)
37 from .transformers import (ActivatableTransformer) #TODO
25
38
39 #-----------------------------------------------------------------------------
40 # Classes and functions
41 #-----------------------------------------------------------------------------
26 class SphinxTransformer(ActivatableTransformer):
42 class SphinxTransformer(ActivatableTransformer):
27 """
43 """
28 Sphinx utility transformer.
44 Sphinx utility transformer.
General Comments 0
You need to be logged in to leave comments. Login now