8000 Add a Sphinx role to link to GitHub files by ezio-melotti · Pull Request #961 · python/devguide · GitHub
[go: up one dir, main page]

Skip to content

Add a Sphinx role to link to GitHub files #961

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

Merged
merged 6 commits into from
Nov 14, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
  • Loading branch information
ezio-melotti and AA-Turner authored Oct 10, 2022
commit 060b22a4dd925c3d7572aa76b087a87c2caf3c64
12 changes: 7 additions & 5 deletions _extensions/custom_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ def setup(app):


def autolink(pattern):
def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
def role(name, rawtext, text, lineno, inliner, _options=None, _content=None):
"""Combine literal + reference (unless the text is prefixed by a !)."""
if " " in text:
url_text = urllib.parse.quote(f"{text}")
url_text = urllib.parse.quote(text)
else:
url_text = text
url = pattern.format(url_text)
node = nodes.literal(rawtext, text.lstrip('!'), **options)
# don't create a reference if the text starts with !
if not text.startswith('!'):
node = nodes.reference('', '', node, refuri=url, **options)
if text.startswith('!'):
node = nodes.literal(rawtext, text[1:])
else:
node = nodes.reference(rawtext, '', nodes.literal(rawtext, text),
refuri=url, internal=False)
return [node], []

return role
0