Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom bg fg colors and css #203

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/ansi2html/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand All @@ -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
Expand All @@ -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,
)
]
)

Expand Down Expand Up @@ -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
Expand All @@ -649,7 +678,17 @@ def convert(
def produce_headers(self) -> str:
return '<style type="text/css">\n%(style)s\n</style>\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,
),
)
)
}

Expand Down
11 changes: 9 additions & 2 deletions src/ansi2html/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,23 @@ 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(
".ansi2html-content",
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"
Expand Down
Loading