Skip to content

Commit 47c3e5f

Browse files
committed
Tidy up the URLs in the docutils role.
1 parent de77c68 commit 47c3e5f

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

sphinx_json_schema_spec/__init__.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
from urllib.parse import urljoin
23
import errno
34
import os
45
import urllib.request
@@ -10,7 +11,11 @@
1011
import jsonschema
1112

1213
__version__ = "1.1.0"
13-
VALIDATION_SPEC = "https://json-schema.org/draft-07/json-schema-validation.html"
14+
15+
BASE_URL = "https://json-schema.org/draft-07/"
16+
VALIDATION_SPEC = urljoin(BASE_URL, "json-schema-validation.html")
17+
REF_URL = urljoin(BASE_URL, "json-schema-core.html#rfc.section.8.3")
18+
SCHEMA_URL = urljoin(BASE_URL, "json-schema-core.html#rfc.section.7")
1419

1520

1621
def setup(app):
@@ -34,7 +39,7 @@ def setup(app):
3439

3540
path = os.path.join(app.config.cache_path, "spec.html")
3641
spec = fetch_or_load(path)
37-
app.add_role("validator", docutils_sucks(spec))
42+
app.add_role("validator", docutils_does_not_allow_using_classes(spec))
3843

3944
return dict(version=__version__, parallel_read_safe=True)
4045

@@ -77,18 +82,15 @@ def fetch_or_load(spec_path):
7782
return html.parse(spec)
7883

7984

80-
def docutils_sucks(spec):
85+
def docutils_does_not_allow_using_classes(spec):
8186
"""
8287
Yeah.
8388
84-
It doesn't allow using a class because it does stupid stuff like try to set
85-
attributes on the callable object rather than just keeping a dict.
89+
It doesn't allow using a class because it does annoying stuff like
90+
try to set attributes on the callable object rather than just
91+
keeping a dict.
8692
"""
8793

88-
base_url = VALIDATION_SPEC
89-
ref_url = "https://json-schema.org/draft-07/json-schema-core.html#rfc.section.8.3"
90-
schema_url = "https://json-schema.org/draft-07/json-schema-core.html#rfc.section.7"
91-
9294
def validator(name, raw_text, text, lineno, inliner):
9395
"""
9496
Link to the JSON Schema documentation for a validator.
@@ -124,9 +126,9 @@ def validator(name, raw_text, text, lineno, inliner):
124126
"""
125127

126128
if text == "$ref":
127-
return [nodes.reference(raw_text, text, refuri=ref_url)], []
129+
return [nodes.reference(raw_text, text, refuri=REF_URL)], []
128130
elif text == "$schema":
129-
return [nodes.reference(raw_text, text, refuri=schema_url)], []
131+
return [nodes.reference(raw_text, text, refuri=SCHEMA_URL)], []
130132

131133
# find the header in the validation spec containing matching text
132134
header = spec.xpath("//h1[contains(text(), '{0}')]".format(text))
@@ -135,15 +137,15 @@ def validator(name, raw_text, text, lineno, inliner):
135137
inliner.reporter.warning(
136138
"Didn't find a target for {0}".format(text),
137139
)
138-
uri = base_url
140+
uri = VALIDATION_SPEC
139141
else:
140142
if len(header) > 1:
141143
inliner.reporter.info(
142144
"Found multiple targets for {0}".format(text),
143145
)
144146

145147
# get the href from link in the header
146-
uri = base_url + header[0].find("a").attrib["href"]
148+
uri = urljoin(VALIDATION_SPEC, header[0].find("a").attrib["href"])
147149

148150
reference = nodes.reference(raw_text, text, refuri=uri)
149151
return [reference], []

0 commit comments

Comments
 (0)