From 0751e1ccbdd81eace7ac805bd9b43bc84e60c20c Mon Sep 17 00:00:00 2001 From: Tracy Hoffmann Date: Thu, 29 Mar 2018 18:01:34 +0200 Subject: [PATCH] add draft of jekyll-rdf serializer --- ShacShifter/JekyllRDFSerializer.py | 139 +++++++++++++++++++++++++++++ ShacShifter/ShacShifter.py | 3 + ShacShifter/__init__.py | 1 + 3 files changed, 143 insertions(+) create mode 100644 ShacShifter/JekyllRDFSerializer.py diff --git a/ShacShifter/JekyllRDFSerializer.py b/ShacShifter/JekyllRDFSerializer.py new file mode 100644 index 0000000..94f937a --- /dev/null +++ b/ShacShifter/JekyllRDFSerializer.py @@ -0,0 +1,139 @@ +import logging + + +# example class for +class JekyllRDFSerializer: + """A Serializer that writes Template for JekyllRDF""" + + logger = logging.getLogger('ShacShifter.JekyllRDFSerializer') + content = [] + outputfile = '' + + def __init__(self, shapes, outputfile): + try: + fp = open(outputfile, 'w') + self.outputfile = outputfile + fp.close + except Exception: + raise Exception('Can''t write to file {}'.format(outputfile)) + + self.content.append(''' + + \n''') + self.logger.debug(shapes) + self.content.append('
') + self.content.append("

{{ page.rdf.iri }}

\n") + + nodeShapes = shapes[0] + for nodeShape in nodeShapes: + self.nodeShapeEvaluation(nodeShapes[nodeShape], fp) + self.content.append("""
+
+ Footer
+ The data ist taken from: diggr.project +
""") + self.content.append('') + self.saveToFile() + + def saveToFile(self): + fp = open(self.outputfile, 'w') + fp.write(''.join(self.content)) + fp.close + + def nodeShapeEvaluation(self, nodeShape, fp): + """Evaluate a nodeShape. + + 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( + '

{type}


{short}


'.format( + type=tClass, short=tClass.rsplit('/', 1)[-1])) + + elif len(nodeShape.targetClass) == 1: + self.content.append("Ressource: {resource} ({type})
".format( + resource=nodeShape.targetClass[0].rsplit('/', 1)[-1], type=nodeShape.targetClass[0])) + + self.content.append( + '{{% assign {resource} = page.rdf | rdf_property: "<{type}>" %}}
'.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) + + for nodes in nodeShape.targetSubjectsOf: + self.logger.debug(nodes) + + for property in nodeShape.properties: + shapeName = nodeShape.targetClass[0].rsplit('/', 1)[-1] + content = self.propertyShapeEvaluation(property, fp, shapeName) + self.content.append(content) + + def propertyShapeEvaluation(self, propertyShape, fp, shapeName): + """Evaluate a propertyShape and return HTML. + + args: propertyShape a propertyShape object + fp + return: html string + """ + html = '' + + if isinstance(propertyShape.path, dict): + # TODO handle complex paths (inverse, oneOrMorePath ...) + self.logger.info('Complex path not supported, yet') + elif isinstance(propertyShape.path, list): + # TODO handle sequence paths + 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] + + html += """
""" + + if not propertyShape.isSet['minCount'] and not propertyShape.isSet['maxCount']: + html += """
{label}
+
{{{{ {type} | rdf_property: "<{uri}>" }}}}
""".format( + uri=uri, label=label, type=shapeName) + else: + html += """
{label}
+
{{{{ {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})
\n""".format(counter=str(i+1)) + + if propertyShape.isSet['maxCount']: + for i in range(max(propertyShape.minCount, 0), propertyShape.maxCount): + html += """ (max: {counter})\n""".format( + counter=str(propertyShape.minCount + i-1)) ''' + html += """
""" + + return html +''' + html+= '

TEST:

' + uri = propertyShape.path + html+= """ + {{{{ statement.object }}}} + {{% if statement.object.iri %}} + ({{{{ statement.object | rdf_property: '{uri}' }}}}) + {{% endif %}} + """.format(uri=uri)''' + diff --git a/ShacShifter/ShacShifter.py b/ShacShifter/ShacShifter.py index 9d3660a..9a9fdea 100644 --- a/ShacShifter/ShacShifter.py +++ b/ShacShifter/ShacShifter.py @@ -6,6 +6,7 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))) from ShacShifter.HTMLSerializer import HTMLSerializer +from ShacShifter.JekyllRDFSerializer import JekyllRDFSerializer from ShacShifter.RDFormsSerializer import RDFormsSerializer from ShacShifter.ShapeParser import ShapeParser import logging @@ -25,6 +26,8 @@ def shift(self, input, output, format): if (format == "html"): writer = HTMLSerializer(parseResult, output) + if (format == "jekyll-rdf"): + writer = JekyllRDFSerializer(parseResult, output) elif (format == "rdforms"): writer = RDFormsSerializer() else: diff --git a/ShacShifter/__init__.py b/ShacShifter/__init__.py index d2bcf8c..d6042b9 100644 --- a/ShacShifter/__init__.py +++ b/ShacShifter/__init__.py @@ -20,6 +20,7 @@ def main(args=None): parser.add_argument('-o', '--output', type=str, help="The output file") parser.add_argument('-f', '--format', type=str, choices=[ 'rdforms', + 'jekyll-rdf', 'wisski', 'html' ], help="The output format")