Skip to content

Commit 1bacc91

Browse files
committed
updating make-html.py
1 parent c1cf854 commit 1bacc91

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

basics/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ worked just fine. Later we'll learn what `(3, 14)` is.
6363
## Using Python as a calculator
6464

6565
```diff
66-
-WARNING: This part contains boring math. Be careful!
66+
-WARNING: This part contains boring math. Be careful!-
6767
```
6868

6969
Let's type some math stuff into Python and see what it does.

make-html.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,29 @@ def block_code(self, code, lang=None):
135135
formatter = pygments.formatters.HtmlFormatter(
136136
style=self.pygments_style, noclasses=True)
137137
return pygments.highlight(code, lexer, formatter)
138-
# we can't highlight it
139-
return super().block_code(code, lang)
138+
139+
elif lang == 'diff':
140+
# http://stackoverflow.com/a/39413824
141+
result = []
142+
for line in code.split('\n'):
143+
line = line.strip()
144+
if not line:
145+
continue
146+
147+
if line.startswith('+'):
148+
result.append('<p><font color="green">%s</font></p>'
149+
% line.strip('+'))
150+
elif line.startswith('-'):
151+
result.append('<p><font color="red">%s</font></p>'
152+
% line.strip('-'))
153+
else:
154+
result.append('<p>%s</p>' % line)
155+
156+
return '\n'.join(result)
157+
158+
else:
159+
# we can't highlight it
160+
return super().block_code(code, lang)
140161

141162
def image(self, src, title, text):
142163
"""Return an image inside a link."""

0 commit comments

Comments
 (0)