View source with formatted comments or as raw
    1:- module(mbox, []).    2:- use_module(library(semweb/rdf_db)).    3:- use_module(library(http/http_dispatch)).    4
    5:- rdf_register_ns(foaf, 'http://xmlns.com/foaf/0.1/').    6
    7		 /*******************************
    8		 *	      QUERIES		*
    9		 *******************************/
   10
   11%%	name_mbox(?Name, ?MBox) is nondet.
   12%
   13%	True if MBox is the URL for sending mail to Name.
   14
   15name_mbox(Name,	MBox) :-
   16	rdf(X, foaf:name, literal(Name)),
   17	rdf(X, foaf:mbox, MBox).
   18
   19
   20		 /*******************************
   21		 *	      SIMPLE		*
   22		 *******************************/
   23
   24:- http_handler('/mbox/simple', simple_mbox_table, []).   25
   26%%	simple_mbox_table(+HTTPRequest)
   27%
   28%	Write a table the naive way.
   29
   30simple_mbox_table(_Request) :-
   31	format('Content-type: text/html~n~n'),
   32	format('<table>~n'),
   33	forall(name_mbox(Name, MBox),
   34	       format('<tr><td>~w<td>~w~n', [Name, MBox])),
   35	format('</table>~n').
   36	    
   37
   38		 /*******************************
   39		 *	       GOOD		*
   40		 *******************************/
   41
   42:- use_module(library(http/html_write)).   43:- http_handler('/mbox', mbox_table, []).   44
   45%%	mbox_table(+HTTPRequest)
   46%
   47%	Write a table the proper way. This code is guaranteed to produce
   48%	legal HTML and can, based on a   global switch, be configured to
   49%	produce XHTML or plain HTML.
   50
   51mbox_table(_Request) :-
   52	findall(Name-MBox, name_mbox(Name, MBox), Pairs),
   53	reply_html_page(title('Mailboxes'),
   54			table(\mailboxes(Pairs))).
   55
   56mailboxes([]) -->
   57	[].
   58mailboxes([Name-MBox|T]) -->
   59	html(tr([td(Name), td(MBox)])),
   60	mailboxes(T).
   61
   62
   63		 /*******************************
   64		 *	       JSON		*
   65		 *******************************/
   66
   67:- use_module(library(http/http_json)).   68:- http_handler('/mbox/json', json_mbox_table, []).   69
   70%%	json_mbox_table(+HTTPRequest)
   71%
   72%	Write a table as a  JSON  object,   leaving  the  rendering to a
   73%	Javascript client library.
   74
   75json_mbox_table(_Request) :-
   76	findall(json([ name=Name,
   77		       mbox=MBox
   78		     ]),
   79		name_mbox(Name, MBox), Objects),
   80	reply_json(Objects)