From e06c803f81b682cb5849e83eaf63918cab3ddfb5 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 20 Oct 2019 16:06:24 +0200 Subject: [PATCH] Improve highlighting of code samples Add highlighting for the prompt marker in the code samples. Refactor the html formatting so that the html tags are automatically generated and don't have to be written manually in the code samples sources. --- codesamples/factories.py | 79 +++++++++++++++++++++------------------- static/sass/style.css | 3 ++ 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/codesamples/factories.py b/codesamples/factories.py index fe8715fd7..6556ce094 100644 --- a/codesamples/factories.py +++ b/codesamples/factories.py @@ -1,3 +1,4 @@ +import re import textwrap import factory @@ -22,20 +23,31 @@ class Meta: def initial_data(): + def format_html(code): + """Add HTML tags for highlighting of the given code snippet.""" + code = code.strip() + for pattern, repl in [ + (r'^([^\s\.>#].*)$', r'\1'), + (r'^(>>>)', r'\1'), + (r'^(\.\.\.)', r'\1'), + (r'(#.*)$', r'\1'), + ]: + code = re.sub(pattern, repl, code, flags=re.MULTILINE) + return f'
{code}
' + code_samples = [ ( - """\ -
# Simple output (with Unicode)
-            >>> print(\"Hello, I'm Python!\")
-            Hello, I'm Python!
+            r"""
+            # Simple output (with Unicode)
+            >>> print("Hello, I'm Python!")
+            Hello, I'm Python!
 
-            # Input, assignment
-            >>> name = input('What is your name?\\n')
+            # Input, assignment
+            >>> name = input('What is your name?\n')
             >>> print('Hi, %s.' % name)
-            What is your name?
+            What is your name?
             Python
-            Hi, Python.
-            
+ Hi, Python. """, """\

Quick & Easy to Learn

@@ -47,16 +59,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

@@ -70,16 +82,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

@@ -91,19 +103,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

@@ -116,10 +124,8 @@ 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:
@@ -127,9 +133,7 @@ def initial_data():
             >>>         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

@@ -144,7 +148,8 @@ def initial_data(): return { 'boxes': [ CodeSampleFactory( - code=textwrap.dedent(code), copy=textwrap.dedent(copy), + code=format_html(textwrap.dedent(code)), + copy=textwrap.dedent(copy), ) for code, copy in code_samples ], } diff --git a/static/sass/style.css b/static/sass/style.css index 1425afb3e..937dd9505 100644 --- a/static/sass/style.css +++ b/static/sass/style.css @@ -1632,6 +1632,9 @@ input#s, color: #666666; } .slide-code code .output { color: #dddddd; } + .slide-code code .code-prompt { + color: #c65d09; } + .js .launch-shell, .no-js .launch-shell { display: none; }