1
+ import re
1
2
import textwrap
2
3
3
4
import factory
@@ -23,20 +24,31 @@ class Meta:
23
24
24
25
25
26
def initial_data ():
27
+ def format_html (code ):
28
+ """Add HTML tags for highlighting of the given code snippet."""
29
+ code = code .strip ()
30
+ for pattern ,repl in [
31
+ (r'^([^\s\.>#].*)$' ,r'<span class="output">\1</span>' ),
32
+ (r'^(>>>)' ,r'<span class="code-prompt">\1</span>' ),
33
+ (r'^(\.\.\.)' ,r'<span class="code-prompt">\1</span>' ),
34
+ (r'(#.*)$' ,r'<span class="comment">\1</span>' ),
35
+ ]:
36
+ code = re .sub (pattern ,repl ,code ,flags = re .MULTILINE )
37
+ return f'<pre><code>{ code } </code></pre>'
38
+
26
39
code_samples = [
27
40
(
28
- """\
29
- <pre><code><span class= \" comment \" > # Simple output (with Unicode)</span>
41
+ r """
42
+ # Simple output (with Unicode)
30
43
>>> print("Hello, I'm Python!")
31
- <span class= \" output \" > Hello, I'm Python!</span>
44
+ Hello, I'm Python!
32
45
33
- <span class= \" comment \" > # Input, assignment</span>
46
+ # Input, assignment
34
47
>>> name = input('What is your name?\n')
35
- <span class=\" output\" >What is your name?
36
- Python</span>
37
- >>> print(f'Hi, {name}.')
38
- <span class=\" output\" >Hi, Python.</span></code>
39
- </pre>
48
+ >>> print('Hi, %s.' % name)
49
+ What is your name?
50
+ Python
51
+ Hi, Python.
40
52
""" ,
41
53
"""\
42
54
<h1>Quick & Easy to Learn</h1>
@@ -48,16 +60,16 @@ def initial_data():
48
60
"""
49
61
),
50
62
(
51
- """\
52
- <pre><code><span class= \" comment \" > # Simple arithmetic</span>
63
+ """
64
+ # Simple arithmetic
53
65
>>> 1 / 2
54
- <span class= \" output \" > 0.5</span>
66
+ 0.5
55
67
>>> 2 ** 3
56
- <span class= \" output \" >8</span>
57
- >>> 17 / 3<span class= \" comment \" > # true division returns a float</span>
58
- <span class= \" output \" > 5.666666666666667</span>
59
- >>> 17 // 3<span class= \" comment \" > # floor division</span>
60
- <span class= \" output \" >5</span></code></pre>
68
+ 8
69
+ >>> 17 / 3 # true division returns a float
70
+ 5.666666666666667
71
+ >>> 17 // 3 # floor division
72
+ 5
61
73
""" ,
62
74
"""\
63
75
<h1>Intuitive Interpretation</h1>
@@ -71,16 +83,16 @@ def initial_data():
71
83
"""
72
84
),
73
85
(
74
- """\
75
- <pre><code><span class= \" comment \" > # List comprehensions</span>
86
+ """
87
+ # List comprehensions
76
88
>>> fruits = ['Banana', 'Apple', 'Lime']
77
89
>>> loud_fruits = [fruit.upper() for fruit in fruits]
78
90
>>> print(loud_fruits)
79
- <span class= \" output \" > ['BANANA', 'APPLE', 'LIME']</span>
91
+ ['BANANA', 'APPLE', 'LIME']
80
92
81
- <span class= \" comment \" > # List and the enumerate function</span>
93
+ # List and the enumerate function
82
94
>>> list(enumerate(fruits))
83
- <span class= \" output \" > [(0, 'Banana'), (1, 'Apple'), (2, 'Lime')]</span></code></pre>
95
+ [(0, 'Banana'), (1, 'Apple'), (2, 'Lime')]
84
96
""" ,
85
97
"""\
86
98
<h1>Compound Data Types</h1>
@@ -92,19 +104,15 @@ def initial_data():
92
104
"""
93
105
),
94
106
(
95
- """\
96
- <pre>
97
- <code>
98
- <span class=\" comment\" ># For loop on a list</span>
107
+ """
108
+ # For loop on a list
99
109
>>> numbers = [2, 4, 6, 8]
100
110
>>> product = 1
101
111
>>> for number in numbers:
102
112
... product = product * number
103
113
...
104
114
>>> print('The product is:', product)
105
- <span class=\" output\" >The product is: 384</span>
106
- </code>
107
- </pre>
115
+ The product is: 384
108
116
""" ,
109
117
"""\
110
118
<h1>All the Flow You’d Expect</h1>
@@ -117,20 +125,16 @@ def initial_data():
117
125
"""
118
126
),
119
127
(
120
- """\
121
- <pre>
122
- <code>
123
- <span class=\" comment\" ># Write Fibonacci series up to n</span>
128
+ """
129
+ # Write Fibonacci series up to n
124
130
>>> def fib(n):
125
131
>>> a, b = 0, 1
126
132
>>> while a < n:
127
133
>>> print(a, end=' ')
128
134
>>> a, b = b, a+b
129
135
>>> print()
130
136
>>> fib(1000)
131
- <span class=\" output\" >0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610</span>
132
- </code>
133
- </pre>
137
+ 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
134
138
""" ,
135
139
"""\
136
140
<h1>Functions Defined</h1>
@@ -145,7 +149,8 @@ def initial_data():
145
149
return {
146
150
'boxes' : [
147
151
CodeSampleFactory (
148
- code = textwrap .dedent (code ),copy = textwrap .dedent (copy ),
152
+ code = format_html (textwrap .dedent (code )),
153
+ copy = textwrap .dedent (copy ),
149
154
)for code ,copy in code_samples
150
155
],
151
156
}