def run(self):
env = self.state.document.settings.env
try:
if self.arguments and self.content:
raise self.warning('both argument and content. it is invalid')
if self.arguments:
dirname = os.path.dirname(env.doc2path(env.docname, base=None))
relpath = os.path.join(dirname, self.arguments[0])
abspath = os.path.join(env.srcdir, relpath)
if not os.access(abspath, os.R_OK):
raise self.warning('JSON Schema file not readable: %s' %
self.arguments[0])
env.note_dependency(relpath)
schema = JSONSchema.loadfromfile(abspath)
else:
schema = JSONSchema.loadfromfile(''.join(self.content))
except ValueError as exc:
raise self.error('Failed to parse JSON Schema: %s' % exc)
headers = ['Name', 'Type', 'Description', 'Validations']
widths = [1, 1, 1, 2]
tgroup = nodes.tgroup(cols=len(headers))
for width in widths:
tgroup += nodes.colspec(colwidth=width)
table = nodes.table('', tgroup)
header_row = nodes.row()
for header in headers:
entry = nodes.entry('', nodes.paragraph(text=header))
header_row += entry
tgroup += nodes.thead('', header_row)
tbody = nodes.tbody()
tgroup += tbody
for prop in schema:
row = nodes.row()
row += self.cell(prop.name)
if prop.required:
row += self.cell(prop.type + " (required)")
else:
row += self.cell(prop.type)
row += self.cell(prop.description or '')
row += self.cell('\n'.join(('* %s' % v for v in prop.validations)))
tbody += row
return [table]
评论列表
文章目录