Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc9c98c6

Browse files
timhoffmJacobCoffee
authored andcommitted
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 automaticallygenerated and don't have to be written manually in the code samplessources.
1 parente89f46d commitc9c98c6

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

‎codesamples/factories.py

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
importre
12
importtextwrap
23

34
importfactory
@@ -23,20 +24,31 @@ class Meta:
2324

2425

2526
definitial_data():
27+
defformat_html(code):
28+
"""Add HTML tags for highlighting of the given code snippet."""
29+
code=code.strip()
30+
forpattern,replin [
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+
returnf'<pre><code>{code}</code></pre>'
38+
2639
code_samples= [
2740
(
28-
"""\
29-
<pre><code><span class=\"comment\"># Simple output (with Unicode)</span>
41+
r"""
42+
# Simple output (with Unicode)
3043
>>> print("Hello, I'm Python!")
31-
<span class=\"output\">Hello, I'm Python!</span>
44+
Hello, I'm Python!
3245
33-
<span class=\"comment\"># Input, assignment</span>
46+
# Input, assignment
3447
>>> 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.
4052
""",
4153
"""\
4254
<h1>Quick &amp; Easy to Learn</h1>
@@ -48,16 +60,16 @@ def initial_data():
4860
"""
4961
),
5062
(
51-
"""\
52-
<pre><code><span class=\"comment\"># Simple arithmetic</span>
63+
"""
64+
# Simple arithmetic
5365
>>> 1 / 2
54-
<span class=\"output\">0.5</span>
66+
0.5
5567
>>> 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
6173
""",
6274
"""\
6375
<h1>Intuitive Interpretation</h1>
@@ -71,16 +83,16 @@ def initial_data():
7183
"""
7284
),
7385
(
74-
"""\
75-
<pre><code><span class=\"comment\"># List comprehensions</span>
86+
"""
87+
# List comprehensions
7688
>>> fruits = ['Banana', 'Apple', 'Lime']
7789
>>> loud_fruits = [fruit.upper() for fruit in fruits]
7890
>>> print(loud_fruits)
79-
<span class=\"output\">['BANANA', 'APPLE', 'LIME']</span>
91+
['BANANA', 'APPLE', 'LIME']
8092
81-
<span class=\"comment\"># List and the enumerate function</span>
93+
# List and the enumerate function
8294
>>> list(enumerate(fruits))
83-
<span class=\"output\">[(0, 'Banana'), (1, 'Apple'), (2, 'Lime')]</span></code></pre>
95+
[(0, 'Banana'), (1, 'Apple'), (2, 'Lime')]
8496
""",
8597
"""\
8698
<h1>Compound Data Types</h1>
@@ -92,19 +104,15 @@ def initial_data():
92104
"""
93105
),
94106
(
95-
"""\
96-
<pre>
97-
<code>
98-
<span class=\"comment\"># For loop on a list</span>
107+
"""
108+
# For loop on a list
99109
>>> numbers = [2, 4, 6, 8]
100110
>>> product = 1
101111
>>> for number in numbers:
102112
... product = product * number
103113
...
104114
>>> print('The product is:', product)
105-
<span class=\"output\">The product is: 384</span>
106-
</code>
107-
</pre>
115+
The product is: 384
108116
""",
109117
"""\
110118
<h1>All the Flow You&rsquo;d Expect</h1>
@@ -117,20 +125,16 @@ def initial_data():
117125
"""
118126
),
119127
(
120-
"""\
121-
<pre>
122-
<code>
123-
<span class=\"comment\"># Write Fibonacci series up to n</span>
128+
"""
129+
# Write Fibonacci series up to n
124130
>>> def fib(n):
125131
>>> a, b = 0, 1
126132
>>> while a &lt; n:
127133
>>> print(a, end=' ')
128134
>>> a, b = b, a+b
129135
>>> print()
130136
>>> 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
134138
""",
135139
"""\
136140
<h1>Functions Defined</h1>
@@ -145,7 +149,8 @@ def initial_data():
145149
return {
146150
'boxes': [
147151
CodeSampleFactory(
148-
code=textwrap.dedent(code),copy=textwrap.dedent(copy),
152+
code=format_html(textwrap.dedent(code)),
153+
copy=textwrap.dedent(copy),
149154
)forcode,copyincode_samples
150155
],
151156
}

‎static/sass/style.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ form, .header-banner, .success-stories-widget .quote-from {
405405
.slides,
406406
.flex-control-nav,
407407
.flex-direction-nav {margin: 0; padding: 0; list-style: none;} */
408-
/* FlexSlider Necessary Styles
408+
/* FlexSlider Necessary Styles
409409
.flexslider {margin: 0; padding: 0;}
410410
.flexslider .slides > li {display: none; -webkit-backface-visibility: hidden;} /* Hide the slides before the JS is loaded. Avoids image jumping
411411
.flexslider .slides img {width: 100%; display: block;}
@@ -1640,6 +1640,8 @@ input#s,
16401640
color:#666; }
16411641
.slide-codecode .output {
16421642
color:#ddd; }
1643+
.slide-codecode .code-prompt {
1644+
color:#c65d09; }
16431645

16441646
.js .launch-shell, .no-js .launch-shell {
16451647
display: none; }

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp