Skip to content

Commit

Permalink
Add URLs for validated snippets and use them for the viewers
Browse files Browse the repository at this point in the history
  • Loading branch information
avillar committed Aug 20, 2023
1 parent 3852e78 commit 442144a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
7 changes: 7 additions & 0 deletions ogc/bblocks/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ def do_postprocess(bblock: BuildingBlock) -> bool:
if test_count and test_outputs_base_url:
bblock.metadata['testOutputs'] = f"{test_outputs_base_url}{bblock.subdirs}/"

if bblock.examples:
for example in bblock.examples:
for snippet in example.get('snippets', ()):
path = snippet.pop('path', None)
if base_url and path:
snippet['url'] = f"{base_url}{path}"

print(f" > Generating documentation for {bblock.identifier}", file=sys.stderr)
doc_generator.generate_doc(bblock)
return True
Expand Down
17 changes: 11 additions & 6 deletions ogc/bblocks/templates/slate/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,34 @@ ${example['content'].replace('@@assets@@', assets_rel or '')}

%endif
% for snippet in example.get('snippets', []):
<% snippet_lang = lang_aliases.get(snippet['language'].lower(), snippet['language']) %>
<%
snippet_lang = lang_aliases.get(snippet['language'].lower(), snippet['language'])
snippet_url = snippet.get('url')
%>
```${snippet_lang}
${snippet['code']}
```
% if snippet_url:
% if snippet_lang == 'json':

<blockquote class="lang-specific ${snippet_lang}">
<p><a target="_blank" href="https://avillar.github.io/TreedocViewer/?dataParser=json&amp;data=${urllib.parse.quote_plus(snippet['code'])}">View on JSON Viewer</a></p>
<p><a target="_blank" href="https://avillar.github.io/TreedocViewer/?dataParser=json&amp;dataUrl=${urllib.parse.quote_plus(snippet_url)}">View on JSON Viewer</a></p>
</blockquote>

% elif snippet_lang in ('json-ld', 'jsonld'):

<blockquote class="lang-specific ${snippet_lang}">
<p><a target="_blank" href="https://json-ld.org/playground/#json-ld=${urllib.parse.quote_plus(snippet['code'])}">View on JSON-LD Playground</a></p>
<p><a target="_blank" href="https://json-ld.org/playground/#json-ld=${urllib.parse.quote_plus(snippet_url)}">View on JSON-LD Playground</a></p>
</blockquote>

% elif snippet_lang == 'yaml':

<blockquote class="lang-specific ${snippet_lang}">
<p><a target="_blank" href="https://avillar.github.io/TreedocViewer/?dataParser=yaml&amp;data=${urllib.parse.quote_plus(snippet['code'], safe='/!$')}">View on YAML Viewer</a></p>
<p><a target="_blank" href="https://avillar.github.io/TreedocViewer/?dataParser=yaml&amp;dataUrl=${urllib.parse.quote_plus(snippet_url)}">View on YAML Viewer</a></p>
</blockquote>

% endif
% endif

% endfor
% endfor
Expand All @@ -122,7 +127,7 @@ ${'#'} JSON Schema
${bblock.annotated_schema_contents}
```

> <a target="_blank" href="https://avillar.github.io/TreedocViewer/?dataParser=yaml&amp;data=${urllib.parse.quote_plus(bblock.annotated_schema_contents)}">View on YAML Viewer</a>
> <a target="_blank" href="https://avillar.github.io/TreedocViewer/?dataParser=yaml&amp;dataUrl=${urllib.parse.quote_plus(bblock.metadata['schema']['application/yaml'])}">View on YAML Viewer</a>
Links to the schema:

Expand All @@ -138,7 +143,7 @@ ${'#'} JSON-LD Context
${bblock.jsonld_context_contents}
```

> <a target="_blank" href="https://json-ld.org/playground/#json-ld=${urllib.parse.quote_plus(bblock.jsonld_context_contents)}">View on JSON-LD Playground</a>
> <a target="_blank" href="https://json-ld.org/playground/#json-ld=${urllib.parse.quote_plus(bblock.ldContext)}">View on JSON-LD Playground</a>
You can find the full JSON-LD context here:
<a href="${bblock.ldContext}" target="_blank">${bblock.ldContext}</a>
Expand Down
18 changes: 11 additions & 7 deletions ogc/bblocks/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ValidationReport:
def __init__(self, require_fail: bool = False):
self._errors = False
self._sections: dict[str, list[str]] = {}
self.uplifted_files: dict[str, str] = {}
self.uplifted_files: dict[str, tuple[Path, str]] = {}
self.require_fail = require_fail

def add_info(self, section, text):
Expand Down Expand Up @@ -136,8 +136,8 @@ def validate_inner():
jsonld_contents = json.dumps(jsonld_uplifted, indent=2)
with open(jsonld_fn, 'w') as f:
f.write(jsonld_contents)
report.uplifted_files['jsonld'] = jsonld_contents
report.add_info('Files', f'Output JSON-LD {jsonld_fn.name} created')
report.uplifted_files['jsonld'] = (jsonld_fn, jsonld_contents)
report.add_info('Files', f'Output JSON-LD {jsonld_fn.name} created')

elif output_filename.suffix == '.jsonld':
graph = Graph().parse(data=json_doc, format='json-ld', base=base_uri)
Expand All @@ -164,7 +164,7 @@ def validate_inner():
if graph is not None and (resource_contents or filename.suffix != '.ttl'):
ttl_fn = output_filename.with_suffix('.ttl')
graph.serialize(ttl_fn, format='ttl')
report.uplifted_files['ttl'] = graph.serialize(format='ttl')
report.uplifted_files['ttl'] = (ttl_fn, graph.serialize(format='ttl'))
if graph:
report.add_info('Files', f'Output Turtle {ttl_fn.name} created')
else:
Expand Down Expand Up @@ -374,6 +374,8 @@ def validate_test_resources(bblock: BuildingBlock,
with open(output_fn, 'w') as f:
f.write(code)

snippet['path'] = output_fn

shacl_closure = snippet.get('shacl-closure')
if shacl_closure:
shacl_closure = [bblock.files_path.joinpath(c) for c in shacl_closure]
Expand All @@ -393,17 +395,19 @@ def validate_test_resources(bblock: BuildingBlock,
schema_ref=snippet.get('schema-ref'),
shacl_closure=shacl_closure)
result = result and not example_result.failed
for file_format, file_contents in example_result.uplifted_files.items():
for file_format, file_data in example_result.uplifted_files.items():
if file_format not in snippet_langs and file_format in add_snippets_formats:
add_snippets[file_format] = file_contents
add_snippets[file_format] = file_data
test_count += 1

if add_snippets:
snippets = example.setdefault('snippets', [])
for lang, code in add_snippets.items():
for lang, file_data in add_snippets.items():
fn, code = file_data
snippets.append({
'language': lang,
'code': code,
'path': fn,
})

return result, test_count
Expand Down

0 comments on commit 442144a

Please sign in to comment.