49
49
try :
50
50
import pygments .formatters
51
51
import pygments .lexers
52
+ import pygments .style
52
53
import pygments .styles
54
+ import pygments .token
53
55
except ImportError :
54
56
# we can work without pygments, but we won't get colors
55
57
pygments = None
56
58
57
59
import common
58
60
59
61
62
+ if pygments is not None :
63
+ class TutorialStyle (pygments .style .Style ):
64
+ background_color = '#111111'
65
+ styles = {
66
+ pygments .token .Comment : 'italic #336666' ,
67
+ pygments .token .Keyword : 'bold #6699cc' ,
68
+ pygments .token .Name .Builtin : '#9966ff' ,
69
+ pygments .token .String : '#ffff33' ,
70
+ pygments .token .Name .Exception : 'bold #ff0000' ,
71
+ }
72
+
73
+
60
74
HTML_TEMPLATE = """\
61
75
<!DOCTYPE html>
62
76
<html>
@@ -129,9 +143,9 @@ def block_code(self, code, lang=None):
129
143
if lang == 'python' and pygments is not None :
130
144
# we can highlight it
131
145
if code .startswith ('>>> ' ):
132
- lexer = pygments .lexers .PythonConsoleLexer ()
146
+ lexer = pygments .lexers .PythonConsoleLexer (python3 = True )
133
147
else :
134
- lexer = pygments .lexers .PythonLexer ()
148
+ lexer = pygments .lexers .Python3Lexer ()
135
149
formatter = pygments .formatters .HtmlFormatter (
136
150
style = self .pygments_style , noclasses = True )
137
151
return pygments .highlight (code , lexer , formatter )
@@ -198,10 +212,10 @@ def main():
198
212
help = "write the HTML files here, defaults to %(default)r" )
199
213
if pygments is not None :
200
214
parser .add_argument (
201
- '--pygments-style' , metavar = 'STYLE' , default = 'native' ,
215
+ '--pygments-style' , metavar = 'STYLE' , default = TutorialStyle ,
202
216
choices = list (pygments .styles .get_all_styles ()),
203
217
help = ("the Pygments color style (see above), "
204
- "%(default)r by default " ))
218
+ "defaults to a custom style " ))
205
219
args = parser .parse_args ()
206
220
207
221
if pygments is None :
@@ -215,6 +229,7 @@ def main():
215
229
if not common .askyesno ("Continue without pygments?" ):
216
230
print ("Interrupt." )
217
231
return
232
+ args .pygments_style = None
218
233
219
234
if os .path .exists (args .outdir ):
220
235
if not common .askyesno ("%s exists. Do you want to remove it?"
@@ -228,16 +243,16 @@ def main():
228
243
229
244
print ("Generating HTML files..." )
230
245
for markdownfile in common .get_markdown_files ():
231
- fixed_markdownfile = fix_filename (markdownfile )
232
- htmlfile = posixpath .join (args .outdir , fixed_markdownfile )
246
+ fixed_file = fix_filename (markdownfile )
247
+ htmlfile = posixpath .join (args .outdir , fixed_file )
233
248
print (' %-30.30s --> %-30.30s' % (markdownfile , htmlfile ), end = '\r ' )
234
249
235
250
with common .slashfix_open (markdownfile , 'r' ) as f :
236
251
markdown = f .read ()
237
252
renderer = TutorialRenderer (args .pygments_style )
238
253
body = mistune .markdown (markdown , renderer = renderer )
239
254
stylefile = posixpath .relpath (
240
- 'style.css' , posixpath .dirname (fixed_markdownfile ))
255
+ 'style.css' , posixpath .dirname (fixed_file ))
241
256
242
257
html = HTML_TEMPLATE .format (
243
258
title = renderer .title ,
0 commit comments