1
1
from datetime import datetime
2
+ from urllib .parse import urljoin
2
3
import errno
3
4
import os
4
5
import urllib .request
10
11
import jsonschema
11
12
12
13
__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" )
14
19
15
20
16
21
def setup (app ):
@@ -34,7 +39,7 @@ def setup(app):
34
39
35
40
path = os .path .join (app .config .cache_path , "spec.html" )
36
41
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 ))
38
43
39
44
return dict (version = __version__ , parallel_read_safe = True )
40
45
@@ -77,18 +82,15 @@ def fetch_or_load(spec_path):
77
82
return html .parse (spec )
78
83
79
84
80
- def docutils_sucks (spec ):
85
+ def docutils_does_not_allow_using_classes (spec ):
81
86
"""
82
87
Yeah.
83
88
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.
86
92
"""
87
93
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
-
92
94
def validator (name , raw_text , text , lineno , inliner ):
93
95
"""
94
96
Link to the JSON Schema documentation for a validator.
@@ -124,9 +126,9 @@ def validator(name, raw_text, text, lineno, inliner):
124
126
"""
125
127
126
128
if text == "$ref" :
127
- return [nodes .reference (raw_text , text , refuri = ref_url )], []
129
+ return [nodes .reference (raw_text , text , refuri = REF_URL )], []
128
130
elif text == "$schema" :
129
- return [nodes .reference (raw_text , text , refuri = schema_url )], []
131
+ return [nodes .reference (raw_text , text , refuri = SCHEMA_URL )], []
130
132
131
133
# find the header in the validation spec containing matching text
132
134
header = spec .xpath ("//h1[contains(text(), '{0}')]" .format (text ))
@@ -135,15 +137,15 @@ def validator(name, raw_text, text, lineno, inliner):
135
137
inliner .reporter .warning (
136
138
"Didn't find a target for {0}" .format (text ),
137
139
)
138
- uri = base_url
140
+ uri = VALIDATION_SPEC
139
141
else :
140
142
if len (header ) > 1 :
141
143
inliner .reporter .info (
142
144
"Found multiple targets for {0}" .format (text ),
143
145
)
144
146
145
147
# 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" ])
147
149
148
150
reference = nodes .reference (raw_text , text , refuri = uri )
149
151
return [reference ], []
0 commit comments