Skip to content

Commit

Permalink
Implemented LaTeX support. Only colors are supported but it does alre…
Browse files Browse the repository at this point in the history
…ady what I need.

Tested with subnetcalc and ls.
  • Loading branch information
Robin Schneider committed May 20, 2014
1 parent d082217 commit caa8c6f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
62 changes: 53 additions & 9 deletions ansi2html/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,24 @@
ANSI_NEGATIVE_OFF = 27


_template = six.u("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
# http://stackoverflow.com/a/15190498
_latex_template = '''\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{fancyvrb}
\usepackage[usenames,dvipsnames]{xcolor}
\definecolor{red-sd}{HTML}{7ed2d2}
\\fvset{commandchars=\\\\\\{\}}
\\begin{document}
\\begin{Verbatim}[label={My orange command sample output}]
%(content)s
\end{Verbatim}
\end{document}
'''

_html_template = six.u("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=%(output_encoding)s">
Expand All @@ -78,7 +95,6 @@
</html>
""")


class _State(object):
def __init__(self):
self.reset()
Expand Down Expand Up @@ -182,6 +198,7 @@ class Ansi2HTMLConverter(object):
"""

def __init__(self,
latex=False,
inline=False,
dark_bg=True,
font_size='normal',
Expand All @@ -193,6 +210,7 @@ def __init__(self,
title=''
):

self.latex = latex
self.inline = inline
self.dark_bg = dark_bg
self.font_size = font_size
Expand Down Expand Up @@ -276,7 +294,10 @@ def _apply_regex(self, ansi):
params = params[last_null_index + 1:]
if inside_span:
inside_span = False
yield '</span>'
if self.latex:
yield '}'
else:
yield '</span>'
state.reset()

if not params:
Expand All @@ -299,24 +320,38 @@ def _apply_regex(self, ansi):
state.adjust(v, parameter=parameter)

if inside_span:
yield '</span>'
if self.latex:
yield '}'
else:
yield '</span>'
inside_span = False

css_classes = state.to_css_classes()
if not css_classes:
continue

if self.inline:
style = [self.styles[klass].kw for klass in css_classes if
klass in self.styles]
yield '<span style="%s">' % "; ".join(style)
if self.latex:
style = [self.styles[klass].kwl[0][1] for klass in css_classes if
self.styles[klass].kwl[0][0] == 'color']
yield '\\textcolor[HTML]{%s}{' % style[0]
else:
style = [self.styles[klass].kw for klass in css_classes if
klass in self.styles]
yield '<span style="%s">' % "; ".join(style)
else:
yield '<span class="%s">' % " ".join(css_classes)
if self.latex:
yield '\\textcolor{%s}{' % " ".join(css_classes)
else:
yield '<span class="%s">' % " ".join(css_classes)
inside_span = True

yield ansi[last_end:]
if inside_span:
yield '</span>'
if self.latex:
yield '}'
else:
yield '</span>'
inside_span = False

def _collapse_cursor(self, parts):
Expand Down Expand Up @@ -369,6 +404,10 @@ def convert(self, ansi, full=True, ensure_trailing_newline=False):
if not full:
return attrs["body"]
else:
if self.latex:
_template = _latex_template
else:
_template = _html_template
return _template % {
'style' : "\n".join(map(str, get_styles(self.dark_bg, self.scheme))),
'title' : self.title,
Expand Down Expand Up @@ -399,6 +438,10 @@ def main():
"-p", "--partial", dest="partial",
default=False, action="store_true",
help="Process lines as them come in. No headers are produced.")
parser.add_option(
"-L", "--latex", dest="latex",
default=False, action="store_true",
help="Export as LaTeX instead of HTML.")
parser.add_option(
"-i", "--inline", dest="inline",
default=False, action="store_true",
Expand Down Expand Up @@ -448,6 +491,7 @@ def main():
opts, args = parser.parse_args()

conv = Ansi2HTMLConverter(
latex=opts.latex,
inline=opts.inline,
dark_bg=not opts.light_background,
font_size=opts.font_size,
Expand Down
2 changes: 2 additions & 0 deletions ansi2html/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def __init__(self, klass, **kw):
self.klass = klass
self.kw = '; '.join([(k.replace('_', '-')+': '+kw[k])
for k in sorted(kw.keys())]).strip()
self.kwl = [(k.replace('_', '-'), kw[k][1:])
for k in sorted(kw.keys())]

def __str__(self):
return '%s { %s; }' % (self.klass, self.kw)
Expand Down

0 comments on commit caa8c6f

Please sign in to comment.