From a83d819b8b9cc67d4a874b3aba8924cf8f22dbeb Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Fri, 9 Sep 2016 19:24:54 +0200 Subject: Improve bibliography rendering --- formatRefs.py | 108 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 25 deletions(-) diff --git a/formatRefs.py b/formatRefs.py index b6e0be3..4da52c6 100755 --- a/formatRefs.py +++ b/formatRefs.py @@ -4,6 +4,7 @@ from rdflib import URIRef, BNode, Literal, Graph, Namespace from rdflib.namespace import RDF, NamespaceManager from urllib.parse import urlparse import sys +from itertools import chain def first (it): try: @@ -34,44 +35,101 @@ def formatPerson (s, g, n): return familyname def formatParent (s, g, n): - parent = first (g.objects (n, s.isPartOf)) - parentname = first (g.objects (parent, s.name)) if parent else None - volume = first (g.objects (n, s.volumeNumber)) - ret = '' + ret = [] + + parentname = first (g.objects (n, s.name)) if parentname: - ret += '{}'.format (parentname) - if volume: - ret += ', volume {}'.format (volume) - issue = first (g.objects (parent, s.issueNumber)) - if issue: - ret += ', issue {}'.format (issue) - return ret + ret.append (parentname) + + volume = first (g.objects (n, s.volumeNumber)) + if volume: + ret.append ('volume {}'.format (volume)) + + issue = first (g.objects (n, s.issueNumber)) + if issue: + ret.append ('issue {}'.format (issue)) + + return ', '.join (ret) + +def relUri (base, u): + if u.startswith (base): + return u[len (base):] + else: + return u + +def hideLocalUri (base, l): + """ + Show local uris only iff no other sources are available + """ + l = list (l) + notLocal = list (filter (lambda u: relUri (base, u) == u, l)) + return notLocal or l + +def getRecursive (s, g, n, predicate): + """ + Look for predicate in n and all Things it is a part of until it is found + """ + res = g.objects (n, predicate) + if res: + yield from res + parents = g.objects (n, s.isPartOf) + for p in parents: + yield from getRecursive (s, g, p, predicate) + +def getRecursiveAll (s, g, n, predicate): + parents = list (g.objects (n, predicate)) + yield from parents + for p in parents: + yield from getRecursiveAll (s, g, p, predicate) if __name__ == '__main__': g = Graph() result = g.parse ("index.ttl", format='turtle') + rootUri = sys.argv[1] + rootNode = URIRef (rootUri) s = Namespace("https://schema.org/") - for ref in result.objects (predicate=s.citation): + for ref in result.objects (rootNode, s.citation): t = list (g.objects (ref, RDF.type)) assert len (t) == 1 t = t[0] - name = first (g.objects (ref, s.name)) - authors = humanList ([formatPerson (s, g, author) for author in g.objects (ref, s.author)]) - published = first (g.objects (ref, s.datePublished )) + # object _must_ have a name + what = first (g.objects (ref, s.name)) + + # look for people who wrote/translated/edited it + who = map (lambda a: formatPerson (s, g, a), getRecursive (s, g, ref, s.author)) + #who = chain (who, map (lambda a: formatPerson (s, g, a) + ' (ed.)', getRecursive (s, g, ref, s.editor))) + #who = chain (who, map (lambda a: formatPerson (s, g, a) + ' (trans.)', getRecursive (s, g, ref, s.translator))) + who = humanList (list (who)) + + # when was it published? + when = first (getRecursive (s, g, ref, s.datePublished)) + + # where can we find it? (print) + # print from root to ref (i.e. magazine, volume, issue) + parents = reversed (list (getRecursiveAll (s, g, ref, s.isPartOf))) + where = [formatParent (s, g, p) for p in parents] + + # where can we find it? (online) + urls = hideLocalUri (rootUri, g.objects (ref, s.url)) + urls = ['`{} <{}>`__'.format (formatDomain (url), relUri (rootUri, url)) for url in urls] + refname = urlparse (ref).fragment if not refname: refname = first (g.objects (ref, s.alternateName)) - ret = '.. [{}]'.format (refname) - if authors: - ret += ' {}:'.format (authors) - ret += ' *{}*.'.format (name) - parent = formatParent (s, g, ref) - if parent: - ret += ' {}.'.format (parent) - if published: - ret += ' {}.'.format (published) - urls = ['`{} <{}>`__'.format (formatDomain (url), url) for url in g.objects (ref, s.url)] + ret = '.. [{}] \\ '.format (refname) + + if who: + ret += ' {}:'.format (who) + + ret += ' *{}*.'.format (what) + + if where: + ret += ' {}.'.format (', '.join (where)) + + if when: + ret += ' {}.'.format (when) + if urls: ret += ' {}.'.format (', '.join (urls)) print (ret) -- cgit v1.2.3