-
Notifications
You must be signed in to change notification settings - Fork 615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for syntax highlighting in PEPs #1638
Changes from all commits
2f4c8fc
a91ec43
ee186f6
fc47728
367f758
3cbae81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,15 @@ | |
from django.core.files import File | ||
from django.db.models import Max | ||
|
||
from pygments import highlight | ||
from pygments.lexers import PythonLexer | ||
from pygments.formatters import HtmlFormatter | ||
|
||
from pages.models import Page, Image | ||
|
||
PEP_TEMPLATE = 'pages/pep-page.html' | ||
pep_url = lambda num: f'dev/peps/pep-{num}/' | ||
|
||
|
||
def get_peps_last_updated(): | ||
last_update = Page.objects.filter( | ||
path__startswith='dev/peps', | ||
|
@@ -140,6 +143,22 @@ def convert_pep_page(pep_number, content): | |
# Fix PEP links | ||
pep_content = BeautifulSoup(data['content'], 'lxml') | ||
body_links = pep_content.find_all("a") | ||
# Fix highlighting code | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This chunk (from here to the next comment) is dealing with blocks with an explicit language, such as More detail: the generated PEPs from https://github.com/python/peps has HTML like for these: <pre class="code python literal-block">
...
<pre class="code c literal-block"> And with no language specified ( <pre class="literal-block"> This chunk finds all |
||
code_blocks = pep_content.find_all('pre', class_='code') | ||
for cb in code_blocks: | ||
del cb['class'] | ||
div = pep_content.new_tag('div') | ||
div['class'] = ['highlight'] | ||
cb.wrap(div) | ||
|
||
# Default to Python highlighting for '::' code blocks. | ||
# Other languages can be set like '.. code-block:: c'. | ||
literal_blocks = pep_content.find_all('pre', class_='literal-block') | ||
for lb in literal_blocks: | ||
block = lb.string | ||
if block: | ||
highlighted = highlight(block, PythonLexer(), HtmlFormatter()) | ||
lb.replace_with(BeautifulSoup(highlighted, 'lxml').html.body.div) | ||
|
||
pep_href_re = re.compile(r'pep-(\d+)\.html') | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand this comment. Is there a plan to host the PEPs elsewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the plan is to build PEPs using Sphinx and host them on Read the Docs at peps.python.org: python/peps#2 (June 2016).
There's a PR to do add Sphinx support, but it's a big one and hasn't been reviewed yet: python/peps#1385 (April 2020).
Hence reviving this PR.