diff --git a/fix_code_class_pygments_hex.patch b/fix_code_class_pygments_hex.patch new file mode 100644 index 0000000000..a56f8d781d --- /dev/null +++ b/fix_code_class_pygments_hex.patch @@ -0,0 +1,28 @@ +diff --git a/tests/test_code_class.py b/tests/test_code_class.py +new file mode 100644 +index 00000000..f310e667 +--- /dev/null ++++ b/tests/test_code_class.py +@@ -0,0 +1,22 @@ ++import re ++import pytest ++ ++ ++def expand_hex(markup): ++ return re.sub( ++ r'(?<=["\'])#([0-9a-fA-F]{3})(?=["\'])', ++ lambda m: '#' + ''.join(c * 2 for c in m.group(1)), ++ markup ++ ) ++ ++ ++def test_expand_3digit_hex(): ++ assert expand_hex('foreground="#abc"') == 'foreground="#aabbcc"' ++ assert expand_hex('foreground="#fff"') == 'foreground="#ffffff"' ++ assert expand_hex('foreground="#aabbcc"') == 'foreground="#aabbcc"' ++ ++ ++def test_expand_multiple_colors(): ++ markup = 'color="#abc" background="#def"' ++ result = expand_hex(markup) ++ assert result == 'color="#aabbcc" background="#ddeeff"' diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index 91bfada8fc..ed4c828317 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -454,6 +454,12 @@ def __init__( style=code_style ) markup = pygments.highlight(code, lexer, formatter) + # Pango does not support 3-digit hex colors generated by pygments >= 2.14 + markup = re.sub( + r'(?<=["\'])#([0-9a-fA-F]{3})(?=["\'])', + lambda m: '#' + ''.join(c * 2 for c in m.group(1)), + markup + ) markup = re.sub(r"", "", markup) super().__init__( markup, diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000000..8205b1755e --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "manim", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/test_code_fix.py b/test_code_fix.py new file mode 100644 index 0000000000..1d12be7ce7 --- /dev/null +++ b/test_code_fix.py @@ -0,0 +1,8 @@ +from manimlib import * + + +class TestCodeFix(Scene): + def construct(self): + c = Code('sorted(iterable, key=None, reverse=False)') + self.play(FadeIn(c)) + self.wait() diff --git a/tests/test_code_class.py b/tests/test_code_class.py new file mode 100644 index 0000000000..f310e667ca --- /dev/null +++ b/tests/test_code_class.py @@ -0,0 +1,22 @@ +import re +import pytest + + +def expand_hex(markup): + return re.sub( + r'(?<=["\'])#([0-9a-fA-F]{3})(?=["\'])', + lambda m: '#' + ''.join(c * 2 for c in m.group(1)), + markup + ) + + +def test_expand_3digit_hex(): + assert expand_hex('foreground="#abc"') == 'foreground="#aabbcc"' + assert expand_hex('foreground="#fff"') == 'foreground="#ffffff"' + assert expand_hex('foreground="#aabbcc"') == 'foreground="#aabbcc"' + + +def test_expand_multiple_colors(): + markup = 'color="#abc" background="#def"' + result = expand_hex(markup) + assert result == 'color="#aabbcc" background="#ddeeff"'