diff --git a/src/ansi2html/converter.py b/src/ansi2html/converter.py index 5e13814..7152750 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,6 +310,9 @@ def __init__( output_encoding: str = "utf-8", scheme: str = "ansi2html", title: str = "", + custom_bg: Optional[str] = None, + custom_fg: Optional[str] = None, + custom_content_css_dict: Optional[dict] = None, ) -> None: self.latex = latex self.inline = inline @@ -316,11 +327,22 @@ def __init__( self.title = title self._attrs: Attributes 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) + 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, + ) ] ) @@ -631,7 +653,14 @@ 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, + 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 @@ -649,7 +678,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)) + 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 719a95e..344dda3 100644 --- a/src/ansi2html/style.py +++ b/src/ansi2html/style.py @@ -243,6 +243,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, ) -> List[Rule]: css = [ Rule( @@ -250,9 +253,13 @@ 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], ), - Rule(".body_foreground", color=("#000000", "#AAAAAA")[dark_bg]), - Rule(".body_background", background_color=("#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"