From fcd7052a159ebdff11b66831a1e0498f3fba431c Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 30 Mar 2012 23:23:33 +0000 Subject: Automatically generate links to properties and instances as well as Classes. Validate after linkification, not before. --- lv2specgen/lv2specgen.py | 80 ++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 34 deletions(-) (limited to 'lv2specgen') diff --git a/lv2specgen/lv2specgen.py b/lv2specgen/lv2specgen.py index 4a1c9e4..704f09a 100755 --- a/lv2specgen/lv2specgen.py +++ b/lv2specgen/lv2specgen.py @@ -241,33 +241,10 @@ def linkify(string): return rgx.sub(translateCodeLink, string) -def getComment(m, urinode, classlist): +def getComment(m, urinode, classlist, proplist, instalist): c = findOne(m, urinode, lv2.documentation, None) if c: markup = getLiteralString(getObject(c)) - if have_lxml: - try: - # Parse and validate documentation as XHTML Basic 1.1 - doc = """ - - - - Validation Skeleton Document - - -%s - - -""" % str(markup.decode()) - - oldcwd = os.getcwd() - os.chdir(specgendir) - parser = etree.XMLParser(dtd_validation=True, no_network=True) - root = etree.fromstring(doc, parser) - os.chdir(oldcwd) - except Exception as e: - print("Invalid lv2:documentation for %s\n%s" % (urinode, e)) # Syntax highlight all C code if have_pygments: @@ -300,19 +277,54 @@ def getComment(m, urinode, classlist): # Add links to code documentation for identifiers markup = linkify(markup) - # Replace ext:ClassName with links to appropriate fragment + # Replace ext:ClassName with link to appropriate fragment rgx = re.compile(spec_pre + ':([A-Z][a-zA-Z0-9_-]*)') - def translateClassLink(match): curie = match.group(0) name = curie[curie.find(':') + 1:] uri = spec_ns + name if rdflib.URIRef(uri) not in classlist: print("warning: Link to undefined class %s\n" % curie) - return '%s' % (name, curie) + markup = rgx.sub(translateClassLink, markup) + + # Replace ext:instanceName with link to appropriate fragment + rgx = re.compile(spec_pre + ':([a-z][a-zA-Z0-9_-]*)') + def translateInstanceLink(match): + curie = match.group(0) + name = curie[curie.find(':') + 1:] + uri = spec_ns + name + if (rdflib.URIRef(uri) not in instalist and + rdflib.URIRef(uri) not in proplist): + print("warning: Link to undefined instance/property %s\n" % curie) + return '%s' % (name, curie) + markup = rgx.sub(translateInstanceLink, markup) + + if have_lxml: + try: + # Parse and validate documentation as XHTML Basic 1.1 + doc = """ + + + + Validation Skeleton Document + + +%s + + +""" % str(markup.decode()) + + oldcwd = os.getcwd() + os.chdir(specgendir) + parser = etree.XMLParser(dtd_validation=True, no_network=True) + root = etree.fromstring(doc, parser) + os.chdir(oldcwd) + except Exception as e: + print("Invalid lv2:documentation for %s\n%s" % (urinode, e)) - return rgx.sub(translateClassLink, markup) + return markup c = findOne(m, urinode, rdfs.comment, None) if c: @@ -619,7 +631,7 @@ def owlInfo(term, m): return res -def docTerms(category, list, m, classlist): +def docTerms(category, list, m, classlist, proplist, instalist): """ A wrapper class for listing all the terms in a specific class (either Properties, or Classes. Category is 'Property' or 'Class', list is a @@ -648,7 +660,7 @@ def docTerms(category, list, m, classlist): doc += """
\n

%s %s

\n""" % (t, term_uri, category, getAnchor(str(term_uri)), curie) label = getLabel(m, term) - comment = getComment(m, term, classlist) + comment = getComment(m, term, classlist, proplist, instalist) doc += '
' if label != '' or comment != '': @@ -1105,10 +1117,10 @@ def specgen(specloc, indir, style_uri, docdir, tags, instances=False, mode="spec azlist = buildIndex(m, classlist, proplist, instalist) # Generate Term HTML - termlist = docTerms('Property', proplist, m, classlist) - termlist = docTerms('Class', classlist, m, classlist) + termlist + termlist = docTerms('Property', proplist, m, classlist, proplist, instalist) + termlist = docTerms('Class', classlist, m, classlist, proplist, instalist) + termlist if instances: - termlist += docTerms('Instance', instalist, m, classlist) + termlist += docTerms('Instance', instalist, m, classlist, proplist, instalist) template = template.replace('@NAME@', specProperty(m, spec, doap.name)) template = template.replace('@SUBTITLE@', specProperty(m, spec, doap.shortdesc)) @@ -1181,7 +1193,7 @@ def specgen(specloc, indir, style_uri, docdir, tags, instances=False, mode="spec template = template.replace('@FILES@', files) - comment = getComment(m, rdflib.URIRef(spec_url), classlist) + comment = getComment(m, rdflib.URIRef(spec_url), classlist, proplist, instalist) if comment != '': template = template.replace('@COMMENT@', comment) else: -- cgit v1.2.1