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 22, 2023
1 parent 1bc8175 commit e1f3b1d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
64 changes: 63 additions & 1 deletion tests/config/test_themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import pytest
from pygments.styles.material import MaterialStyle
from pygments.styles.perldoc import PerldocStyle
from pygments.token import STANDARD_TYPES
from pygments.token import STANDARD_TYPES, _TokenType
from pytest import param as case
from pytest_mock import MockerFixture

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,
generate_urwid_compatible_pygments_styles,
valid_16_color_codes,
validate_colors,
)
Expand Down Expand Up @@ -402,3 +404,63 @@ class Color3(Enum):
+ "- GRAY_244 = dark_gra\n"
+ "- LIGHT2 = whit"
)


@pytest.mark.parametrize(
"pygments_styles, expected_styles, style_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",
"token2": "#abc",
},
{
"token1": "newstyle italic",
"token2": "#abc",
},
{"style": "newstyle"},
id="custom_translations",
),
],
)
def test_generate_urwid_compatible_pygments_styles(
pygments_styles: Dict[_TokenType, str],
expected_styles: Dict[_TokenType, str],
style_translations: Dict[str, str],
) -> None:
generated_styles = generate_urwid_compatible_pygments_styles(
pygments_styles, style_translations
)
assert sorted(expected_styles.items()) == sorted(generated_styles.items())
24 changes: 23 additions & 1 deletion zulipterminal/config/themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from typing import Any, Dict, List, Optional, Tuple, Union

from pygments.token import STANDARD_TYPES
from pygments.token import STANDARD_TYPES, _TokenType

from zulipterminal.config.color import term16
from zulipterminal.themes import gruvbox_dark, gruvbox_light, zt_blue, zt_dark, zt_light
Expand Down Expand Up @@ -128,6 +128,13 @@
"white",
]

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


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


def generate_urwid_compatible_pygments_styles(
pygments_styles: Dict[_TokenType, str],
style_translations: Dict[str, str] = STYLE_TRANSLATIONS,
) -> Dict[_TokenType, str]:
urwid_compatible_styles = {}
for token, style in pygments_styles.items():
updated_style = style
for old_value, new_value in style_translations.items():
updated_style = updated_style.replace(old_value, new_value)
urwid_compatible_styles[token] = updated_style
return urwid_compatible_styles


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

theme_styles_from_pygments: ThemeSpec = []
pygments_styles = generate_urwid_compatible_pygments_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 e1f3b1d

Please sign in to comment.