Show More
@@ -1,138 +1,139 b'' | |||||
1 | """Filters for processing ANSI colors within Jinja templates. |
|
1 | """Filters for processing ANSI colors within Jinja templates. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (c) 2013, the IPython Development Team. |
|
4 | # Copyright (c) 2013, the IPython Development Team. | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the Modified BSD License. |
|
6 | # Distributed under the terms of the Modified BSD License. | |
7 | # |
|
7 | # | |
8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Imports |
|
12 | # Imports | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | import re |
|
15 | import re | |
|
16 | from IPython.utils import coloransi | |||
16 |
|
17 | |||
17 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
18 | # Classes and functions |
|
19 | # Classes and functions | |
19 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
20 |
|
21 | |||
21 | def remove_ansi(source): |
|
22 | def remove_ansi(source): | |
22 | """ |
|
23 | """ | |
23 | Remove ansi from text |
|
24 | Remove ansi from text | |
24 |
|
25 | |||
25 | Parameters |
|
26 | Parameters | |
26 | ---------- |
|
27 | ---------- | |
27 | source : str |
|
28 | source : str | |
28 | Source to remove the ansi from |
|
29 | Source to remove the ansi from | |
29 | """ |
|
30 | """ | |
30 |
|
31 | |||
31 | return re.sub(r'\033\[(0|\d;\d\d)m', '', source) |
|
32 | return re.sub(r'\033\[(0|\d;\d\d)m', '', source) | |
32 |
|
33 | |||
33 |
|
34 | |||
34 | def ansi2html(text): |
|
35 | def ansi2html(text): | |
35 | """ |
|
36 | """ | |
36 | Conver ansi colors to html colors. |
|
37 | Conver ansi colors to html colors. | |
37 |
|
38 | |||
38 | Parameters |
|
39 | Parameters | |
39 | ---------- |
|
40 | ---------- | |
40 | text : str |
|
41 | text : str | |
41 | Text containing ansi colors to convert to html |
|
42 | Text containing ansi colors to convert to html | |
42 | """ |
|
43 | """ | |
43 |
|
44 | |||
44 | ansi_colormap = { |
|
45 | ansi_colormap = { | |
45 | '30': 'ansiblack', |
|
46 | '30': 'ansiblack', | |
46 | '31': 'ansired', |
|
47 | '31': 'ansired', | |
47 | '32': 'ansigreen', |
|
48 | '32': 'ansigreen', | |
48 | '33': 'ansiyellow', |
|
49 | '33': 'ansiyellow', | |
49 | '34': 'ansiblue', |
|
50 | '34': 'ansiblue', | |
50 | '35': 'ansipurple', |
|
51 | '35': 'ansipurple', | |
51 | '36': 'ansicyan', |
|
52 | '36': 'ansicyan', | |
52 | '37': 'ansigrey', |
|
53 | '37': 'ansigrey', | |
53 | '01': 'ansibold', |
|
54 | '01': 'ansibold', | |
54 | } |
|
55 | } | |
55 |
|
56 | |||
56 | # do ampersand first |
|
57 | # do ampersand first | |
57 | text = text.replace('&', '&') |
|
58 | text = text.replace('&', '&') | |
58 | html_escapes = { |
|
59 | html_escapes = { | |
59 | '<': '<', |
|
60 | '<': '<', | |
60 | '>': '>', |
|
61 | '>': '>', | |
61 | "'": ''', |
|
62 | "'": ''', | |
62 | '"': '"', |
|
63 | '"': '"', | |
63 | '`': '`', |
|
64 | '`': '`', | |
64 | } |
|
65 | } | |
65 |
|
66 | |||
66 | for c, escape in html_escapes.iteritems(): |
|
67 | for c, escape in html_escapes.iteritems(): | |
67 | text = text.replace(c, escape) |
|
68 | text = text.replace(c, escape) | |
68 |
|
69 | |||
69 | ansi_re = re.compile('\x1b' + r'\[([\dA-Fa-f;]*?)m') |
|
70 | ansi_re = re.compile('\x1b' + r'\[([\dA-Fa-f;]*?)m') | |
70 | m = ansi_re.search(text) |
|
71 | m = ansi_re.search(text) | |
71 | opened = False |
|
72 | opened = False | |
72 | cmds = [] |
|
73 | cmds = [] | |
73 | opener = '' |
|
74 | opener = '' | |
74 | closer = '' |
|
75 | closer = '' | |
75 | while m: |
|
76 | while m: | |
76 | cmds = m.groups()[0].split(';') |
|
77 | cmds = m.groups()[0].split(';') | |
77 | closer = '</span>' if opened else '' |
|
78 | closer = '</span>' if opened else '' | |
78 |
|
79 | |||
79 | # True if there is there more than one element in cmds, *or* |
|
80 | # True if there is there more than one element in cmds, *or* | |
80 | # if there is only one but it is not equal to a string of zeroes. |
|
81 | # if there is only one but it is not equal to a string of zeroes. | |
81 | opened = len(cmds) > 1 or cmds[0] != '0' * len(cmds[0]) |
|
82 | opened = len(cmds) > 1 or cmds[0] != '0' * len(cmds[0]) | |
82 | classes = [] |
|
83 | classes = [] | |
83 | for cmd in cmds: |
|
84 | for cmd in cmds: | |
84 | if cmd in ansi_colormap: |
|
85 | if cmd in ansi_colormap: | |
85 | classes.append(ansi_colormap.get(cmd)) |
|
86 | classes.append(ansi_colormap.get(cmd)) | |
86 |
|
87 | |||
87 | if classes: |
|
88 | if classes: | |
88 | opener = '<span class="%s">' % (' '.join(classes)) |
|
89 | opener = '<span class="%s">' % (' '.join(classes)) | |
89 | else: |
|
90 | else: | |
90 | opener = '' |
|
91 | opener = '' | |
91 | text = re.sub(ansi_re, closer + opener, text, 1) |
|
92 | text = re.sub(ansi_re, closer + opener, text, 1) | |
92 |
|
93 | |||
93 | m = ansi_re.search(text) |
|
94 | m = ansi_re.search(text) | |
94 |
|
95 | |||
95 | if opened: |
|
96 | if opened: | |
96 | text += '</span>' |
|
97 | text += '</span>' | |
97 | return text |
|
98 | return text | |
98 |
|
99 | |||
99 |
|
100 | |||
100 | def single_ansi2latex(code): |
|
101 | def single_ansi2latex(code): | |
101 | """Converts single ansi markup to latex format |
|
102 | """Converts single ansi markup to latex format | |
102 |
|
103 | |||
103 | Return latex code and number of open brackets. |
|
104 | Return latex code and number of open brackets. | |
104 | """ |
|
105 | """ | |
105 | for color in coloransi.color_templates: |
|
106 | for color in coloransi.color_templates: | |
106 | colcode = getattr(coloransi.TermColors,color[0]) |
|
107 | colcode = getattr(coloransi.TermColors,color[0]) | |
107 | # regular fonts |
|
108 | # regular fonts | |
108 | if code == colcode: |
|
109 | if code == colcode: | |
109 | return '\\'+color[0].lower()+'{', 1 |
|
110 | return '\\'+color[0].lower()+'{', 1 | |
110 | # bold fonts |
|
111 | # bold fonts | |
111 | if code == colcode[:3]+str(1)+colcode[3:]: |
|
112 | if code == colcode[:3]+str(1)+colcode[3:]: | |
112 | return '\\textbf{\\textcolor{'+color[0].lower()+'}{', 2 |
|
113 | return '\\textbf{\\textcolor{'+color[0].lower()+'}{', 2 | |
113 | return '', 0 |
|
114 | return '', 0 | |
114 |
|
115 | |||
115 | def ansi2latex(text): |
|
116 | def ansi2latex(text): | |
116 | """Converts ansi formated text to latex version |
|
117 | """Converts ansi formated text to latex version | |
117 |
|
118 | |||
118 | based on https://bitbucket.org/birkenfeld/sphinx-contrib/ansi.py |
|
119 | based on https://bitbucket.org/birkenfeld/sphinx-contrib/ansi.py | |
119 | """ |
|
120 | """ | |
120 | color_pattern = re.compile('\x1b\\[([^m]+)m') |
|
121 | color_pattern = re.compile('\x1b\\[([^m]+)m') | |
121 | last_end = 0 |
|
122 | last_end = 0 | |
122 | openbrack = 0 |
|
123 | openbrack = 0 | |
123 | outstring = '' |
|
124 | outstring = '' | |
124 | for match in color_pattern.finditer(text): |
|
125 | for match in color_pattern.finditer(text): | |
125 | head = text[last_end:match.start()] |
|
126 | head = text[last_end:match.start()] | |
126 | formater = match.group() |
|
127 | formater = match.group() | |
127 | outstring += head |
|
128 | outstring += head | |
128 | if openbrack: |
|
129 | if openbrack: | |
129 | outstring += '}'*openbrack |
|
130 | outstring += '}'*openbrack | |
130 | openbrack = 0 |
|
131 | openbrack = 0 | |
131 | if match.group() <> coloransi.TermColors.Normal and not openbrack: |
|
132 | if match.group() <> coloransi.TermColors.Normal and not openbrack: | |
132 | texform, openbrack = single_ansi2latex(match.group()) |
|
133 | texform, openbrack = single_ansi2latex(match.group()) | |
133 | outstring += texform |
|
134 | outstring += texform | |
134 | last_end = match.end() |
|
135 | last_end = match.end() | |
135 | if openbrack: |
|
136 | if openbrack: | |
136 | outstring += '}'*openbrack |
|
137 | outstring += '}'*openbrack | |
137 | outstring += text[last_end:] |
|
138 | outstring += text[last_end:] | |
138 | return outstring.strip() |
|
139 | return outstring.strip() |
@@ -1,105 +1,104 b'' | |||||
1 | """String filters. |
|
1 | """String filters. | |
2 |
|
2 | |||
3 | Contains a collection of useful string manipulation filters for use in Jinja |
|
3 | Contains a collection of useful string manipulation filters for use in Jinja | |
4 | templates. |
|
4 | templates. | |
5 | """ |
|
5 | """ | |
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | # Copyright (c) 2013, the IPython Development Team. | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the Modified BSD License. |
|
9 | # Distributed under the terms of the Modified BSD License. | |
10 | # |
|
10 | # | |
11 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | import re |
|
17 | import re | |
18 |
|
18 | |||
19 | # Our own imports |
|
19 | # Our own imports | |
20 | import textwrap |
|
20 | import textwrap | |
21 | from IPython.utils import coloransi |
|
|||
22 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
23 | # Functions |
|
22 | # Functions | |
24 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
25 |
|
24 | |||
26 | def wrap(text, width=100): |
|
25 | def wrap(text, width=100): | |
27 | """ |
|
26 | """ | |
28 | Intelligently wrap text. |
|
27 | Intelligently wrap text. | |
29 | Wrap text without breaking words if possible. |
|
28 | Wrap text without breaking words if possible. | |
30 |
|
29 | |||
31 | Parameters |
|
30 | Parameters | |
32 | ---------- |
|
31 | ---------- | |
33 | text : str |
|
32 | text : str | |
34 | Text to wrap. |
|
33 | Text to wrap. | |
35 | width : int, optional |
|
34 | width : int, optional | |
36 | Number of characters to wrap to, default 100. |
|
35 | Number of characters to wrap to, default 100. | |
37 | """ |
|
36 | """ | |
38 |
|
37 | |||
39 | split_text = text.split('\n') |
|
38 | split_text = text.split('\n') | |
40 | wrp = map(lambda x:textwrap.wrap(x,width), split_text) |
|
39 | wrp = map(lambda x:textwrap.wrap(x,width), split_text) | |
41 | wrpd = map('\n'.join, wrp) |
|
40 | wrpd = map('\n'.join, wrp) | |
42 | return '\n'.join(wrpd) |
|
41 | return '\n'.join(wrpd) | |
43 |
|
42 | |||
44 |
|
43 | |||
45 | def strip_dollars(text): |
|
44 | def strip_dollars(text): | |
46 | """ |
|
45 | """ | |
47 | Remove all dollar symbols from text |
|
46 | Remove all dollar symbols from text | |
48 |
|
47 | |||
49 | Parameters |
|
48 | Parameters | |
50 | ---------- |
|
49 | ---------- | |
51 | text : str |
|
50 | text : str | |
52 | Text to remove dollars from |
|
51 | Text to remove dollars from | |
53 | """ |
|
52 | """ | |
54 |
|
53 | |||
55 | return text.strip('$') |
|
54 | return text.strip('$') | |
56 |
|
55 | |||
57 |
|
56 | |||
58 | def rm_fake(text): |
|
57 | def rm_fake(text): | |
59 | """ |
|
58 | """ | |
60 | Remove all occurrences of '/files/' from text |
|
59 | Remove all occurrences of '/files/' from text | |
61 |
|
60 | |||
62 | Parameters |
|
61 | Parameters | |
63 | ---------- |
|
62 | ---------- | |
64 | text : str |
|
63 | text : str | |
65 | Text to remove '/files/' from |
|
64 | Text to remove '/files/' from | |
66 | """ |
|
65 | """ | |
67 | return text.replace('/files/', '') |
|
66 | return text.replace('/files/', '') | |
68 |
|
67 | |||
69 |
|
68 | |||
70 | def python_comment(text): |
|
69 | def python_comment(text): | |
71 | """ |
|
70 | """ | |
72 | Build a Python comment line from input text. |
|
71 | Build a Python comment line from input text. | |
73 |
|
72 | |||
74 | Parameters |
|
73 | Parameters | |
75 | ---------- |
|
74 | ---------- | |
76 | text : str |
|
75 | text : str | |
77 | Text to comment out. |
|
76 | Text to comment out. | |
78 | """ |
|
77 | """ | |
79 |
|
78 | |||
80 | #Replace line breaks with line breaks and comment symbols. |
|
79 | #Replace line breaks with line breaks and comment symbols. | |
81 | #Also add a comment symbol at the beginning to comment out |
|
80 | #Also add a comment symbol at the beginning to comment out | |
82 | #the first line. |
|
81 | #the first line. | |
83 | return '# '+'\n# '.join(text.split('\n')) |
|
82 | return '# '+'\n# '.join(text.split('\n')) | |
84 |
|
83 | |||
85 |
|
84 | |||
86 | def get_lines(text, start=None,end=None): |
|
85 | def get_lines(text, start=None,end=None): | |
87 | """ |
|
86 | """ | |
88 | Split the input text into separate lines and then return the |
|
87 | Split the input text into separate lines and then return the | |
89 | lines that the caller is interested in. |
|
88 | lines that the caller is interested in. | |
90 |
|
89 | |||
91 | Parameters |
|
90 | Parameters | |
92 | ---------- |
|
91 | ---------- | |
93 | text : str |
|
92 | text : str | |
94 | Text to parse lines from. |
|
93 | Text to parse lines from. | |
95 | start : int, optional |
|
94 | start : int, optional | |
96 | First line to grab from. |
|
95 | First line to grab from. | |
97 | end : int, optional |
|
96 | end : int, optional | |
98 | Last line to grab from. |
|
97 | Last line to grab from. | |
99 | """ |
|
98 | """ | |
100 |
|
99 | |||
101 | # Split the input into lines. |
|
100 | # Split the input into lines. | |
102 | lines = text.split("\n") |
|
101 | lines = text.split("\n") | |
103 |
|
102 | |||
104 | # Return the right lines. |
|
103 | # Return the right lines. | |
105 | 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