##// END OF EJS Templates
move Ansi related filters to ansi.py
jakobgager -
Show More
@@ -95,3 +95,44 b' def ansi2html(text):'
95 if opened:
95 if opened:
96 text += '</span>'
96 text += '</span>'
97 return text
97 return text
98
99
100 def single_ansi2latex(code):
101 """Converts single ansi markup to latex format
102
103 Return latex code and number of open brackets.
104 """
105 for color in coloransi.color_templates:
106 colcode = getattr(coloransi.TermColors,color[0])
107 # regular fonts
108 if code == colcode:
109 return '\\'+color[0].lower()+'{', 1
110 # bold fonts
111 if code == colcode[:3]+str(1)+colcode[3:]:
112 return '\\textbf{\\textcolor{'+color[0].lower()+'}{', 2
113 return '', 0
114
115 def ansi2latex(text):
116 """Converts ansi formated text to latex version
117
118 based on https://bitbucket.org/birkenfeld/sphinx-contrib/ansi.py
119 """
120 color_pattern = re.compile('\x1b\\[([^m]+)m')
121 last_end = 0
122 openbrack = 0
123 outstring = ''
124 for match in color_pattern.finditer(text):
125 head = text[last_end:match.start()]
126 formater = match.group()
127 outstring += head
128 if openbrack:
129 outstring += '}'*openbrack
130 openbrack = 0
131 if match.group() <> coloransi.TermColors.Normal and not openbrack:
132 texform, openbrack = single_ansi2latex(match.group())
133 outstring += texform
134 last_end = match.end()
135 if openbrack:
136 outstring += '}'*openbrack
137 outstring += text[last_end:]
138 return outstring.strip()
@@ -54,52 +54,6 b' def strip_dollars(text):'
54
54
55 return text.strip('$')
55 return text.strip('$')
56
56
57 def add_ansi_attr(ansistr, attr):
58 """Adds the attribute key to the ansi colors defined
59 with IPython.utils.ansicolors. Allows to boldface
60 the dark characters.
61 """
62 return ansistr[:3]+str(attr)+ansistr[3:]
63
64 def single_ansi2latex(code):
65 """Converts single ansi markup to latex format
66
67 Return latex code and number of open brackets.
68 """
69 for color in coloransi.color_templates:
70 colcode = getattr(coloransi.TermColors,color[0])
71 # regular fonts
72 if code == colcode:
73 return '\\'+color[0].lower()+'{', 1
74 # bold fonts
75 if code == add_ansi_attr(colcode,1):
76 return '\\textbf{\\textcolor{'+color[0].lower()+'}{', 2
77 return '', 0
78
79 def ansi2latex(text):
80 """Converts ansi formated text to latex version
81
82 based on https://bitbucket.org/birkenfeld/sphinx-contrib/ansi.py
83 """
84 color_pattern = re.compile('\x1b\\[([^m]+)m')
85 last_end = 0
86 openbrack = 0
87 outstring = ''
88 for match in color_pattern.finditer(text):
89 head = text[last_end:match.start()]
90 formater = match.group()
91 outstring += head
92 if openbrack:
93 outstring += '}'*openbrack
94 openbrack = 0
95 if match.group() <> coloransi.TermColors.Normal and not openbrack:
96 texform, openbrack = single_ansi2latex(match.group())
97 outstring += texform
98 last_end = match.end()
99 if openbrack:
100 outstring += '}'*openbrack
101 outstring += text[last_end:]
102 return outstring.strip()
103
57
104 def rm_fake(text):
58 def rm_fake(text):
105 """
59 """
General Comments 0
You need to be logged in to leave comments. Login now