chiark / gitweb /
plugins: reST roles for linking to GitHub articles.
authorVladimír Vondruš <mosra@centrum.cz>
Tue, 4 Jul 2017 09:22:01 +0000 (11:22 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Wed, 5 Jul 2017 23:02:08 +0000 (01:02 +0200)
pelican-plugins/m/__init__.py [new file with mode: 0644]
pelican-plugins/m/gh.py [new file with mode: 0644]

diff --git a/pelican-plugins/m/__init__.py b/pelican-plugins/m/__init__.py
new file mode 100644 (file)
index 0000000..4ab2dca
--- /dev/null
@@ -0,0 +1,12 @@
+#!/usr/bin/env python
+
+from docutils import utils
+import re
+
+link_regexp = re.compile(r'(?P<title>.*) <(?P<link>.+)>')
+
+def parse_link(text):
+    link = utils.unescape(text)
+    m = link_regexp.match(link)
+    if m: return m.group('title', 'link')
+    return None, link
diff --git a/pelican-plugins/m/gh.py b/pelican-plugins/m/gh.py
new file mode 100644 (file)
index 0000000..9db3bcc
--- /dev/null
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+
+from . import parse_link
+from docutils import nodes
+from docutils.parsers import rst
+
+def gh_internal(account, ref, title, link):
+    base_url = "https://github.com/{}/{}/{}/{}"
+    if '#' in ref:
+        project, _, issue = ref.partition('#')
+        url = base_url.format(account, project, "issues", issue)
+        if not title: title = link
+    elif '@' in ref:
+        project, _, commit = ref.partition('@')
+        url = base_url.format(account, project, "commit", commit)
+        if not title: title = link
+    elif '$' in ref:
+        project, _, branch = ref.partition('$')
+        url = base_url.format(account, project, "tree", branch)
+        if not title: title = url
+    elif '^' in ref:
+        project, _, branch = ref.partition('^')
+        url = base_url.format(account, project, "releases/tag", branch)
+        if not title: title = url
+    else:
+        url = "https://github.com/{}/{}".format(account, ref)
+        if not title: title = url
+
+    return title, url
+
+def gh(name, rawtext, text, lineno, inliner, options={}, content=[]):
+    title, link = parse_link(text)
+    account, _, ref = link.partition('/')
+    if not ref:
+        url = "https://github.com/{}".format(account)
+        if not title: title = "@{}".format(account)
+    else:
+        title, url = gh_internal(account, ref, title, link)
+
+    node = nodes.reference(rawtext, title, refuri=url, **options)
+    return [node], []
+
+def register():
+    rst.roles.register_local_role('gh', gh)