Daniel@0: /* Part of DML (Digital Music Laboratory) Daniel@0: Copyright 2014-2015 Samer Abdallah, University of London Daniel@0: Daniel@0: This program is free software; you can redistribute it and/or Daniel@0: modify it under the terms of the GNU General Public License Daniel@0: as published by the Free Software Foundation; either version 2 Daniel@0: of the License, or (at your option) any later version. Daniel@0: Daniel@0: This program is distributed in the hope that it will be useful, Daniel@0: but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: GNU General Public License for more details. Daniel@0: Daniel@0: You should have received a copy of the GNU General Public Daniel@0: License along with this library; if not, write to the Free Software Daniel@0: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: */ Daniel@0: Daniel@0: :- module(decoration, []). Daniel@0: /** Hook for customised RDF links with extra HTML Daniel@0: */ Daniel@0: Daniel@0: Daniel@0: :- use_module(cliopatria(hooks)). Daniel@0: :- use_module(library(http/html_write)). Daniel@0: :- use_module(library(http/html_head)). Daniel@0: :- use_module(library(semweb/rdfs)). Daniel@0: :- use_module(library(dcg_core)). Daniel@0: :- use_module(library(rdfutils)). Daniel@0: :- use_module(components(label)). Daniel@0: :- use_module(components(icons)). Daniel@0: Daniel@0: Daniel@0: :- multifile resource_decoration//2, resource_view//2. Daniel@0: Daniel@0: %% resource_decoration(URI)// is nondet. Daniel@0: % This DCG rule succeeds when the resource specified by URI should be Daniel@0: % preceeded by arbitrary HTML elements. It should produce a list of HTML Daniel@0: % tokens as produced by html//1. Daniel@0: Daniel@0: cliopatria:display_link(R,_) --> Daniel@0: { rdf_equal(R,rdf:nil) }, !, html(span(title('rdf:nil'),b('[]'))). Daniel@0: Daniel@0: cliopatria:display_link(R,Opts) --> Daniel@0: { rdf_has(R,rdf:first,_), !, Daniel@0: option(max_length(Max),Opts,5), Daniel@0: cp_label:resource_link(R,ListLink), Daniel@0: rdf_list_take(Max,R,Items,Tail), Daniel@0: rdf_list_length(Tail,Rem), Daniel@0: ( Rem=0 -> length(Items,ListLength) Daniel@0: ; ListLength is Max+Rem Daniel@0: ), Daniel@0: format(string(ListTitle), 'RDF collection with ~D members', ListLength) Daniel@0: }, Daniel@0: html([ a([href(ListLink),title(ListTitle)],b('[')) Daniel@0: , \seqmap_with_sep(comma, display_item(Opts), Items) Daniel@0: , \display_tail(Rem,Tail) Daniel@0: , a([href(ListLink),title(ListTitle)],b(']')) Daniel@0: ]). Daniel@0: Daniel@0: cliopatria:display_link(R,Opts) --> Daniel@0: { rdf(R,rdf:type,owl:'Restriction'), Daniel@0: rdf(R,owl:onProperty,Prop), Daniel@0: restriction_condition(R,Cond) Daniel@0: }, !, Daniel@0: html(span(title(R),['(', \html(Cond),')',&(nbsp),\rdf_link(Prop,Opts)])). Daniel@0: Daniel@0: restriction_condition(R,['=',Num]) :- rdf_number(R,owl:cardinality,Num), !. Daniel@0: restriction_condition(R,[&(ge),Num]) :- rdf_number(R,owl:minCardinality,Num), !. Daniel@0: restriction_condition(R,[&(le),Num]) :- rdf_number(R,owl:maxCardinality,Num), !. Daniel@0: Daniel@0: cliopatria:display_link(URI,Opts) --> Daniel@0: { atom(URI), \+rdf_graph(URI), !, Daniel@0: cp_label:resource_link(URI, Target), Daniel@0: (rdf(URI,_,_) -> Class=r_def; Class=r_undef), Daniel@0: Link = a( [class(Class), href(Target), title(URI)], Daniel@0: \(cp_label:resource_label(URI, Opts))) Daniel@0: }, Daniel@0: ( {option(decoration(true),Opts,true)}, Daniel@0: resource_decoration(URI,decoration:html(Link)) -> [] Daniel@0: ; html(Link) Daniel@0: ). Daniel@0: Daniel@0: decoration:resource_decoration(URI,Link) --> Daniel@0: { rdf_has(_,foaf:page,URI) Daniel@0: ; rdf_has(_,foaf:isPrimaryTopicOf,URI) Daniel@0: % ; rdfs_individual_of(URI,foaf:'Document') Daniel@0: }, !, Daniel@0: html_requires(font_awesome), Daniel@0: html( span( [ \Link, &(nbsp), a([href(URI),target('_blank')],\icon('external-link')) ])). Daniel@0: Daniel@0: Daniel@0: display_item(Opts,Item) --> rdf_link(Item,Opts). Daniel@0: display_tail(0,_) --> !. Daniel@0: display_tail(N,Tail) --> Daniel@0: { cp_label:resource_link(Tail,Link), Daniel@0: format(string(Title),'Remaining ~D items',[N]) Daniel@0: }, Daniel@0: html([' | ',a([href(Link),title(Title)],&(hellip))]). Daniel@0: Daniel@0: comma --> html(', '). Daniel@0: Daniel@0: cliopatria:list_resource(URI,Opts) --> Daniel@0: {debug(decoration,'Checking for views for ~q...',[URI])}, Daniel@0: {findall(Head-Tail,resource_view(URI,Opts,Head,Tail),Views), Views\=[]}, !, Daniel@0: {length(Views,N), debug(decoration,'Found ~d views.',[N])}, Daniel@0: seqmap(dlist,Views), Daniel@0: cpa_browse:list_resource(URI,[raw(true)|Opts]). Daniel@0: Daniel@0: dlist(Head-Tail,Head,Tail).