This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathspecs.py
More file actions
72 lines (56 loc) · 2.37 KB
/
Copy pathspecs.py
File metadata and controls
72 lines (56 loc) · 2.37 KB
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
67
68
69
70
71
72
import re
import unittest
from docutils import nodes
REFERENCE_RE = re.compile(r'''
^(
(?P<ref>[\w\-]+)
|
((?P<link_text>(.+))\s*<(?P<ref_brackets>[\w\-]+)>)
)$
''', re.VERBOSE)
SPECS = {
'apib': ('API Blueprint', 'https://apiblueprint.org/documentation/specification.html#{anchor}'),
'mson': ('MSON', 'https://apiblueprint.org/documentation/mson/specification.html#{anchor}'),
'openapi2': ('OpenAPI 2', 'https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md'), # TODO: re-add #{anchor}, fix yarn docs:lint
'openapi3': ('OpenAPI 3', 'https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#{anchor}'),
}
# https://docutils.readthedocs.io/en/sphinx-docs/howto/rst-roles.html
def link(name, rawtext, text, lineno, inliner, options=None, content=None):
title, url_template = SPECS[name]
try:
link_text, url = parse_text(text, url_template)
except ValueError:
message = "Could not parse {title} spec reference: '{text}'".format(
title=title,
text=text,
)
error = inliner.reporter.error(message, line=lineno)
problematic = inliner.problematic(rawtext, rawtext, error)
return [problematic], [error]
node = nodes.reference(rawtext, link_text, refuri=url, **(options or {}))
return [node], []
def parse_text(text, url_template):
match = REFERENCE_RE.match(text)
if match:
link_text = (match.group('link_text') or '').strip()
ref = match.group('ref') or match.group('ref_brackets')
url = url_template.format(anchor=ref)
return (link_text or 'spec', url)
raise ValueError(text)
def setup(app):
for name in SPECS.keys():
app.add_role(name, link)
return {'version': '1.0', 'parallel_read_safe': True}
class Tests(unittest.TestCase):
def test_anchor(self):
url_template = 'https://example.com#{anchor}'
link_text, url = parse_text('foobar', url_template)
self.assertEqual(link_text, 'spec')
self.assertEqual(url, 'https://example.com#foobar')
def test_custom_link_text(self):
url_template = 'https://example.com#{anchor}'
link_text, url = parse_text('my amazing link <foobar>', url_template)
self.assertEqual(link_text, 'my amazing link')
self.assertEqual(url, 'https://example.com#foobar')
if __name__ == '__main__':
unittest.main()