From 72bb4fd0c7fc6b33f59069b268e141c045535dd7 Mon Sep 17 00:00:00 2001 From: Stephan Seitz <stephan.seitz@fau.de> Date: Fri, 27 Mar 2020 17:51:41 +0100 Subject: [PATCH] Make highlight_style configurable --- pystencils/cpu/cpujit.py | 12 +++++++++++- pystencils/display_utils.py | 14 +++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pystencils/cpu/cpujit.py b/pystencils/cpu/cpujit.py index 07a8a84d..b7bb061d 100644 --- a/pystencils/cpu/cpujit.py +++ b/pystencils/cpu/cpujit.py @@ -176,8 +176,14 @@ def read_config(): ('clear_cache_on_start', False), ]) + default_highlight_style = OrderedDict([ + ('light_theme', 'default'), + ('dark_theme', 'stata-dark'), + ]) + default_config = OrderedDict([('compiler', default_compiler_config), - ('cache', default_cache_config)]) + ('cache', default_cache_config), + ('highlight_style', default_highlight_style)]) config_path, config_exists = get_configuration_file_path() config = default_config.copy() @@ -219,6 +225,10 @@ def get_cache_config(): return _config['cache'] +def get_highlight_style_config(): + return _config['highlight_style'] + + def add_or_change_compiler_flags(flags): if not isinstance(flags, list) and not isinstance(flags, tuple): flags = [flags] diff --git a/pystencils/display_utils.py b/pystencils/display_utils.py index 77e63ba2..bb17c4fd 100644 --- a/pystencils/display_utils.py +++ b/pystencils/display_utils.py @@ -29,10 +29,18 @@ def highlight_cpp(code: str): # noinspection PyUnresolvedReferences from pygments.lexers import CppLexer - css = HtmlFormatter().get_style_defs('.highlight') + from pystencils.cpu.cpujit import get_highlight_style_config + config = get_highlight_style_config() + + try: + css = HtmlFormatter(style=config['light_theme']).get_style_defs('.highlight') + except Exception: + css = HtmlFormatter(style='default').get_style_defs('.highlight') + print(f"Could not find light theme: {config['light_theme']}") + try: - dark_css = HtmlFormatter(style="stata-dark").get_style_defs('.highlight') - except ModuleNotFoundError: + dark_css = HtmlFormatter(style=config['dark_theme']).get_style_defs('.highlight') + except Exception: dark_css = css css_tag = "<style>{css} @media (prefers-color-scheme: dark) {{ {dark_css} }}</style>".format(css=css, -- GitLab