View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2016, VU University Amsterdam
    7
    8    This program is free software; you can redistribute it and/or
    9    modify it under the terms of the GNU General Public License
   10    as published by the Free Software Foundation; either version 2
   11    of the License, or (at your option) any later version.
   12
   13    This program is distributed in the hope that it will be useful,
   14    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16    GNU General Public License for more details.
   17
   18    You should have received a copy of the GNU General Public
   19    License along with this library; if not, write to the Free Software
   20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   21
   22    As a special exception, if you link this library with other files,
   23    compiled with a Free Software compiler, to produce an executable, this
   24    library does not by itself cause the resulting executable to be covered
   25    by the GNU General Public License. This exception does not however
   26    invalidate any other reasons why the executable file might be covered by
   27    the GNU General Public License.
   28*/
   29
   30:- module(cliopatria_render_rdf,
   31	  [ term_rendering//3			% +Term, +Vars, +Options
   32	  ]).   33:- use_module(library(semweb/rdf_db)).   34:- use_module(library(http/html_write)).   35:- use_module(components(label)).   36:- use_module(library(uri)).   37:- use_module(library(lists)).   38:- use_module(library(apply)).   39:- use_module(library(swish/render)).   40
   41:- register_renderer(rdf, "Render RDF terms").   42
   43/** <module> SWISH RDF renderer
   44
   45Render RDF data. This turns URIs into  links to the ClioPatria localview
   46and prints literal values using Turtle syntax.   Lists  of RDF terms are
   47rendered as an inline block with one   element  per line. Long lists are
   48truncated to the `max_depth` option  of the `answer_write_options` flag.
   49This can be overruled using e.g.,
   50
   51    :- use_rendering(rdf, [max_length(100)]).
   52*/
   53
   54%%	term_rendering(+Term, +Vars, +Options)//
   55%
   56%	Renders Term as  a  uri  or   Turtle  syntax  literal.  List are
   57%	rendered as a list of these.
   58
   59term_rendering(List, _Vars, Options) -->
   60	{ is_list(List), List \== [],
   61	  maplist(is_rdf, List),
   62	  truncate(List, Truncated, Options)
   63	},
   64	html(span([class('rdf-list'), style('display:inline-block')],
   65		  \rdf_list(Truncated, Options))).
   66term_rendering(Term, _Vars, Options) -->
   67	{ is_rdf(Term) }, !,
   68	rdf_link(Term, [target('cliopatria-localview')|Options]).
   69
   70rdf_list([], _) --> [].
   71rdf_list([H|T], Options) -->
   72	html(div(class('rdf-list-element'),
   73		 \rdf_list_elem(H, Options))),
   74	rdf_list(T, Options).
   75
   76
   77rdf_list_elem(skipped(Skipped), _) --> !,
   78	html(span(class('rdf-list-skipped'),
   79		  '... skipped ~D ...'-[Skipped])).
   80rdf_list_elem(Elem, Options) -->
   81	rdf_link(Elem, [target('cliopatria-localview')|Options]).
   82
   83truncate(List, Truncated, Options) :-
   84	(   option(max_length(Max), Options)
   85	->  true
   86	;   current_prolog_flag(answer_write_options, WrOptions),
   87	    option(max_depth(Max), WrOptions)
   88	),
   89	length(List, Len),
   90	Len + 1 > Max,
   91	Start is Max - 1,
   92	Skipped is Len-Max,
   93	length(Prefix, Start),
   94	append(Prefix, _, List),
   95	last(List, Last),
   96	append(Prefix, [skipped(Skipped), Last], Truncated).
   97truncate(List, List, _).
   98
   99%%	is_rdf(@Term)
  100%
  101%	True if Term is an RDF term.
  102
  103is_rdf(Term) :-
  104	is_uri(Term), !.
  105is_rdf(literal(Value)) :-
  106	is_literal(Value).
  107is_rdf(^^(Value,Type)) :- atom(Type), ground(Value).
  108is_rdf(@(Text,Lang)) :- atom(Lang), is_text(Text).
  109
  110is_uri(Term) :-
  111	atom(Term),
  112	(   uri_is_global(Term)
  113	->  true
  114	;   rdf_is_bnode(Term)
  115	).
  116
  117is_literal(Atomic) :- is_plain_literal(Atomic).
  118is_literal(type(Type, Literal)) :- is_uri(Type), is_plain_literal(Literal).
  119is_literal(lang(Lang, Literal)) :- atom(Lang),   is_text(Literal).
  120
  121is_plain_literal(Value) :-
  122	atomic(Value).
  123
  124is_text(Value) :- atom(Value), !.
  125is_text(Value) :- string(Value)