Skip to content

Commit

Permalink
config/themes: Convert pygments styles to urwid compatible styles.
Browse files Browse the repository at this point in the history
Pygments 2.16.0 introduced a style to support a combination of bold and
italic styling in pygments/pygments#2444. Both of our gruvbox themes and
the light native theme gain a 'bold strong' style via pygments as a
result, which urwid fails to parse and blocks the application from
loading.

This work would represent a clean fix for zulip#1431, which was temporarily
fixed by pinning pygments at ~=2.15.1 in zulip#1433.

Add method `translate_styles` that manually converts the pygments
`bold italic` style into the urwid-compatible `bold, italics`.

Tests added.

Fixes part of zulip#1434.
  • Loading branch information
jrijul1201 committed Dec 19, 2023
1 parent 1bc8175 commit 44643f9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/config/test_themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from zulipterminal.config.regexes import REGEX_COLOR_VALID_FORMATS
from zulipterminal.config.themes import (
REQUIRED_STYLES,
STYLE_TRANSLATIONS,
THEMES,
InvalidThemeColorCode,
MissingThemeAttributeError,
Expand All @@ -21,6 +22,7 @@
generate_pygments_styles,
generate_theme,
parse_themefile,
translate_styles,
valid_16_color_codes,
validate_colors,
)
Expand Down Expand Up @@ -402,3 +404,64 @@ class Color3(Enum):
+ "- GRAY_244 = dark_gra\n"
+ "- LIGHT2 = whit"
)


@pytest.mark.parametrize(
"styles, expected_styles, translations",
[
case(
{},
{},
STYLE_TRANSLATIONS,
id="empty_input",
),
case(
{
"token1": "style1",
"token2": "style2",
},
{
"token1": "style1",
"token2": "style2",
},
{},
id="empty_translations",
),
case(
{
"token1": "bold italic",
"token2": "italic bold",
"token3": "italic #abc",
},
{
"token1": "bold, italics",
"token2": "italics, bold",
"token3": "italics, #abc",
},
STYLE_TRANSLATIONS,
id="default_translations",
),
case(
{
"token1": "style italic",
"token3": "#abc",
},
{
"token1": "newstyle italic",
"token3": "#abc",
},
{"style": "newstyle"},
id="custom_translations",
),
],
)
def test_translate_styles(
mocker: MockerFixture,
styles: Dict[Any, str],
expected_styles: Dict[Any, str],
translations: Dict[str, str],
) -> None:
pygments_styles = translate_styles(styles, translations)

for style in expected_styles:
assert style in pygments_styles
31 changes: 31 additions & 0 deletions zulipterminal/config/themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@
"white",
]

# These are translations for translating pygments styles into urwid-compatible styles
STYLE_TRANSLATIONS = {
" ": ",",
"italic": "italics",
}


class ThemeError(Exception):
pass
Expand Down Expand Up @@ -255,6 +261,28 @@ def parse_themefile(
return urwid_theme


def translate_styles(
styles: Dict[Any, str], translations: Dict[str, str] = STYLE_TRANSLATIONS
) -> Dict[Any, str]:
"""
This function translates pygments styles into urwid compatible styles.
Parameters:
- styles (Dict[Any, str]): pygments styles
- translations (Dict[str, str]): replacements to be applied
Returns:
- Dict[Any, str]: translated urwid-compatible styles
"""
translated_styles = {}
for token, style in styles.items():
updated_style = style
for old_value, new_value in translations.items():
updated_style = updated_style.replace(old_value, new_value)
translated_styles[token] = updated_style
return translated_styles


def generate_pygments_styles(pygments: Dict[str, Any]) -> ThemeSpec:
"""
This function adds pygments styles for use in syntax
Expand All @@ -278,6 +306,9 @@ def generate_pygments_styles(pygments: Dict[str, Any]) -> ThemeSpec:
term16_bg = term16.background_color

theme_styles_from_pygments: ThemeSpec = []

pygments_styles = translate_styles(pygments_styles)

for token, css_class in STANDARD_TYPES.items():
if css_class in pygments_overrides:
pygments_styles[token] = pygments_overrides[css_class]
Expand Down

0 comments on commit 44643f9

Please sign in to comment.