Skip to content

Commit

Permalink
Overridable default colors for ANSI "inverse"
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeier committed Nov 2, 2017
1 parent 1ce0701 commit 006153c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
42 changes: 26 additions & 16 deletions nbconvert/filters/ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,33 @@ def ansi2latex(text):
return _ansi2anything(text, _latexconverter)


def _htmlconverter(fg, bg, bold, underline):
def _htmlconverter(fg, bg, bold, underline, inverse):
"""
Return start and end tags for given foreground/background/bold/underline.
"""
if (fg, bg, bold, underline) == (None, None, False, False):
if (fg, bg, bold, underline, inverse) == (None, None, False, False, False):
return '', ''

classes = []
styles = []

if inverse:
fg, bg = bg, fg

if isinstance(fg, int):
classes.append(_ANSI_COLORS[fg] + '-fg')
elif fg:
styles.append('color: rgb({},{},{})'.format(*fg))
elif inverse:
classes.append('ansi-default-inverse-fg')

if isinstance(bg, int):
classes.append(_ANSI_COLORS[bg] + '-bg')
elif bg:
styles.append('background-color: rgb({},{},{})'.format(*bg))
elif inverse:
classes.append('ansi-default-inverse-bg')

if bold:
classes.append('ansi-bold')
Expand All @@ -110,16 +117,19 @@ def _htmlconverter(fg, bg, bold, underline):
return starttag, '</span>'


def _latexconverter(fg, bg, bold, underline):
def _latexconverter(fg, bg, bold, underline, inverse):
"""
Return start and end markup given foreground/background/bold/underline.
"""
if (fg, bg, bold, underline) == (None, None, False, False):
if (fg, bg, bold, underline, inverse) == (None, None, False, False, False):
return '', ''

starttag, endtag = '', ''

if inverse:
fg, bg = bg, fg

if isinstance(fg, int):
starttag += r'\textcolor{' + _ANSI_COLORS[fg] + '}{'
endtag = '}' + endtag
Expand All @@ -128,17 +138,24 @@ def _latexconverter(fg, bg, bold, underline):
starttag += r'\def\tcRGB{\textcolor[RGB]}\expandafter'
starttag += r'\tcRGB\expandafter{\detokenize{%s,%s,%s}}{' % fg
endtag = '}' + endtag
elif inverse:
starttag += r'\textcolor{ansi-default-inverse-fg}{'
endtag = '}' + endtag

if isinstance(bg, int):
starttag += r'\setlength{\fboxsep}{0pt}\colorbox{'
starttag += _ANSI_COLORS[bg] + '}{'
starttag += r'\setlength{\fboxsep}{0pt}'
starttag += r'\colorbox{' + _ANSI_COLORS[bg] + '}{'
endtag = r'\strut}' + endtag
elif bg:
starttag += r'\setlength{\fboxsep}{0pt}'
# See http://tex.stackexchange.com/a/291102/13684
starttag += r'\def\cbRGB{\colorbox[RGB]}\expandafter'
starttag += r'\cbRGB\expandafter{\detokenize{%s,%s,%s}}{' % bg
endtag = r'\strut}' + endtag
elif inverse:
starttag += r'\setlength{\fboxsep}{0pt}'
starttag += r'\colorbox{ansi-default-inverse-bg}{'
endtag = r'\strut}' + endtag

if bold:
starttag += r'\textbf{'
Expand Down Expand Up @@ -197,16 +214,9 @@ def _ansi2anything(text, converter):
chunk, text = text, ''

if chunk:
chunk_fg, chunk_bg = fg, bg
if bold and chunk_fg in range(8):
chunk_fg += 8
if inverse:
if chunk_fg is None:
chunk_fg = 0, 0, 0
if chunk_bg is None:
chunk_bg = 255, 255, 255
chunk_fg, chunk_bg = chunk_bg, chunk_fg
starttag, endtag = converter(chunk_fg, chunk_bg, bold, underline)
starttag, endtag = converter(
fg + 8 if bold and fg in range(8) else fg,
bg, bold, underline, inverse)
out.append(starttag)
out.append(chunk)
out.append(endtag)
Expand Down
2 changes: 2 additions & 0 deletions nbconvert/templates/latex/base.tplx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ This template does not define a docclass, the inheriting class must define this.
\definecolor{ansi-cyan-intense}{HTML}{258F8F}
\definecolor{ansi-white}{HTML}{C5C1B4}
\definecolor{ansi-white-intense}{HTML}{A1A6B2}
\definecolor{ansi-default-inverse-fg}{HTML}{FFFFFF}
\definecolor{ansi-default-inverse-bg}{HTML}{000000}

% commands and environments needed by pandoc snippets
% extracted from the output of `pandoc -s`
Expand Down

0 comments on commit 006153c

Please sign in to comment.