diff --git a/nbconvert/filters/ansi.py b/nbconvert/filters/ansi.py index 722f573..8461e09 100644 --- a/nbconvert/filters/ansi.py +++ b/nbconvert/filters/ansi.py @@ -95,3 +95,44 @@ def ansi2html(text): if opened: text += '' return text + + +def single_ansi2latex(code): + """Converts single ansi markup to latex format + + Return latex code and number of open brackets. + """ + for color in coloransi.color_templates: + colcode = getattr(coloransi.TermColors,color[0]) + # regular fonts + if code == colcode: + return '\\'+color[0].lower()+'{', 1 + # bold fonts + if code == colcode[:3]+str(1)+colcode[3:]: + return '\\textbf{\\textcolor{'+color[0].lower()+'}{', 2 + return '', 0 + +def ansi2latex(text): + """Converts ansi formated text to latex version + + based on https://bitbucket.org/birkenfeld/sphinx-contrib/ansi.py + """ + color_pattern = re.compile('\x1b\\[([^m]+)m') + last_end = 0 + openbrack = 0 + outstring = '' + for match in color_pattern.finditer(text): + head = text[last_end:match.start()] + formater = match.group() + outstring += head + if openbrack: + outstring += '}'*openbrack + openbrack = 0 + if match.group() <> coloransi.TermColors.Normal and not openbrack: + texform, openbrack = single_ansi2latex(match.group()) + outstring += texform + last_end = match.end() + if openbrack: + outstring += '}'*openbrack + outstring += text[last_end:] + return outstring.strip() diff --git a/nbconvert/filters/strings.py b/nbconvert/filters/strings.py index b27b2ad..d42a7ad 100755 --- a/nbconvert/filters/strings.py +++ b/nbconvert/filters/strings.py @@ -54,52 +54,6 @@ def strip_dollars(text): return text.strip('$') -def add_ansi_attr(ansistr, attr): - """Adds the attribute key to the ansi colors defined - with IPython.utils.ansicolors. Allows to boldface - the dark characters. - """ - return ansistr[:3]+str(attr)+ansistr[3:] - -def single_ansi2latex(code): - """Converts single ansi markup to latex format - - Return latex code and number of open brackets. - """ - for color in coloransi.color_templates: - colcode = getattr(coloransi.TermColors,color[0]) - # regular fonts - if code == colcode: - return '\\'+color[0].lower()+'{', 1 - # bold fonts - if code == add_ansi_attr(colcode,1): - return '\\textbf{\\textcolor{'+color[0].lower()+'}{', 2 - return '', 0 - -def ansi2latex(text): - """Converts ansi formated text to latex version - - based on https://bitbucket.org/birkenfeld/sphinx-contrib/ansi.py - """ - color_pattern = re.compile('\x1b\\[([^m]+)m') - last_end = 0 - openbrack = 0 - outstring = '' - for match in color_pattern.finditer(text): - head = text[last_end:match.start()] - formater = match.group() - outstring += head - if openbrack: - outstring += '}'*openbrack - openbrack = 0 - if match.group() <> coloransi.TermColors.Normal and not openbrack: - texform, openbrack = single_ansi2latex(match.group()) - outstring += texform - last_end = match.end() - if openbrack: - outstring += '}'*openbrack - outstring += text[last_end:] - return outstring.strip() def rm_fake(text): """