##// END OF EJS Templates
Added pygments syntax highlighting....
Jonathan Frederic -
Show More
@@ -16,6 +16,8 b' from markdown import markdown'
16 from .utils import remove_ansi
16 from .utils import remove_ansi
17 from .utils import highlight, ansi2html
17 from .utils import highlight, ansi2html
18 from .utils import markdown2latex
18 from .utils import markdown2latex
19 from .utils import highlight2latex
20 from .utils import get_lines
19
21
20 from converters.config import GlobalConfigurable
22 from converters.config import GlobalConfigurable
21
23
@@ -16,6 +16,9 b' from datetime import date'
16 # Configurable traitlets
16 # Configurable traitlets
17 from IPython.utils.traitlets import Unicode, Bool
17 from IPython.utils.traitlets import Unicode, Bool
18
18
19 # Needed for Pygments latex definitions.
20 from pygments.formatters import LatexFormatter
21
19 # Needed to override transformer
22 # Needed to override transformer
20 from converters.transformers import (ActivatableTransformer)
23 from converters.transformers import (ActivatableTransformer)
21
24
@@ -106,8 +109,14 b' class SphinxTransformer(ActivatableTransformer):'
106 # Find and pass in the path to the Sphinx dependencies.
109 # Find and pass in the path to the Sphinx dependencies.
107 other["sphinx_texinputs"] = os.path.abspath(sphinx.__file__ + "/../texinputs")
110 other["sphinx_texinputs"] = os.path.abspath(sphinx.__file__ + "/../texinputs")
108
111
112 # Generate Pygments definitions for Latex
113 other["pygment_definitions"] = self._generate_pygments_latex_def()
114
109 # End
115 # End
110 return nb, other
116 return nb, other
117
118 def _generate_pygments_latex_def(self):
119 return LatexFormatter().get_style_defs()
111
120
112 def _prompt_author(self):
121 def _prompt_author(self):
113 return self._input("Author name: ")
122 return self._input("Author name: ")
@@ -41,8 +41,8 b' from converters.latex_transformer import (LatexTransformer)'
41
41
42 # some jinja filters
42 # some jinja filters
43 from converters.jinja_filters import (python_comment, indent,
43 from converters.jinja_filters import (python_comment, indent,
44 rm_fake, remove_ansi, markdown, highlight,
44 rm_fake, remove_ansi, markdown, highlight, highlight2latex,
45 ansi2html, markdown2latex, escape_tex, FilterDataType)
45 ansi2html, markdown2latex, get_lines, escape_tex, FilterDataType)
46
46
47 from converters.utils import markdown2rst
47 from converters.utils import markdown2rst
48
48
@@ -176,9 +176,11 b' class ConverterTemplate(Configurable):'
176 self.env.filters['rm_ansi'] = remove_ansi
176 self.env.filters['rm_ansi'] = remove_ansi
177 self.env.filters['markdown'] = markdown
177 self.env.filters['markdown'] = markdown
178 self.env.filters['highlight'] = highlight
178 self.env.filters['highlight'] = highlight
179 self.env.filters['highlight2latex'] = highlight2latex
179 self.env.filters['ansi2html'] = ansi2html
180 self.env.filters['ansi2html'] = ansi2html
180 self.env.filters['markdown2latex'] = markdown2latex
181 self.env.filters['markdown2latex'] = markdown2latex
181 self.env.filters['markdown2rst'] = markdown2rst
182 self.env.filters['markdown2rst'] = markdown2rst
183 self.env.filters['get_lines'] = get_lines
182 self.env.filters['wrap'] = wrap
184 self.env.filters['wrap'] = wrap
183
185
184 ## user filter will overwrite
186 ## user filter will overwrite
@@ -45,19 +45,49 b" _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json']"
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46 def highlight(src, lang='ipython'):
46 def highlight(src, lang='ipython'):
47 """
47 """
48 Return a syntax-highlighted version of the input source.
48 Return a syntax-highlighted version of the input source as html output.
49 """
50 from pygments.formatters import HtmlFormatter
51 return pygment_highlight(src, HtmlFormatter(), lang)
52
53 def highlight2latex(src, lang='ipython'):
54 """
55 Return a syntax-highlighted version of the input source as latex output.
56 """
57 from pygments.formatters import LatexFormatter
58 return pygment_highlight(src, LatexFormatter(), lang)
59
60 def pygment_highlight(src, output_formatter, lang='ipython'):
61 """
62 Return a syntax-highlighted version of the input source
49 """
63 """
50 from pygments import highlight
64 from pygments import highlight
51 from pygments.lexers import get_lexer_by_name
65 from pygments.lexers import get_lexer_by_name
52 from pygments.formatters import HtmlFormatter
53
66
54 if lang == 'ipython':
67 if lang == 'ipython':
55 lexer = IPythonLexer()
68 lexer = IPythonLexer()
56 else:
69 else:
57 lexer = get_lexer_by_name(lang, stripall=True)
70 lexer = get_lexer_by_name(lang, stripall=True)
58
71
59 return highlight(src, lexer, HtmlFormatter())
72 return highlight(src, lexer, output_formatter)
60
73
74 def get_lines(src, start=-1,end=-1):
75 """
76 Split the input text into separate lines and then return the
77 lines that the caller is interested in.
78 """
79
80 # Split the input into lines.
81 lines = src.split("\n")
82
83 # Return the right lines.
84 if (start >= 0 and end >= 0):
85 lines = lines[start:end]
86 elif (start >= 0):
87 lines = lines[start:]
88 elif (end >= 0):
89 lines = lines[:end]
90 return "\n".join(lines) #re-join
61
91
62 def output_container(f):
92 def output_container(f):
63 """add a prompt-area next to an output"""
93 """add a prompt-area next to an output"""
General Comments 0
You need to be logged in to leave comments. Login now