File: github.py

package info (click to toggle)
python-itemloaders 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 320 kB
  • sloc: python: 1,547; makefile: 78
file content (32 lines) | stat: -rw-r--r-- 771 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from typing import Optional

from docutils import nodes
from docutils.parsers.rst.roles import set_classes


def setup(app):
    app.add_role("gh", github_role)


def github_role(
    name,
    rawtext,
    text,
    lineno,
    inliner,
    options: Optional[dict] = None,
    content: Optional[list] = None,
):
    options = options or {}
    content = content or []
    if text.isdigit():
        display_text = f"#{text}"
        url = f"https://github.com/scrapy/itemloaders/issues/{text}"
    else:
        short_commit = text[:7]
        display_text = short_commit
        url = f"https://github.com/scrapy/itemloaders/commit/{short_commit}"

    set_classes(options)
    node = nodes.reference(rawtext, display_text, refuri=url, **options)
    return [node], []