Skip to content
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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions base-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ django-filter==2.4.0
django-ordered-model==3.4.3
django-widget-tweaks==1.4.8
django-countries==7.2.1

pygments==2.7.3 # This will be not needed when PEPs are moved to RtD.
Copy link
Member

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?

Copy link
Member Author

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.


xhtml2pdf==0.2.5
django-easy-pdf3==0.1.2
num2words==0.5.10
Expand Down
21 changes: 20 additions & 1 deletion peps/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The 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 .. code-block:: python or .. code-block:: c.

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 (::) have:

<pre class="literal-block">

This chunk finds all <pre class="code ...">, deletes those classes from the pre, and wraps them in a <div class="highlight"> for later processing.

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')

Expand Down
Loading