-
Notifications
You must be signed in to change notification settings - Fork 89
/
badges.py
66 lines (52 loc) · 1.99 KB
/
badges.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Display repository badges.
MyST usage (HTML only):
```{badges} https://mybook.site https://github.com/org/mybook
:doi: 10.5281.zenodo.12345678
```
"""
from docutils import nodes
from docutils.parsers.rst import Directive, directives
from sphinx.application import Sphinx
__version__ = '0.0.0'
def visit_nop(self, node):
pass
class badges_node(nodes.General, nodes.Element):
pass
def visit_badges_html(self, node):
self.body.append(
f"""<a href="{node['baseurl']}" target="_blank">
<img alt="site"
src="https://img.shields.io/badge/site-{node['baseurl'].replace('-', '--')}-orange" />
</a>""")
slug = '/'.join(node['repository_url'].split('/')[-2:])
self.body.append(
f"""<a href="{node['repository_url']}/graphs/contributors" target="_blank">
<img alt="last updated"
src="https://img.shields.io/github/last-commit/{slug}/main?label=updated" />
</a>""")
self.body.append(
f"""<a href="{node['repository_url']}/pulse" target="_blank">
<img alt="activity"
src="https://img.shields.io/github/commit-activity/m/{slug}/main?label=commits" />
</a>""")
if node['doi']:
self.body.append(
f"""<a href="https://doi.org/{node['doi']}" target="_blank">
<img alt="doi"
src="https://img.shields.io/badge/doi-{node['doi']}-black" />
</a>""")
class Badges(Directive):
has_content = True
required_arguments = 2
optional_arguments = 1
final_argument_whitespace = True
option_spec = {'doi': directives.unchanged}
_node = None
def run(self):
return [badges_node(
baseurl=self.arguments[0], repository_url=self.arguments[1], doi=self.options.get('doi', None))]
def setup(app: Sphinx):
app.add_node(badges_node, html=(visit_badges_html, visit_nop),
latex=(visit_nop, visit_nop))
app.add_directive("badges", Badges)
return {'version': __version__, 'parallel_read_safe': True}