Skip to content

Commit

Permalink
Fix checking for relative paths (#77)
Browse files Browse the repository at this point in the history
Fix checking for relative paths
  • Loading branch information
spagh-eddie committed May 1, 2022
1 parent dcbaa21 commit 51f96d6
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions sphinx-jsonschema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def get_json_data(self):
elif filename and filename.startswith('http'):
# Appears to be URL so process it as such
schema, source = self.from_url(filename)
elif os.path.exists(filename):
elif os.path.exists(self._convert_filename(filename)):
# File exists so it must be a JSON schema
schema, source = self.from_file(filename)
elif filename:
Expand Down Expand Up @@ -301,14 +301,18 @@ def from_url(self, url):
data = response.content.decode()
return data, url

def from_file(self, filename):
def _convert_filename(self, filename):
"""
Join the filename with the docs' source if it is not absolute.
"""
if os.path.isabs(filename):
return filename
# file relative to the path of the current rst file
document_source = os.path.dirname(self.state.document.current_source)
if not os.path.isabs(filename):
# file relative to the path of the current rst file
source = os.path.join(document_source, filename)
else:
source = filename
return os.path.join(document_source, filename)

def from_file(self, filename):
source = self._convert_filename(filename)
try:
with open(source, encoding=self.options.get('encoding')) as file:
data = file.read()
Expand All @@ -317,6 +321,7 @@ def from_file(self, filename):
% (self.name, source, error))

# Simplifing source path and to the document a new dependency
document_source = os.path.dirname(self.state.document.current_source)
source = utils.relative_path(document_source, source)
self.state.document.settings.record_dependencies.add(source)

Expand Down

0 comments on commit 51f96d6

Please sign in to comment.