-
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 3 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,11 +10,21 @@ | |
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: 'dev/peps/pep-{}/'.format(num) | ||
|
||
# To simplify syntax highlighting, all literal blocks (those produced by ::) | ||
# in the following PEPs will be automatically highlighted using Python lexer. | ||
# PEP editors/authors could make simple PRs extending this list. | ||
# This will be not needed when PEPs are moved to RtD and all code blocks are | ||
# formatted using .. code:: language. | ||
PURE_PYTHON_PEPS = [483, 484, 526] | ||
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. Seems like it would make more sense to update the PEPs with appropriate code blocks rather than do this. I think this would also remove the need for the following lines (although I'm not sure what "Fix highlighting code" is currently doing. 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. I agree, I'll comment below what's happening. 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 is no longer used. |
||
|
||
def get_peps_last_updated(): | ||
last_update = Page.objects.filter( | ||
|
@@ -140,6 +150,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.