Skip to content

Commit e340ac1

Browse files
ilevkivskyihugovk
authored andcommitted
Add support for syntax highlighting in PEPs
1 parent 65cb6aa commit e340ac1

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

base-requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ django-waffle==0.14
3636

3737
djangorestframework==3.8.2
3838
django-filter==1.1.0
39+
40+
pygments==2.1.3

peps/converters.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
from django.core.files import File
1111
from django.db.models import Max
1212

13+
from pygments import highlight
14+
from pygments.lexers import PythonLexer
15+
from pygments.formatters import HtmlFormatter
16+
1317
from pages.models import Page, Image
1418

1519
PEP_TEMPLATE = 'pages/pep-page.html'
1620
pep_url = lambda num: 'dev/peps/pep-{}/'.format(num)
1721

22+
PURE_PYTHON_PEPS = [483, 484, 526]
1823

1924
def get_peps_last_updated():
2025
last_update = Page.objects.filter(
@@ -140,6 +145,21 @@ def convert_pep_page(pep_number, content):
140145
# Fix PEP links
141146
pep_content = BeautifulSoup(data['content'], 'lxml')
142147
body_links = pep_content.find_all("a")
148+
# Fix hihglighting code
149+
code_blocks = pep_content.find_all("pre", class_="code")
150+
for cb in code_blocks:
151+
del cb['class']
152+
div = pep_content.new_tag('div')
153+
div['class'] = ['highlight']
154+
cb.wrap(div)
155+
# Highlight existing pure-Python PEPs
156+
if int(pep_number) in PURE_PYTHON_PEPS:
157+
literal_blocks = pep_content.find_all("pre", class_="literal-block")
158+
for lb in literal_blocks:
159+
block = lb.string
160+
if block:
161+
highlighted = highlight(block, PythonLexer(), HtmlFormatter())
162+
lb.replace_with(BeautifulSoup(highlighted).html.body.div)
143163

144164
pep_href_re = re.compile(r'pep-(\d+)\.html')
145165

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
.highlight .c { color: #408080; font-style: italic } /* Comment */
2+
.highlight .err { border: 1px solid #FF0000 } /* Error */
3+
.highlight .k { color: #008000; font-weight: bold } /* Keyword */
4+
.highlight .o { color: #666666 } /* Operator */
5+
.highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */
6+
.highlight .cp { color: #BC7A00 } /* Comment.Preproc */
7+
.highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */
8+
.highlight .cs { color: #408080; font-style: italic } /* Comment.Special */
9+
.highlight .gd { color: #A00000 } /* Generic.Deleted */
10+
.highlight .ge { font-style: italic } /* Generic.Emph */
11+
.highlight .gr { color: #FF0000 } /* Generic.Error */
12+
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
13+
.highlight .gi { color: #00A000 } /* Generic.Inserted */
14+
.highlight .go { color: #808080 } /* Generic.Output */
15+
.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
16+
.highlight .gs { font-weight: bold } /* Generic.Strong */
17+
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
18+
.highlight .gt { color: #0040D0 } /* Generic.Traceback */
19+
.highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
20+
.highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
21+
.highlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
22+
.highlight .kp { color: #008000 } /* Keyword.Pseudo */
23+
.highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
24+
.highlight .kt { color: #B00040 } /* Keyword.Type */
25+
.highlight .m { color: #666666 } /* Literal.Number */
26+
.highlight .s { color: #BA2121 } /* Literal.String */
27+
.highlight .na { color: #7D9029 } /* Name.Attribute */
28+
.highlight .nb { color: #008000 } /* Name.Builtin */
29+
.highlight .nc { color: #3333cc; font-weight: bold } /* Name.Class */
30+
.highlight .no { color: #880000 } /* Name.Constant */
31+
.highlight .nd { color: #AA22FF } /* Name.Decorator */
32+
.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */
33+
.highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
34+
.highlight .nf { color: #3333cc } /* Name.Function */
35+
.highlight .nl { color: #A0A000 } /* Name.Label */
36+
.highlight .nn { color: #3333cc; font-weight: bold } /* Name.Namespace */
37+
.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
38+
.highlight .nv { color: #19177C } /* Name.Variable */
39+
.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
40+
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
41+
.highlight .mf { color: #666666 } /* Literal.Number.Float */
42+
.highlight .mh { color: #666666 } /* Literal.Number.Hex */
43+
.highlight .mi { color: #666666 } /* Literal.Number.Integer */
44+
.highlight .mo { color: #666666 } /* Literal.Number.Oct */
45+
.highlight .sb { color: #BA2121 } /* Literal.String.Backtick */
46+
.highlight .sc { color: #BA2121 } /* Literal.String.Char */
47+
.highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
48+
.highlight .s2 { color: #BA2121 } /* Literal.String.Double */
49+
.highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
50+
.highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */
51+
.highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
52+
.highlight .sx { color: #008000 } /* Literal.String.Other */
53+
.highlight .sr { color: #BB6688 } /* Literal.String.Regex */
54+
.highlight .s1 { color: #BA2121 } /* Literal.String.Single */
55+
.highlight .ss { color: #19177C } /* Literal.String.Symbol */
56+
.highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */
57+
.highlight .vc { color: #19177C } /* Name.Variable.Class */
58+
.highlight .vg { color: #19177C } /* Name.Variable.Global */
59+
.highlight .vi { color: #19177C } /* Name.Variable.Instance */
60+
.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */
61+
.highlight .lineno { color: #000000; background-color: #dddddd; }
62+
63+
.highlight .comment { color: #408080; font-style: italic } /* Comment */
64+
.highlight .error { border: 1px solid #FF0000 } /* Error */
65+
.highlight .keyword { color: #008000; font-weight: bold } /* Keyword */
66+
.highlight .operator { color: #666666 } /* Operator */
67+
.highlight .comment.multiline { color: #408080; font-style: italic } /* Comment.Multiline */
68+
.highlight .comment.preproc { color: #BC7A00 } /* Comment.Preproc */
69+
.highlight .comment.single { color: #408080; font-style: italic } /* Comment.Single */
70+
.highlight .comment.special { color: #408080; font-style: italic } /* Comment.Special */
71+
.highlight .generic.deleted { color: #A00000 } /* Generic.Deleted */
72+
.highlight .generic.emph { font-style: italic } /* Generic.Emph */
73+
.highlight .generic.error { color: #FF0000 } /* Generic.Error */
74+
.highlight .generic.heading { color: #000080; font-weight: bold } /* Generic.Heading */
75+
.highlight .generic.inserted { color: #00A000 } /* Generic.Inserted */
76+
.highlight .generic.output { color: #808080 } /* Generic.Output */
77+
.highlight .generic.prompt { color: #000080; font-weight: bold } /* Generic.Prompt */
78+
.highlight .generic.strong { font-weight: bold } /* Generic.Strong */
79+
.highlight .generic.subheading { color: #800080; font-weight: bold } /* Generic.Subheading */
80+
.highlight .generic.traceback { color: #0040D0 } /* Generic.Traceback */
81+
.highlight .keyword.constant { color: #008000; font-weight: bold } /* Keyword.Constant */
82+
.highlight .keyword.declaration { color: #008000; font-weight: bold } /* Keyword.Declaration */
83+
.highlight .keyword.namespace { color: #008000; font-weight: bold } /* Keyword.Namespace */
84+
.highlight .keyword.pseudo { color: #008000 } /* Keyword.Pseudo */
85+
.highlight .keyword.reserved { color: #008000; font-weight: bold } /* Keyword.Reserved */
86+
.highlight .keyword.type { color: #B00040 } /* Keyword.Type */
87+
.highlight .number { color: #666666 } /* Literal.Number */
88+
.highlight .string { color: #BA2121 } /* Literal.String */
89+
.highlight .name.attribute { color: #7D9029 } /* Name.Attribute */
90+
.highlight .name.builtin { color: #008000 } /* Name.Builtin */
91+
.highlight .name.class { color: #3333cc; font-weight: bold } /* Name.Class */
92+
.highlight .name.constant { color: #880000 } /* Name.Constant */
93+
.highlight .name.decorator { color: #AA22FF } /* Name.Decorator */
94+
.highlight .name.entity { color: #999999; font-weight: bold } /* Name.Entity */
95+
.highlight .name.exception { color: #D2413A; font-weight: bold } /* Name.Exception */
96+
.highlight .name.function { color: #3333cc } /* Name.Function */
97+
.highlight .name.label { color: #A0A000 } /* Name.Label */
98+
.highlight .name.namespace { color: #3333cc; font-weight: bold } /* Name.Namespace */
99+
.highlight .name.tag { color: #008000; font-weight: bold } /* Name.Tag */
100+
.highlight .name.variable { color: #19177C } /* Name.Variable */
101+
.highlight .operator.word { color: #AA22FF; font-weight: bold } /* Operator.Word */
102+
.highlight .whitespace { color: #bbbbbb } /* Text.Whitespace */
103+
.highlight .number.float { color: #666666 } /* Literal.Number.Float */
104+
.highlight .number.hex { color: #666666 } /* Literal.Number.Hex */
105+
.highlight .number.integer { color: #666666 } /* Literal.Number.Integer */
106+
.highlight .number.oct { color: #666666 } /* Literal.Number.Oct */
107+
.highlight .string.backtick { color: #BA2121 } /* Literal.String.Backtick */
108+
.highlight .string.char { color: #BA2121 } /* Literal.String.Char */
109+
.highlight .string.doc { color: #BA2121; font-style: italic } /* Literal.String.Doc */
110+
.highlight .string.double { color: #BA2121 } /* Literal.String.Double */
111+
.highlight .string.escape { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
112+
.highlight .string.heredoc { color: #BA2121 } /* Literal.String.Heredoc */
113+
.highlight .string.interpol { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
114+
.highlight .string.other { color: #008000 } /* Literal.String.Other */
115+
.highlight .string.regex { color: #BB6688 } /* Literal.String.Regex */
116+
.highlight .string.single { color: #BA2121 } /* Literal.String.Single */
117+
.highlight .string.symbol { color: #19177C } /* Literal.String.Symbol */
118+
.highlight .name.builtin.pseudo { color: #008000 } /* Name.Builtin.Pseudo */
119+
.highlight .name.variable.class { color: #19177C } /* Name.Variable.Class */
120+
.highlight .name.variable.global { color: #19177C } /* Name.Variable.Global */
121+
.highlight .name.variable.instance { color: #19177C } /* Name.Variable.Instance */
122+
.highlight .number.integer.long { color: #666666 } /* Literal.Number.Integer.Long */
123+
.highlight .lineno { color: #000000; background-color: #dddddd; }

templates/pages/pep-page.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
{% endblock %}
2626
.
2727
{% block content %}
28+
<link rel="stylesheet" type="text/css" href="/static/stylesheets/pygments_default.css" />
2829
<style>
2930
.pep-page pre {
3031
padding: .5em;

0 commit comments

Comments
 (0)