##// END OF EJS Templates
Decode user input into unicode...
Decode user input into unicode Author name: GÄ…bal ... Traceback (most recent call last): ... File "./nbconvert/converters/../templates/skeleton/tex/null.tplx", line 25, in top-level template code ((*- block header -*)) File "./nbconvert/converters/../templates/tex/latex_sphinx_base.tplx", line 141, in block "header" \renewcommand{\releasename}{((( nb.metadata._draft.release | escape_tex )))} UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 1: ordinal not in range(128)

File last commit:

r10676:9d4965be
r10797:f43864cd
Show More
ansi.py
97 lines | 2.6 KiB | text/x-python | PythonLexer
"""Filters for processing ANSI colors within Jinja templates.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import re
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
def remove_ansi(source):
"""
Remove ansi from text
Parameters
----------
source : str
Source to remove the ansi from
"""
return re.sub(r'\033\[(0|\d;\d\d)m', '', source)
def ansi2html(text):
"""
Conver ansi colors to html colors.
Parameters
----------
text : str
Text containing ansi colors to convert to html
"""
ansi_colormap = {
'30': 'ansiblack',
'31': 'ansired',
'32': 'ansigreen',
'33': 'ansiyellow',
'34': 'ansiblue',
'35': 'ansipurple',
'36': 'ansicyan',
'37': 'ansigrey',
'01': 'ansibold',
}
# do ampersand first
text = text.replace('&', '&')
html_escapes = {
'<': '&lt;',
'>': '&gt;',
"'": '&apos;',
'"': '&quot;',
'`': '&#96;',
}
for c, escape in html_escapes.iteritems():
text = text.replace(c, escape)
ansi_re = re.compile('\x1b' + r'\[([\dA-Fa-f;]*?)m')
m = ansi_re.search(text)
opened = False
cmds = []
opener = ''
closer = ''
while m:
cmds = m.groups()[0].split(';')
closer = '</span>' if opened else ''
# True if there is there more than one element in cmds, *or*
# if there is only one but it is not equal to a string of zeroes.
opened = len(cmds) > 1 or cmds[0] != '0' * len(cmds[0])
classes = []
for cmd in cmds:
if cmd in ansi_colormap:
classes.append(ansi_colormap.get(cmd))
if classes:
opener = '<span class="%s">' % (' '.join(classes))
else:
opener = ''
text = re.sub(ansi_re, closer + opener, text, 1)
m = ansi_re.search(text)
if opened:
text += '</span>'
return text