Show More
@@ -1,10 +1,46 b'' | |||||
|
1 | """Filters for processing ANSI colors within Jinja templates. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2013, the IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
1 | import re |
|
15 | import re | |
2 |
|
16 | |||
3 | def remove_ansi(src): |
|
17 | #----------------------------------------------------------------------------- | |
4 | return re.sub(r'\033\[(0|\d;\d\d)m', '', src) |
|
18 | # Classes and functions | |
|
19 | #----------------------------------------------------------------------------- | |||
5 |
|
20 | |||
|
21 | def remove_ansi(source): | |||
|
22 | """ | |||
|
23 | Remove ansi from text | |||
|
24 | ||||
|
25 | Parameters | |||
|
26 | ---------- | |||
|
27 | source : str | |||
|
28 | Source to remove the ansi from | |||
|
29 | """ | |||
|
30 | ||||
|
31 | return re.sub(r'\033\[(0|\d;\d\d)m', '', source) | |||
6 |
|
32 | |||
7 | def ansi2html(txt): |
|
33 | ||
|
34 | def ansi2html(text): | |||
|
35 | """ | |||
|
36 | Conver ansi colors to html colors. | |||
|
37 | ||||
|
38 | Parameters | |||
|
39 | ---------- | |||
|
40 | text : str | |||
|
41 | Text containing ansi colors to convert to html | |||
|
42 | """ | |||
|
43 | ||||
8 | ansi_colormap = { |
|
44 | ansi_colormap = { | |
9 | '30': 'ansiblack', |
|
45 | '30': 'ansiblack', | |
10 | '31': 'ansired', |
|
46 | '31': 'ansired', | |
@@ -18,7 +54,7 b' def ansi2html(txt):' | |||||
18 | } |
|
54 | } | |
19 |
|
55 | |||
20 | # do ampersand first |
|
56 | # do ampersand first | |
21 | txt = txt.replace('&', '&') |
|
57 | text = text.replace('&', '&') | |
22 | html_escapes = { |
|
58 | html_escapes = { | |
23 | '<': '<', |
|
59 | '<': '<', | |
24 | '>': '>', |
|
60 | '>': '>', | |
@@ -28,10 +64,10 b' def ansi2html(txt):' | |||||
28 | } |
|
64 | } | |
29 |
|
65 | |||
30 | for c, escape in html_escapes.iteritems(): |
|
66 | for c, escape in html_escapes.iteritems(): | |
31 | txt = txt.replace(c, escape) |
|
67 | text = text.replace(c, escape) | |
32 |
|
68 | |||
33 | ansi_re = re.compile('\x1b' + r'\[([\dA-Fa-f;]*?)m') |
|
69 | ansi_re = re.compile('\x1b' + r'\[([\dA-Fa-f;]*?)m') | |
34 | m = ansi_re.search(txt) |
|
70 | m = ansi_re.search(text) | |
35 | opened = False |
|
71 | opened = False | |
36 | cmds = [] |
|
72 | cmds = [] | |
37 | opener = '' |
|
73 | opener = '' | |
@@ -52,10 +88,10 b' def ansi2html(txt):' | |||||
52 | opener = '<span class="%s">' % (' '.join(classes)) |
|
88 | opener = '<span class="%s">' % (' '.join(classes)) | |
53 | else: |
|
89 | else: | |
54 | opener = '' |
|
90 | opener = '' | |
55 | txt = re.sub(ansi_re, closer + opener, txt, 1) |
|
91 | text = re.sub(ansi_re, closer + opener, text, 1) | |
56 |
|
92 | |||
57 | m = ansi_re.search(txt) |
|
93 | m = ansi_re.search(text) | |
58 |
|
94 | |||
59 | if opened: |
|
95 | if opened: | |
60 | txt += '</span>' |
|
96 | text += '</span>' | |
61 | return txt No newline at end of file |
|
97 | return text |
@@ -1,3 +1,18 b'' | |||||
|
1 | """ | |||
|
2 | Module containing filter functions that allow code to be highlighted | |||
|
3 | from within Jinja templates. | |||
|
4 | """ | |||
|
5 | #----------------------------------------------------------------------------- | |||
|
6 | # Copyright (c) 2013, the IPython Development Team. | |||
|
7 | # | |||
|
8 | # Distributed under the terms of the Modified BSD License. | |||
|
9 | # | |||
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | ||||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | # Imports | |||
|
15 | #----------------------------------------------------------------------------- | |||
1 |
|
16 | |||
2 | from pygments import highlight as pygements_highlight |
|
17 | from pygments import highlight as pygements_highlight | |
3 | from pygments.lexers import get_lexer_by_name |
|
18 | from pygments.lexers import get_lexer_by_name | |
@@ -10,32 +25,58 b' from nbconvert.utils.lexers import IPythonLexer' | |||||
10 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
11 | # Globals and constants |
|
26 | # Globals and constants | |
12 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
13 | _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] |
|
|||
14 |
|
28 | |||
|
29 | MULTILINE_OUTPUTS = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] | |||
15 |
|
30 | |||
16 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
17 | # Utility functions |
|
32 | # Utility functions | |
18 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
19 | def highlight(src, lang='ipython'): |
|
34 | ||
|
35 | def highlight(source, language='ipython'): | |||
20 | """ |
|
36 | """ | |
21 | Return a syntax-highlighted version of the input source as html output. |
|
37 | Return a syntax-highlighted version of the input source as html output. | |
|
38 | ||||
|
39 | Parameters | |||
|
40 | ---------- | |||
|
41 | source : str | |||
|
42 | Source code to highlight the syntax of. | |||
|
43 | language : str | |||
|
44 | Language to highlight the syntax of. | |||
22 | """ |
|
45 | """ | |
23 |
|
46 | |||
24 | return _pygment_highlight(src, HtmlFormatter(), lang) |
|
47 | return _pygment_highlight(source, HtmlFormatter(), language) | |
|
48 | ||||
25 |
|
49 | |||
26 | def highlight2latex(src, lang='ipython'): |
|
50 | def highlight2latex(source, language='ipython'): | |
27 | """ |
|
51 | """ | |
28 | Return a syntax-highlighted version of the input source as latex output. |
|
52 | Return a syntax-highlighted version of the input source as latex output. | |
|
53 | ||||
|
54 | Parameters | |||
|
55 | ---------- | |||
|
56 | source : str | |||
|
57 | Source code to highlight the syntax of. | |||
|
58 | language : str | |||
|
59 | Language to highlight the syntax of. | |||
29 | """ |
|
60 | """ | |
30 | return _pygment_highlight(src, LatexFormatter(), lang) |
|
61 | return _pygment_highlight(source, LatexFormatter(), language) | |
31 |
|
62 | |||
32 | def _pygment_highlight(src, output_formatter, lang='ipython'): |
|
63 | ||
|
64 | def _pygment_highlight(source, output_formatter, language='ipython'): | |||
33 | """ |
|
65 | """ | |
34 | Return a syntax-highlighted version of the input source |
|
66 | Return a syntax-highlighted version of the input source | |
|
67 | ||||
|
68 | Parameters | |||
|
69 | ---------- | |||
|
70 | source : str | |||
|
71 | Source code to highlight the syntax of. | |||
|
72 | output_formatter : Pygments formatter | |||
|
73 | language : str | |||
|
74 | Language to highlight the syntax of. | |||
35 | """ |
|
75 | """ | |
36 | if lang == 'ipython': |
|
76 | ||
|
77 | if language == 'ipython': | |||
37 | lexer = IPythonLexer() |
|
78 | lexer = IPythonLexer() | |
38 | else: |
|
79 | else: | |
39 | lexer = get_lexer_by_name(lang, stripall=True) |
|
80 | lexer = get_lexer_by_name(language, stripall=True) | |
40 |
|
81 | |||
41 | return pygements_highlight(src, lexer, output_formatter) |
|
82 | return pygements_highlight(source, lexer, output_formatter) |
@@ -1,7 +1,6 b'' | |||||
1 |
"""Latex |
|
1 | """Latex filters. | |
2 |
|
2 | |||
3 | Module that allows latex output notebooks to be conditioned before |
|
3 | Module of useful filters for processing Latex within Jinja latex templates. | |
4 | they are converted. |
|
|||
5 | """ |
|
4 | """ | |
6 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
7 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
@@ -33,16 +32,32 b' LATEX_SUBS = (' | |||||
33 | #----------------------------------------------------------------------------- |
|
32 | #----------------------------------------------------------------------------- | |
34 | # Functions |
|
33 | # Functions | |
35 | #----------------------------------------------------------------------------- |
|
34 | #----------------------------------------------------------------------------- | |
36 | def escape_tex(value): |
|
35 | ||
37 | newval = value |
|
36 | def escape_latex(text): | |
|
37 | """ | |||
|
38 | Escape characters that may conflict with latex. | |||
|
39 | ||||
|
40 | Parameters | |||
|
41 | ---------- | |||
|
42 | text : str | |||
|
43 | Text containing characters that may conflict with Latex | |||
|
44 | """ | |||
|
45 | return_text = text | |||
38 | for pattern, replacement in LATEX_SUBS: |
|
46 | for pattern, replacement in LATEX_SUBS: | |
39 |
|
|
47 | return_text = pattern.sub(replacement, return_text) | |
40 |
return |
|
48 | return return_text | |
41 |
|
49 | |||
42 |
|
50 | |||
43 | def rm_math_space(text): |
|
51 | def rm_math_space(text): | |
44 | """ |
|
52 | """ | |
45 | Remove the space between latex math commands and enclosing $ symbols. |
|
53 | Remove the space between latex math commands and enclosing $ symbols. | |
|
54 | This filter is important because latex isn't as flexible as the notebook | |||
|
55 | front end when it comes to flagging math using ampersand symbols. | |||
|
56 | ||||
|
57 | Parameters | |||
|
58 | ---------- | |||
|
59 | text : str | |||
|
60 | Text to filter. | |||
46 | """ |
|
61 | """ | |
47 |
|
62 | |||
48 | # First, scan through the markdown looking for $. If |
|
63 | # First, scan through the markdown looking for $. If |
@@ -1,7 +1,6 b'' | |||||
1 |
"""Markdown |
|
1 | """Markdown filters | |
2 |
|
2 | This file contains a collection of utility filters for dealing with | ||
3 | This file contains a collection of utility functions for dealing with |
|
3 | markdown within Jinja templates. | |
4 | markdown. |
|
|||
5 | """ |
|
4 | """ | |
6 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
7 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
@@ -24,17 +23,15 b' import subprocess' | |||||
24 | # Functions |
|
23 | # Functions | |
25 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
26 |
|
25 | |||
27 | # Pandoc-dependent code |
|
26 | def markdown2latex(source): | |
28 | def markdown2latex(src): |
|
|||
29 | """Convert a markdown string to LaTeX via pandoc. |
|
27 | """Convert a markdown string to LaTeX via pandoc. | |
30 |
|
28 | |||
31 | This function will raise an error if pandoc is not installed. |
|
29 | This function will raise an error if pandoc is not installed. | |
32 |
|
||||
33 | Any error messages generated by pandoc are printed to stderr. |
|
30 | Any error messages generated by pandoc are printed to stderr. | |
34 |
|
31 | |||
35 | Parameters |
|
32 | Parameters | |
36 | ---------- |
|
33 | ---------- | |
37 | src : string |
|
34 | source : string | |
38 | Input string, assumed to be valid markdown. |
|
35 | Input string, assumed to be valid markdown. | |
39 |
|
36 | |||
40 | Returns |
|
37 | Returns | |
@@ -44,23 +41,25 b' def markdown2latex(src):' | |||||
44 | """ |
|
41 | """ | |
45 | p = subprocess.Popen('pandoc -f markdown -t latex'.split(), |
|
42 | p = subprocess.Popen('pandoc -f markdown -t latex'.split(), | |
46 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
43 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
47 | out, err = p.communicate(src.encode('utf-8')) |
|
44 | ||
|
45 | out, err = p.communicate(source.encode('utf-8')) | |||
|
46 | ||||
48 | if err: |
|
47 | if err: | |
49 | print(err, file=sys.stderr) |
|
48 | print(err, file=sys.stderr) | |
50 | #print('*'*20+'\n', out, '\n'+'*'*20) # dbg |
|
49 | #print('*'*20+'\n', out, '\n'+'*'*20) # dbg | |
|
50 | ||||
51 | return unicode(out, 'utf-8') |
|
51 | return unicode(out, 'utf-8') | |
52 |
|
52 | |||
53 |
|
53 | |||
54 | def markdown2rst(src): |
|
54 | def markdown2rst(source): | |
55 | """Convert a markdown string to LaTeX via pandoc. |
|
55 | """Convert a markdown string to LaTeX via pandoc. | |
56 |
|
56 | |||
57 | This function will raise an error if pandoc is not installed. |
|
57 | This function will raise an error if pandoc is not installed. | |
58 |
|
||||
59 | Any error messages generated by pandoc are printed to stderr. |
|
58 | Any error messages generated by pandoc are printed to stderr. | |
60 |
|
59 | |||
61 | Parameters |
|
60 | Parameters | |
62 | ---------- |
|
61 | ---------- | |
63 | src : string |
|
62 | source : string | |
64 | Input string, assumed to be valid markdown. |
|
63 | Input string, assumed to be valid markdown. | |
65 |
|
64 | |||
66 | Returns |
|
65 | Returns | |
@@ -70,8 +69,11 b' def markdown2rst(src):' | |||||
70 | """ |
|
69 | """ | |
71 | p = subprocess.Popen('pandoc -f markdown -t rst'.split(), |
|
70 | p = subprocess.Popen('pandoc -f markdown -t rst'.split(), | |
72 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
71 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
73 | out, err = p.communicate(src.encode('utf-8')) |
|
72 | ||
|
73 | out, err = p.communicate(source.encode('utf-8')) | |||
|
74 | ||||
74 | if err: |
|
75 | if err: | |
75 | print(err, file=sys.stderr) |
|
76 | print(err, file=sys.stderr) | |
76 | #print('*'*20+'\n', out, '\n'+'*'*20) # dbg |
|
77 | #print('*'*20+'\n', out, '\n'+'*'*20) # dbg | |
77 | return unicode(out, 'utf-8') No newline at end of file |
|
78 | ||
|
79 | return unicode(out, 'utf-8') |
@@ -1,6 +1,7 b'' | |||||
1 |
"""String |
|
1 | """String filters. | |
2 |
|
2 | |||
3 |
Contains a collection of useful string manipulation |
|
3 | Contains a collection of useful string manipulation filters for use in Jinja | |
|
4 | templates. | |||
4 | """ |
|
5 | """ | |
5 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | # Copyright (c) 2013, the IPython Development Team. | |
@@ -15,43 +16,89 b' Contains a collection of useful string manipulations functions.' | |||||
15 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
16 |
|
17 | |||
17 | # Our own imports |
|
18 | # Our own imports | |
18 |
import textwrap |
|
19 | import textwrap | |
19 |
|
20 | |||
20 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
21 | # Functions |
|
22 | # Functions | |
22 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
|
24 | ||||
23 | def wrap(text, width=100): |
|
25 | def wrap(text, width=100): | |
24 | """ Intelligently wrap text""" |
|
26 | """ | |
|
27 | Intelligently wrap text. | |||
|
28 | Wrap text without breaking words if possible. | |||
|
29 | ||||
|
30 | Parameters | |||
|
31 | ---------- | |||
|
32 | text : str | |||
|
33 | Text to wrap. | |||
|
34 | width : int, optional | |||
|
35 | Number of characters to wrap to, default 100. | |||
|
36 | """ | |||
25 |
|
37 | |||
26 | splitt = text.split('\n') |
|
38 | split_text = text.split('\n') | |
27 | wrp = map(lambda x:textwrap.wrap(x,width),splitt) |
|
39 | wrp = map(lambda x:textwrap.wrap(x,width), split_text) | |
28 | wrpd = map('\n'.join, wrp) |
|
40 | wrpd = map('\n'.join, wrp) | |
29 | return '\n'.join(wrpd) |
|
41 | return '\n'.join(wrpd) | |
30 |
|
42 | |||
31 |
|
43 | |||
32 | def strip_dollars(text): |
|
44 | def strip_dollars(text): | |
33 | """Remove all dollar symbols from text""" |
|
45 | """ | |
|
46 | Remove all dollar symbols from text | |||
|
47 | ||||
|
48 | Parameters | |||
|
49 | ---------- | |||
|
50 | text : str | |||
|
51 | Text to remove dollars from | |||
|
52 | """ | |||
34 |
|
53 | |||
35 | return text.strip('$') |
|
54 | return text.strip('$') | |
36 |
|
55 | |||
37 |
|
56 | |||
38 | #TODO: Comment me. |
|
57 | def rm_fake(text): | |
39 | def rm_fake(strng): |
|
58 | """ | |
40 | return strng.replace('/files/', '') |
|
59 | Remove all occurrences of '/files/' from text | |
|
60 | ||||
|
61 | Parameters | |||
|
62 | ---------- | |||
|
63 | text : str | |||
|
64 | Text to remove '/files/' from | |||
|
65 | """ | |||
|
66 | return text.replace('/files/', '') | |||
41 |
|
67 | |||
42 |
|
68 | |||
43 | #TODO: Comment me. |
|
69 | def python_comment(text): | |
44 | def python_comment(string): |
|
70 | """ | |
45 | return '# '+'\n# '.join(string.split('\n')) |
|
71 | Build a Python comment line from input text. | |
|
72 | ||||
|
73 | Parameters | |||
|
74 | ---------- | |||
|
75 | text : str | |||
|
76 | Text to comment out. | |||
|
77 | """ | |||
|
78 | ||||
|
79 | #Replace line breaks with line breaks and comment symbols. | |||
|
80 | #Also add a comment symbol at the beginning to comment out | |||
|
81 | #the first line. | |||
|
82 | return '# '+'\n# '.join(text.split('\n')) | |||
|
83 | ||||
46 |
|
84 | |||
47 |
def get_lines( |
|
85 | def get_lines(text, start=None,end=None): | |
48 | """ |
|
86 | """ | |
49 | Split the input text into separate lines and then return the |
|
87 | Split the input text into separate lines and then return the | |
50 | lines that the caller is interested in. |
|
88 | lines that the caller is interested in. | |
|
89 | ||||
|
90 | Parameters | |||
|
91 | ---------- | |||
|
92 | text : str | |||
|
93 | Text to parse lines from. | |||
|
94 | start : int, optional | |||
|
95 | First line to grab from. | |||
|
96 | end : int, optional | |||
|
97 | Last line to grab from. | |||
51 | """ |
|
98 | """ | |
52 |
|
99 | |||
53 | # Split the input into lines. |
|
100 | # Split the input into lines. | |
54 |
lines = |
|
101 | lines = text.split("\n") | |
55 |
|
102 | |||
56 | # Return the right lines. |
|
103 | # Return the right lines. | |
57 | return "\n".join(lines[start:end]) #re-join |
|
104 | return "\n".join(lines[start:end]) #re-join |
General Comments 0
You need to be logged in to leave comments.
Login now