Skip to content

Commit

Permalink
JekyllRDFSerializer with links for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Tracy Hoffmann authored and splattater committed Sep 4, 2018
1 parent 5770e1b commit eba5e17
Showing 1 changed file with 42 additions and 47 deletions.
89 changes: 42 additions & 47 deletions ShacShifter/JekyllRDFSerializer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import uuid


# example class for
# example class for Jekyll-RDF
class JekyllRDFSerializer:
"""A Serializer that writes Template for JekyllRDF"""

Expand All @@ -21,8 +21,9 @@ def __init__(self, shapes, outputfile):
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
</head><body>\n''')
self.logger.debug(shapes)
self.content.append('<div class ="container">')
self.content.append("<h1>{{ page.rdf.iri }}</h1>\n")
self.content.append('<div class ="container">\n')
self.content.append("<h2>{{ page.rdf.iri }}</h2>\n")
self.content.append("""{% assign type = page.rdf | rdf_property: "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>" %}\n""")

nodeShapes = shapes[0]
for nodeShape in nodeShapes:
Expand All @@ -46,34 +47,28 @@ def nodeShapeEvaluation(self, nodeShape, fp):
args: nodeShape a nodeShape object
fp
"""
self.logger.debug(
'This Resource needs to be in the following classes'
+ '(can be used through rdfa annotation?):'
)

if len(nodeShape.targetClass) > 1:
self.content.append("Uebersicht")

for tClass in nodeShape.targetClass:
self.content.append(
'<h1>{type}</h1><br><h3>{short}</h3><br>'.format(
'<h2>{type}</h2><br><h3>{short}</h3><br>'.format(
type=tClass, short=tClass.rsplit('/', 1)[-1]))

elif len(nodeShape.targetClass) == 1:
self.content.append("Ressource: {resource} ({type})<br>".format(
self.content.append('{{% if type.iri == "{type}" %}}\n\n'.format(
type=nodeShape.targetClass[0].lower()))
self.content.append("Ressource: <strong>{resource}</strong> ({type})<br><br>\n\n".format(
resource=nodeShape.targetClass[0].rsplit('/', 1)[-1], type=nodeShape.targetClass[0]))

self.content.append(
'{{% assign {resource} = page.rdf | rdf_property: "<{type}>" %}} <br>'.format(
type=nodeShape.targetClass[0], resource=nodeShape.targetClass[0].rsplit('/', 1)[-1]))

for nodes in nodeShape.targetNode:
self.logger.debug(nodes)
self.logger.debug(
'The following ressources need to be Objects of those predicates'
+ '(can be used through rdfa annotation?):'
)

for nodes in nodeShape.targetObjectsOf:
self.logger.debug(nodes)

Expand All @@ -83,10 +78,21 @@ def nodeShapeEvaluation(self, nodeShape, fp):
for property in nodeShape.properties:
shapeName = nodeShape.targetClass[0].rsplit('/', 1)[-1]
content = self.propertyShapeEvaluation(property, fp, shapeName)

self.content.append(content)

resource_hack = nodeShape.targetClass[0].rsplit('/', 1)[-1]
self.content.append(
'{{% assign {resource} = page.rdf | rdf_property: "<{type}/{resource}>", nil, true %}} <br>\n'.format(
type=nodeShape.targetClass[0].rsplit('/', 1)[-2], resource=resource_hack.lower()))
self.content.append('\n{{% for {instance}_instance in {resource} %}}\n<h3>{{{{ {instance}_instance.iri }}}}</h3>\n'.format(
instance=nodeShape.targetClass[0].rsplit('/', 1)[-1].lower(), resource=resource_hack.lower()))

if len(nodeShape.targetClass) == 1:
self.content.append("\n{% endfor %}\n{% endif %}\n<hr/>")

def propertyShapeEvaluation(self, propertyShape, fp, shapeName):
"""Evaluate a propertyShape and return HTML.
"""Evaluate propertyShape and return HTML.
args: propertyShape a propertyShape object
fp
Expand All @@ -102,38 +108,27 @@ def propertyShapeEvaluation(self, propertyShape, fp, shapeName):
self.logger.info('Sequence path not supported, yet')
else:
uri = propertyShape.path
label = propertyShape.name \
if propertyShape.isSet['name'] else propertyShape.path.rsplit('/', 1)[-1]
lowercase_str = uuid.uuid4().hex[:4]
label_jekyll = propertyShape.name.lower() \
if propertyShape.isSet['name'] else propertyShape.path.lower().rsplit('/', 1)[-1]
label_jekyll += "_"+ lowercase_str
label = propertyShape.name.lower() \
if propertyShape.isSet['name'] else propertyShape.path.lower().rsplit('/', 1)[-1]

html += """{{% assign {label} = page.rdf | rdf_property: "<{uri}>", nil, true %}}\n""".format(
uri=uri, label=label_jekyll, type=shapeName.lower())

html += """{{% if {label} %}}\n""".format(label=label_jekyll)

html += """<dl>"""
html += """<dt>{label}</dt>""".format(label=label)
html += """{{% for each_{label} in {label} %}}\n""".format(label=label_jekyll)
html += """{{% if each_{label}.iri %}}\n""".format(label=label_jekyll)
html += """<dd>Link: <a href={{{{each_{label}.page_url}}}}>{{{{each_{label}}}}}</a></dd>\n
{{% else %}}\n""".format(label=label_jekyll)
html += """<dd>{{{{each_{label} }}}}</dd>\n{{% endif %}}\n""".format(label=label_jekyll)

html += """\n{% endfor %}\n</dl>\n{% endif %}\n"""

if not propertyShape.isSet['minCount'] and not propertyShape.isSet['maxCount']:
html += """<dt>{label}</dt>
<dd>{{{{ {type} | rdf_property: "<{uri}>" }}}}</dd>""".format(
uri=uri, label=label, type=shapeName)
else:
html += """<dt>{label}</dt>
<dd>{{{{ {type} | rdf_property: "<{uri}>" }}}}""".format(
uri=uri, label=label, type=shapeName)
'''
if propertyShape.isSet['minCount']:
for i in range(0, propertyShape.minCount):
html += """ (min: {counter})</dd>\n""".format(counter=str(i+1))
if propertyShape.isSet['maxCount']:
for i in range(max(propertyShape.minCount, 0), propertyShape.maxCount):
html += """ (max: {counter})</dd>\n""".format(
counter=str(propertyShape.minCount + i-1)) '''
html += """</dl>"""

return html
'''
html+= '<h1>TEST:</h1>'
uri = propertyShape.path
html+= """<a class="page-link" href="{{{{ statement.object.page_url | prepend: site.baseurl }}}}">
{{{{ statement.object }}}}
{{% if statement.object.iri %}}
({{{{ statement.object | rdf_property: '{uri}' }}}})
{{% endif %}}
</a>""".format(uri=uri)'''


0 comments on commit eba5e17

Please sign in to comment.