8000 Add support for syntax highlighting in PEPs by ilevkivskyi · Pull Request #1063 · python/pythondotorg · GitHub
[go: up one dir, main page]

Skip to content

Add support for syntax highlighting in PEPs #1063

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

Closed
wants to merge 2 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
25 changes: 25 additions & 0 deletions peps/converters.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@
from django.core.exceptions import ImproperlyConfigured
from django.core.files import File

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]

def check_paths():
""" Checks to ensure our PEP_REPO_PATH is setup correctly """
Expand Down Expand Up @@ -160,6 +170,21 @@ def convert_pep_page(pep_number, content):
# Fix PEP links
pep_content = BeautifulSoup(data['content'])
body_links = pep_content.find_all("a")
# Fix highlighting code
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)
# Highlight existing pure-Python PEPs
if int(pep_number) in PURE_PYTHON_PEPS:
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).html.body.div)

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

Expand Down
Loading
0