diff --git a/codesamples/factories.py b/codesamples/factories.py index 5a52b9738..ed3e2b3a5 100644 --- a/codesamples/factories.py +++ b/codesamples/factories.py @@ -1,3 +1,4 @@ +import re import textwrap import factory @@ -23,20 +24,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)
+ r"""
+ # Simple output (with Unicode)
>>> print("Hello, I'm Python!")
- Hello, I'm Python!
+ Hello, I'm Python!
- # Input, assignment
+ # Input, assignment
>>> name = input('What is your name?\n')
- What is your name?
- Python
- >>> print(f'Hi, {name}.')
- Hi, Python.
-
+ >>> print('Hi, %s.' % name)
+ What is your name?
+ Python
+ Hi, Python.
""",
"""\
# 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
""",
"""\
# 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')]
""",
"""\
-
- # 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
""",
"""\
-
- # Write Fibonacci series up to n
+ """
+ # Write Fibonacci series up to n
>>> def fib(n):
>>> a, b = 0, 1
>>> while a < n:
@@ -128,9 +134,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
""",
"""\