View source with raw 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		 *******************************/
 name_mbox(?Name, ?MBox) is nondet
True if MBox is the URL for sending mail to Name.
   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, []).
 simple_mbox_table(+HTTPRequest)
Write a table the naive way.
   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, []).
 mbox_table(+HTTPRequest)
Write a table the proper way. This code is guaranteed to produce legal HTML and can, based on a global switch, be configured to produce XHTML or plain HTML.
   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, []).
 json_mbox_table(+HTTPRequest)
Write a table as a JSON object, leaving the rendering to a Javascript client library.
   75json_mbox_table(_Request) :-
   76	findall(json([ name=Name,
   77		       mbox=MBox
   78		     ]),
   79		name_mbox(Name, MBox), Objects),
   80	reply_json(Objects)