Created
March 30, 2023 04:06
-
-
Save tk0miya/4858e4e2898ef596714b856dfb7fe35c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# リテラルスタイルのアンカーを作る | |
# | |
# 使い方: | |
# * :codehref:`blah blah blah <http://path.to/anywhere>` | |
# * :codehref:`blah \`\`blah\`\` blah <http://path.to/anywhere>` | |
# | |
from docutils import nodes | |
from sphinx.util.docutils import ReferenceRole | |
class CodeHref(ReferenceRole): | |
def run(self): | |
if '``' in self.title: | |
result = [] | |
parts = self.title.split('``') | |
result.append(self.create_ref(parts.pop(0))) | |
while parts: | |
result.append(nodes.literal('', '', self.create_ref(parts.pop(0)))) | |
result.append(self.create_ref(parts.pop(0))) | |
return result, [] | |
else: | |
return [nodes.literal('', '', self.create_ref(self.title))], [] | |
def create_ref(self, text): | |
return nodes.reference(text, text, refuri=self.target) | |
def setup(app): | |
app.add_role('codehref', CodeHref()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment