Skip to content

Commit

Permalink
Added selectable full listings for current view (cor or cor + extensi…
Browse files Browse the repository at this point in the history
…on) and core plus all extensions (#681)

Added CSS for extension defined term links bib / auto (#684)
  • Loading branch information
RichardWallis committed Jul 30, 2015
1 parent fdd9a00 commit 42aa83c
Showing 1 changed file with 96 additions and 47 deletions.
143 changes: 96 additions & 47 deletions sdoapp.py
Expand Up @@ -58,6 +58,8 @@
ENABLE_CORS = True
ENABLE_HOSTED_EXTENSIONS = True

EXTENSION_SUFFIX = "*"

ENABLED_EXTENSIONS = [ 'admin', 'auto', 'bib' ]
ALL_LAYERS = [ 'core', 'admin', 'auto', 'bib' ]

Expand Down Expand Up @@ -92,15 +94,16 @@ def __str__(self):

class TypeHierarchyTree:

def __init__(self):
def __init__(self, prefix=""):
self.txt = ""
self.visited = {}
self.prefix = prefix

def emit(self, s):
self.txt += s + "\n"

def toHTML(self):
return '<ul>%s</ul>' % self.txt
return '%s<ul>%s</ul>' % (self.prefix, self.txt)

def toJSON(self):
return self.txt
Expand All @@ -110,14 +113,28 @@ def traverseForHTML(self, node, depth = 1, hashorslash="/", layers='core'):
"""Generate a hierarchical tree view of the types. hashorslash is used for relative link prefixing."""

# log.debug("traverseForHTML: node=%s hashorslash=%s" % ( node.id, hashorslash ))
log.info("DEPTH %s" % depth)


urlprefix = ""
home = node.getHomeLayer()

if home in ENABLED_EXTENSIONS and home != getHostExt():
urlprefix = makeUrl(home)

extclass = ""
extflag = ""
if home != "core" and home != "":
extclass = "class=\"ext-%s\"" % home
extflag = EXTENSION_SUFFIX

# we are a supertype of some kind
if len(node.GetImmediateSubtypes(layers=layers)) > 0:
if len(node.GetImmediateSubtypes(layers=layers)) > 0:

# and we haven't been here before
if node.id not in self.visited:
self.visited[node.id] = True # remember our visit
self.emit( ' %s<li class="tbranch" id="%s"><a href="%s%s">%s</a>' % (" " * 4 * depth, node.id, hashorslash, node.id, node.id) )
self.emit( ' %s<li class="tbranch" id="%s"><a %s href="%s%s%s">%s</a>%s' % (" " * 4 * depth, node.id, extclass, urlprefix, hashorslash, node.id, node.id, extflag) )
self.emit(' %s<ul>' % (" " * 4 * depth))

# handle our subtypes
Expand All @@ -126,13 +143,13 @@ def traverseForHTML(self, node, depth = 1, hashorslash="/", layers='core'):
self.emit( ' %s</ul>' % (" " * 4 * depth))
else:
# we are a supertype but we visited this type before, e.g. saw Restaurant via Place then via Organization
seen = ' <a href="#%s">*</a> ' % node.id
self.emit( ' %s<li class="tbranch" id="%s"><a href="%s%s">%s</a>%s' % (" " * 4 * depth, node.id, hashorslash, node.id, node.id, seen) )
seen = ' <a href="#%s">+</a> ' % node.id
self.emit( ' %s<li class="tbranch" id="%s"><a %s href="%s%s%s">%s</a>%s%s' % (" " * 4 * depth, node.id, extclass, urlprefix, hashorslash, node.id, node.id, extflag, seen) )

# leaf nodes
if len(node.GetImmediateSubtypes(layers=layers)) == 0:
if node.id not in self.visited:
self.emit( '%s<li class="tleaf" id="%s"><a href="%s%s">%s</a>%s' % (" " * depth, node.id, hashorslash, node.id, node.id, "" ))
self.emit( '%s<li class="tleaf" id="%s"><a %s href="%s%s%s">%s</a>%s%s' % (" " * depth, node.id, extclass, urlprefix, hashorslash, node.id, node.id, extflag, "" ))
#else:
#self.visited[node.id] = True # never...
# we tolerate "VideoGame" appearing under both Game and SoftwareApplication
Expand Down Expand Up @@ -389,19 +406,19 @@ def ml(self, node, label='', title='', prop='', hashorslash='/'):
urlprefix = ""
home = node.getHomeLayer()

if home in ENABLED_EXTENSIONS and home != host_ext:
if home in ENABLED_EXTENSIONS and home != getHostExt():
port = ""
if myport != "80":
port = ":%s" % myport
urlprefix = self.makeUrl(home)
urlprefix = makeUrl(home)

extclass = ""
extflag = ""
if home != "core" and home != "":
extclass = "class=\"ext-%s\" " % home
extflag = "*"
extflag = EXTENSION_SUFFIX

return "<a %shref=\"%s%s%s\"%s%s>%s</a>%s" % (extclass, urlprefix, hashorslash, node.id, prop, title, label, extflag)
return "<a %s href=\"%s%s%s\"%s%s>%s</a>%s" % (extclass, urlprefix, hashorslash, node.id, prop, title, label, extflag)

def makeLinksFromArray(self, nodearray, tooltip=''):
"""Make a comma separate list of links via ml() function.
Expand Down Expand Up @@ -859,7 +876,7 @@ def handleHomepage(self, node):
'myhost': myhost,
'myport': myport,
'mybasehost': mybasehost,
'host_ext': host_ext,
'host_ext': getHostExt(),
'debugging': debugging
}
page = template.render(template_values)
Expand Down Expand Up @@ -935,7 +952,7 @@ def emitExactTermPage(self, node, layers="core"):
target = node.id


s = "<p id='lli' class='layerinfo %s'><a href=\"https://github.com/schemaorg/schemaorg/wiki/ExtensionList\">extension shown</a>: %s [<a href='%s'>x</a>]</p>\n" % (ll, ll, self.makeUrl("",target))
s = "<p id='lli' class='layerinfo %s'><a href=\"https://github.com/schemaorg/schemaorg/wiki/ExtensionList\">extension shown</a>: %s [<a href='%s'>x</a>]</p>\n" % (ll, ll, makeUrl("",target))
self.write(s)

cached = self.GetCachedText(node, layers)
Expand Down Expand Up @@ -1096,10 +1113,10 @@ def setupExtensionLayerlist(self, node):
layerlist = [ "core"]

# 3. Use host_ext if set, e.g. 'bib' from bib.schema.org
if host_ext != None:
log.debug("Host: %s host_ext: %s" % ( self.request.host , host_ext ) )
log.info("Host: %s host_ext: %s" % ( self.request.host , host_ext ) )
extlist.append(host_ext)
if getHostExt() != None:
log.debug("Host: %s host_ext: %s" % ( self.request.host , getHostExt() ) )
log.info("Host: %s host_ext: %s" % ( self.request.host , getHostExt() ) )
extlist.append(getHostExt())

# Report domain-requested extensions
for x in extlist:
Expand Down Expand Up @@ -1143,17 +1160,50 @@ def handleFullHierarchyPage(self, node, layerlist='core'):
return True
else:
template = JINJA_ENVIRONMENT.get_template('full.tpl')

extlist=""
count=0
for i in layerlist:
if i != "core":
sep = ""
if count > 0:
sep = ", "
extlist += "plus '%s'%s" % (i, sep)

count += 1
local_button = ""
local_label = "<h3>Core %s extension vocabularies</h3>" % extlist
if count == 0:
local_button = "Core vocabulary"
elif count == 1:
local_button = "Core %s extension" % extlist
else:
local_button = "Core %s extensions" % extlist

uThing = Unit.GetUnit("Thing")
uDataType = Unit.GetUnit("DataType")

mainroot = TypeHierarchyTree()
mainroot = TypeHierarchyTree(local_label)
mainroot.traverseForHTML(uThing, layers=layerlist)
thing_tree = mainroot.toHTML()

fullmainroot = TypeHierarchyTree("<h3>Core plus all extension vocabularies</h3>")
fullmainroot.traverseForHTML(uThing, layers=ALL_LAYERS)
full_thing_tree = fullmainroot.toHTML()

dtroot = TypeHierarchyTree()
dtroot.traverseForHTML(uDataType, layers=layerlist)
datatype_tree = dtroot.toHTML()
page = template.render({ 'thing_tree': thing_tree, 'datatype_tree': datatype_tree })



full_button = "Core plus all extensions"

page = template.render({ 'thing_tree': thing_tree,
'full_thing_tree': full_thing_tree,
'datatype_tree': datatype_tree,
'local_button': local_button,
'full_button': full_button})

self.response.out.write( page )
log.debug("Serving fresh FullTreePage.")
Expand Down Expand Up @@ -1205,7 +1255,7 @@ def handleExactTermPage(self, node, layers='core'):
self.response.out.write("<h3>Schema.org Extensions</h3>\n<p>The term '%s' is not in the schema.org core, but is described by the following extension(s):</p>\n<ul>\n" % schema_node.id)
for x in all_terms[schema_node.id]:
x = x.replace("#","")
self.response.out.write("<li><a href='%s'>%s</a></li>" % (self.makeUrl(x,schema_node.id), x) )
self.response.out.write("<li><a href='%s'>%s</a></li>" % (makeUrl(x,schema_node.id), x) )
return True
return False

Expand Down Expand Up @@ -1427,33 +1477,7 @@ def setupHostinfo(self, node, test=""):
debugging = False
if "localhost" in hostString or "sdo-ganymede.appspot.com" in hostString:
debugging = True

def getHostExt(self):
return host_ext

def getBaseHost(self):
return mybasehost

def getHostPort(self):
return myport

def makeUrl(self,ext="",path=""):
port = ""
sub = ""
p = ""
if(self.getHostPort() != "80"):
port = ":%s" % self.getHostPort()
if ext != "core" and ext != "":
sub = "%s." % ext
if path != "":
if path.startswith("/"):
p = path
else:
p = "/%s" % path

url = "http://%s%s%s%s" % (sub,self.getBaseHost(),port,p)
return url



def get(self, node):

Expand Down Expand Up @@ -1549,6 +1573,31 @@ def get(self, node):
log.info("Error handling 404.")
return

def getHostExt():
return host_ext

def getBaseHost():
return mybasehost

def getHostPort():
return myport

def makeUrl(ext="",path=""):
port = ""
sub = ""
p = ""
if(getHostPort() != "80"):
port = ":%s" % getHostPort()
if ext != "core" and ext != "":
sub = "%s." % ext
if path != "":
if path.startswith("/"):
p = path
else:
p = "/%s" % path

url = "http://%s%s%s%s" % (sub,getBaseHost(),port,p)
return url

#log.info("STARTING UP... reading schemas.")
read_schemas(loadExtensions=ENABLE_HOSTED_EXTENSIONS)
Expand Down

0 comments on commit 42aa83c

Please sign in to comment.