From 3012eaf9eb9fb35ccf5c19c2fbf428a4376bcbb8 Mon Sep 17 00:00:00 2001 From: Pho Hale Date: Sat, 14 Jan 2023 17:33:48 -0500 Subject: [PATCH 1/3] added ability to provided custom_bg and custom_fg colors --- src/ansi2html/converter.py | 11 ++++++++--- src/ansi2html/style.py | 6 ++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/ansi2html/converter.py b/src/ansi2html/converter.py index 70e2925..56177d3 100644 --- a/src/ansi2html/converter.py +++ b/src/ansi2html/converter.py @@ -302,6 +302,8 @@ def __init__( output_encoding: str = "utf-8", scheme: str = "ansi2html", title: str = "", + custom_bg = None, + custom_fg = None, ) -> None: self.latex = latex @@ -317,11 +319,14 @@ def __init__( self.title = title self._attrs: Attributes self.hyperref = False + self.custom_bg = custom_bg + self.custom_fg = custom_fg + if inline: self.styles = dict( [ (item.klass.strip("."), item) - for item in get_styles(self.dark_bg, self.line_wrap, self.scheme) + for item in get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg) ] ) @@ -633,7 +638,7 @@ def convert( _template = _latex_template else: _template = _html_template - all_styles = get_styles(self.dark_bg, self.line_wrap, self.scheme) + all_styles = get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg) backgrounds = all_styles[:5] used_styles = filter( lambda e: e.klass.lstrip(".") in attrs["styles"], all_styles @@ -651,7 +656,7 @@ def convert( def produce_headers(self) -> str: return '\n' % { "style": "\n".join( - map(str, get_styles(self.dark_bg, self.line_wrap, self.scheme)) + map(str, get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg)) ) } diff --git a/src/ansi2html/style.py b/src/ansi2html/style.py index 347504d..7d52901 100644 --- a/src/ansi2html/style.py +++ b/src/ansi2html/style.py @@ -244,6 +244,8 @@ def get_styles( dark_bg: bool = True, line_wrap: bool = True, scheme: str = "ansi2html", + custom_bg = None, + custom_fg = None ) -> List[Rule]: css = [ Rule( @@ -252,8 +254,8 @@ def get_styles( word_wrap="break-word", display="inline", ), - Rule(".body_foreground", color=("#000000", "#AAAAAA")[dark_bg]), - Rule(".body_background", background_color=("#AAAAAA", "#000000")[dark_bg]), + Rule(".body_foreground", color=custom_fg or ("#000000", "#AAAAAA")[dark_bg]), + Rule(".body_background", background_color=custom_bg or ("#AAAAAA", "#000000")[dark_bg]), Rule(".inv_foreground", color=("#000000", "#AAAAAA")[not dark_bg]), Rule(".inv_background", background_color=("#AAAAAA", "#000000")[not dark_bg]), # These effects are "SGR (Select Graphic Rendition) parameters" From 9a8e27419460a2f6d65372c22a89429a2039b49d Mon Sep 17 00:00:00 2001 From: Pho Hale Date: Sat, 14 Jan 2023 17:46:45 -0500 Subject: [PATCH 2/3] added `custom_bg`, `custom_fg`, `custom_content_css_dict` parameters to Ansi2HTMLConverter to enable specifying custom background/foreground colors and css properties --- src/ansi2html/converter.py | 20 +++++++++++++++----- src/ansi2html/style.py | 4 +++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/ansi2html/converter.py b/src/ansi2html/converter.py index 56177d3..d3de3d7 100644 --- a/src/ansi2html/converter.py +++ b/src/ansi2html/converter.py @@ -287,6 +287,14 @@ class Ansi2HTMLConverter: >>> conv = Ansi2HTMLConverter() >>> ansi = " ".join(sys.stdin.readlines()) >>> html = conv.convert(ansi) + + Example with Custom CSS Overrides: + + >>> custom_css_content_dict = {'font-family':'"Lucida Console", "Courier New", monospace', 'font-size':'8px'} # produces css like: 'font-family: "Lucida Console", "Courier New", monospace; font-size: 12px;' + >>> conv = Ansi2HTMLConverter(custom_bg='#FFFFFF', custom_fg='#FF0000', custom_content_css_dict=custom_css_content_dict) + >>> ansi = " ".join(sys.stdin.readlines()) + >>> html = conv.convert(ansi) + """ def __init__( @@ -302,8 +310,9 @@ def __init__( output_encoding: str = "utf-8", scheme: str = "ansi2html", title: str = "", - custom_bg = None, - custom_fg = None, + custom_bg: Optional[str] = None, + custom_fg: Optional[str] = None, + custom_content_css_dict: Optional[dict] = None ) -> None: self.latex = latex @@ -321,12 +330,13 @@ def __init__( self.hyperref = False self.custom_bg = custom_bg self.custom_fg = custom_fg + self.custom_content_css_dict = (custom_content_css_dict or {}) if inline: self.styles = dict( [ (item.klass.strip("."), item) - for item in get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg) + for item in get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg, custom_content_css_dict=self.custom_content_css_dict) ] ) @@ -638,7 +648,7 @@ def convert( _template = _latex_template else: _template = _html_template - all_styles = get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg) + all_styles = get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg, custom_content_css_dict=self.custom_content_css_dict) backgrounds = all_styles[:5] used_styles = filter( lambda e: e.klass.lstrip(".") in attrs["styles"], all_styles @@ -656,7 +666,7 @@ def convert( def produce_headers(self) -> str: return '\n' % { "style": "\n".join( - map(str, get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg)) + map(str, get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg, custom_content_css_dict=self.custom_content_css_dict)) ) } diff --git a/src/ansi2html/style.py b/src/ansi2html/style.py index 7d52901..b3c6d04 100644 --- a/src/ansi2html/style.py +++ b/src/ansi2html/style.py @@ -245,7 +245,8 @@ def get_styles( line_wrap: bool = True, scheme: str = "ansi2html", custom_bg = None, - custom_fg = None + custom_fg = None, + custom_content_css_dict = None ) -> List[Rule]: css = [ Rule( @@ -253,6 +254,7 @@ def get_styles( white_space=("pre", "pre-wrap")[line_wrap], word_wrap="break-word", display="inline", + **(custom_content_css_dict or {}) ), Rule(".body_foreground", color=custom_fg or ("#000000", "#AAAAAA")[dark_bg]), Rule(".body_background", background_color=custom_bg or ("#AAAAAA", "#000000")[dark_bg]), From 201e1d6818e9f3017c43a105d447e12593e131f6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Jan 2023 22:57:54 +0000 Subject: [PATCH 3/3] chore: auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/ansi2html/converter.py | 34 +++++++++++++++++++++++++++++----- src/ansi2html/style.py | 13 ++++++++----- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/ansi2html/converter.py b/src/ansi2html/converter.py index d3de3d7..c1cc31a 100644 --- a/src/ansi2html/converter.py +++ b/src/ansi2html/converter.py @@ -312,7 +312,7 @@ def __init__( title: str = "", custom_bg: Optional[str] = None, custom_fg: Optional[str] = None, - custom_content_css_dict: Optional[dict] = None + custom_content_css_dict: Optional[dict] = None, ) -> None: self.latex = latex @@ -330,13 +330,20 @@ def __init__( self.hyperref = False self.custom_bg = custom_bg self.custom_fg = custom_fg - self.custom_content_css_dict = (custom_content_css_dict or {}) + self.custom_content_css_dict = custom_content_css_dict or {} if inline: self.styles = dict( [ (item.klass.strip("."), item) - for item in get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg, custom_content_css_dict=self.custom_content_css_dict) + for item in get_styles( + self.dark_bg, + self.line_wrap, + self.scheme, + custom_bg=self.custom_bg, + custom_fg=self.custom_fg, + custom_content_css_dict=self.custom_content_css_dict, + ) ] ) @@ -648,7 +655,14 @@ def convert( _template = _latex_template else: _template = _html_template - all_styles = get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg, custom_content_css_dict=self.custom_content_css_dict) + all_styles = get_styles( + self.dark_bg, + self.line_wrap, + self.scheme, + custom_bg=self.custom_bg, + custom_fg=self.custom_fg, + custom_content_css_dict=self.custom_content_css_dict, + ) backgrounds = all_styles[:5] used_styles = filter( lambda e: e.klass.lstrip(".") in attrs["styles"], all_styles @@ -666,7 +680,17 @@ def convert( def produce_headers(self) -> str: return '\n' % { "style": "\n".join( - map(str, get_styles(self.dark_bg, self.line_wrap, self.scheme, custom_bg=self.custom_bg, custom_fg=self.custom_fg, custom_content_css_dict=self.custom_content_css_dict)) + map( + str, + get_styles( + self.dark_bg, + self.line_wrap, + self.scheme, + custom_bg=self.custom_bg, + custom_fg=self.custom_fg, + custom_content_css_dict=self.custom_content_css_dict, + ), + ) ) } diff --git a/src/ansi2html/style.py b/src/ansi2html/style.py index b3c6d04..9990e11 100644 --- a/src/ansi2html/style.py +++ b/src/ansi2html/style.py @@ -244,9 +244,9 @@ def get_styles( dark_bg: bool = True, line_wrap: bool = True, scheme: str = "ansi2html", - custom_bg = None, - custom_fg = None, - custom_content_css_dict = None + custom_bg=None, + custom_fg=None, + custom_content_css_dict=None, ) -> List[Rule]: css = [ Rule( @@ -254,10 +254,13 @@ def get_styles( white_space=("pre", "pre-wrap")[line_wrap], word_wrap="break-word", display="inline", - **(custom_content_css_dict or {}) + **(custom_content_css_dict or {}), ), Rule(".body_foreground", color=custom_fg or ("#000000", "#AAAAAA")[dark_bg]), - Rule(".body_background", background_color=custom_bg or ("#AAAAAA", "#000000")[dark_bg]), + Rule( + ".body_background", + background_color=custom_bg or ("#AAAAAA", "#000000")[dark_bg], + ), Rule(".inv_foreground", color=("#000000", "#AAAAAA")[not dark_bg]), Rule(".inv_background", background_color=("#AAAAAA", "#000000")[not dark_bg]), # These effects are "SGR (Select Graphic Rendition) parameters"