diff --git a/apps/codesamples/factories.py b/apps/codesamples/factories.py index 654364d7a..d770cccea 100644 --- a/apps/codesamples/factories.py +++ b/apps/codesamples/factories.py @@ -4,6 +4,9 @@ import factory from factory.django import DjangoModelFactory +from pygments import highlight +from pygments.formatters import HtmlFormatter +from pygments.lexers import PythonConsoleLexer from apps.codesamples.models import CodeSample from apps.users.factories import UserFactory @@ -26,22 +29,28 @@ class Meta: is_published = True +def _highlight_python_console(code): + """Highlight a Python console code snippet using Pygments.""" + code = textwrap.dedent(code).strip() + html = highlight(code, PythonConsoleLexer(), HtmlFormatter(nowrap=True)) + return f"
{html}
" + + def initial_data(): """Create the default set of homepage code samples.""" code_samples = [ ( - """\ -
# Simple output (with Unicode)
+            """
+            # Simple output (with Unicode)
             >>> print("Hello, I'm Python!")
-            Hello, I'm Python!
+            Hello, I'm Python!
 
-            # Input, assignment
-            >>> name = input('What is your name?\n')
-            What is your name?
-            Python
+            # Input, assignment
+            >>> name = input('What is your name?\\n')
             >>> print(f'Hi, {name}.')
-            Hi, Python.
-            
+ What is your name? + Python + Hi, Python. """, """\

Quick & Easy to Learn

@@ -53,16 +62,16 @@ def initial_data(): """, ), ( - """\ -
# Simple arithmetic
+            """
+            # Simple arithmetic
             >>> 1 / 2
-            0.5
+            0.5
             >>> 2 ** 3
-            8
-            >>> 17 / 3  # true division returns a float
-            5.666666666666667
-            >>> 17 // 3  # floor division
-            5
+ 8 + >>> 17 / 3 # true division returns a float + 5.666666666666667 + >>> 17 // 3 # floor division + 5 """, """\

Intuitive Interpretation

@@ -76,16 +85,16 @@ def initial_data(): """, ), ( - """\ -
# List comprehensions
+            """
+            # List comprehensions
             >>> fruits = ['Banana', 'Apple', 'Lime']
             >>> loud_fruits = [fruit.upper() for fruit in fruits]
             >>> print(loud_fruits)
-            ['BANANA', 'APPLE', 'LIME']
+            ['BANANA', 'APPLE', 'LIME']
 
-            # List and the enumerate function
+            # List and the enumerate function
             >>> list(enumerate(fruits))
-            [(0, 'Banana'), (1, 'Apple'), (2, 'Lime')]
+ [(0, 'Banana'), (1, 'Apple'), (2, 'Lime')] """, """\

Compound Data Types

@@ -97,19 +106,15 @@ def initial_data(): """, ), ( - """\ -
-            
-            # For loop on a list
+            """
+            # For loop on a list
             >>> numbers = [2, 4, 6, 8]
             >>> product = 1
             >>> for number in numbers:
             ...     product = product * number
             ...
             >>> print('The product is:', product)
-            The product is: 384
-            
-            
+ The product is: 384 """, """\

All the Flow You’d Expect

@@ -122,21 +127,17 @@ def initial_data(): """, ), ( - """\ -
-            
-            # Write Fibonacci series up to n
+            """
+            # Write Fibonacci series up to n
             >>> def fib(n):
             ...     a, b = 0, 1
-            ...     while a < n:
+            ...     while a < n:
             ...         print(a, end=' ')
             ...         a, b = b, a+b
             ...     print()
             ...
             >>> fib(1000)
-            0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
-            
-            
+ 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 """, """\

Functions Defined

@@ -151,7 +152,7 @@ def initial_data(): return { "boxes": [ CodeSampleFactory( - code=textwrap.dedent(code), + code=_highlight_python_console(code), copy=textwrap.dedent(copy), ) for code, copy in code_samples diff --git a/pyproject.toml b/pyproject.toml index e2233b91b..9eb2c0b79 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,6 +20,7 @@ dependencies = [ "docutils==0.21.2", "Markdown==3.7", "cmarkgfm==2024.11.20", + "Pygments>=2.17,<3", "Pillow==10.4.0", "psycopg2-binary==2.9.9", "python3-openid==3.2.0", diff --git a/static/sass/style.css b/static/sass/style.css index f937ab4d4..357ea40ee 100644 --- a/static/sass/style.css +++ b/static/sass/style.css @@ -1529,10 +1529,27 @@ input#s, .slide-code code { display: inline-block; color: #0d870d; } - .slide-code code .comment { - color: #666666; } - .slide-code code .output { + .slide-code code .k, .slide-code code .ow { + color: #6ab825; + font-weight: bold; } + .slide-code code .nb { + color: #24909d; } + .slide-code code .nf { + color: #447fcf; } + .slide-code code .s1, .slide-code code .s2, .slide-code code .sa, .slide-code code .si { + color: #ed9d13; } + .slide-code code .mi { + color: #3677a9; } + .slide-code code .o { + color: #999999; } + .slide-code code .c1 { + color: #999999; + font-style: italic; } + .slide-code code .go { color: #dddddd; } + .slide-code code .gp { + color: #c65d09; + font-weight: bold; } .js .launch-shell, .no-js .launch-shell { display: none; } diff --git a/static/sass/style.scss b/static/sass/style.scss index c16e823dc..a414a0d5a 100644 --- a/static/sass/style.scss +++ b/static/sass/style.scss @@ -697,9 +697,15 @@ input#s, display: inline-block; color: $code-green; - .comment { color: $grey; } - - .output { color: $grey-lighterer; } + .k, .ow { color: #6ab825; font-weight: bold; } + .nb { color: #24909d; } + .nf { color: #447fcf; } + .s1, .s2, .sa, .si { color: #ed9d13; } + .mi { color: #3677a9; } + .o { color: $grey-light; } + .c1 { color: $grey-light; font-style: italic; } + .go { color: $grey-lighterer; } + .gp { color: #c65d09; font-weight: bold; } } }