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