2
2
from docutils import nodes
3
3
import errno
4
4
import os
5
- import urllib2
5
+
6
+ try :
7
+ import urllib2 as urllib
8
+ except ImportError :
9
+ import urllib .request as urllib
6
10
7
11
from lxml import html
8
12
@@ -49,11 +53,11 @@ def fetch_or_load(spec_path):
49
53
if error .errno != errno .ENOENT :
50
54
raise
51
55
52
- request = urllib2 .Request (VALIDATION_SPEC , headers = headers )
53
- response = urllib2 .urlopen (request )
56
+ request = urllib .Request (VALIDATION_SPEC , headers = headers )
57
+ response = urllib .urlopen (request )
54
58
55
59
if response .code == 200 :
56
- with open (spec_path , "w+" ) as spec :
60
+ with open (spec_path , "w+b " ) as spec :
57
61
spec .writelines (response )
58
62
spec .seek (0 )
59
63
return html .parse (spec )
@@ -72,6 +76,8 @@ def docutils_sucks(spec):
72
76
"""
73
77
74
78
base_url = VALIDATION_SPEC
79
+ ref_url = "http://json-schema.org/latest/json-schema-core.html#anchor25"
80
+ schema_url = "http://json-schema.org/latest/json-schema-core.html#anchor22"
75
81
76
82
def validator (name , raw_text , text , lineno , inliner ):
77
83
"""
@@ -88,8 +94,14 @@ def validator(name, raw_text, text, lineno, inliner):
88
94
89
95
"""
90
96
97
+ if text == "$ref" :
98
+ return [nodes .reference (raw_text , text , refuri = ref_url )], []
99
+ elif text == "$schema" :
100
+ return [nodes .reference (raw_text , text , refuri = schema_url )], []
101
+
102
+ xpath = "//h3[re:match(text(), '(^|\W)\" ?{0}\" ?($|\W,)', 'i')]"
91
103
header = spec .xpath (
92
- "//h3[re:match(text(), '(^|\W){0}($|\W,)', 'i')]" .format (text ),
104
+ xpath .format (text ),
93
105
namespaces = {"re" : "http://exslt.org/regular-expressions" },
94
106
)
95
107
@@ -100,7 +112,7 @@ def validator(name, raw_text, text, lineno, inliner):
100
112
uri = base_url
101
113
else :
102
114
if len (header ) > 1 :
103
- inliner .reporter .warning (
115
+ inliner .reporter .info (
104
116
"Found multiple targets for {0}" .format (text ),
105
117
)
106
118
uri = base_url + "#" + header [0 ].getprevious ().attrib ["name" ]
0 commit comments