Skip to content

Commit

Permalink
Merge pull request #26 from ru-fu/monoref
Browse files Browse the repository at this point in the history
add a custom role for references with monospaced link text
  • Loading branch information
ru-fu authored Oct 30, 2023
2 parents 04d8739 + bb96425 commit 94c2a3f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ This extension adds custom roles that can be used in rST.

Currently implemented:

- `spellexception` - Includes the provided text in `<spellexception></spellexception>`, which makes it possible to exclude it from a spell checker.
- `spellexception` - Includes the provided text in `<spellexception></spellexception>`, which makes it possible to exclude it from a spelling checker.
- `monoref` - Renders the provided reference in code-style, which excludes the link text from the spelling checker.
You can provide either just the link (for example, ``:monoref:`www.example.com` ``, which results in `www.example.com` as the link text and `https://www.example.com` as the link URL) or a separate link text and URL (for example, ``:monoref:`xyzcommand <www.example.com>` ``).

### Config options

Expand Down
25 changes: 25 additions & 0 deletions custom-rst-roles/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from docutils import nodes
import re


def spellexception_role(
Expand All @@ -10,7 +11,31 @@ def spellexception_role(
return [node], []


def literalref_role(
name, rawtext, text, lineno, inliner, options=None, content=None
):

findURL = re.compile(r"^(.+)<(.+)>$")
m = findURL.match(text)

if m is not None:
linktext = m.groups()[0]
linkurl = m.groups()[1]
else:
linktext = text
linkurl = text

if linkurl.find("://") < 0:
linkurl = "https://" + linkurl

node = nodes.reference("", "", internal=False, refuri=linkurl)
node.append(nodes.literal(text=linktext))

return [node], []


def setup(app):
app.add_role("spellexception", spellexception_role)
app.add_role("literalref", literalref_role)

return
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = lxd-sphinx-extensions
version = 0.0.14
version = 0.0.15
author = Ruth Fuchss
author_email = [email protected]
description = A collection of Sphinx extensions used in LXD
Expand Down

0 comments on commit 94c2a3f

Please sign in to comment.