/*

Generate a document (as an HTML tree for a W3C Tech Report) from a
"Doc" information structure.

Currently, we fetch the individual section text (using page_text),
although we could do that in an earlier run if we need to.  Doing
lazy-inclusion like this lets us get started on outputting the
document before we've downloaded all the content we'll need.

ACTUALLY, right now, we generate the whole HTML tree before we return,
but we could just use the +foo hook in the HTML serializer to fix
that.


This makes heavy use of html.pl approach, which is basically HTML
Templating (as seen in a million web frameworks) with a Prolog syntax.

*/

:- ensure_loaded(library(debug)).
:- ensure_loaded(semwalker(must)).

% all document data is accessed via this interface, which could go to
% RDF or whatever. 


page(Doc, H) :-
	Heading = div(class=head,
		      +w3c_icon,
			h1(id=main, class='no-num no-toc', +doc(Doc, title)),
			h2(id=main_what, class='no-num no-toc', 'W3C ',
			   +doc(Doc, className), ' ', +doc(Doc, pubDate)),
			+versions(Doc),
			+editors(Doc),
			+legal,
			hr(title='Separator for header')
		       ),
	H=html(head(title(+doc(Doc, title)), +style(Doc), +charset, +wrapstyle),
	       body(Heading, 
		    +abstract(Doc),
		    +status(Doc),
		    +toc(Doc),
		    +contents(Doc)
		   )).


:- style_check(-atom).

wrapstyle(H) :-
	H = style(type='text/css',
		  ['/* borrowed from moinmoin wikis common.css */
pre {
	border: 1pt solid #AEBDCC;
	background-color: #F3F5F7;
	padding: 5pt;
	font-family: courier, monospace;
	white-space: pre;
	/* begin css 3 or browser specific rules - do not remove!
	see: http://forums.techguy.org/archive/index.php/t-249849.html */
    white-space: pre-wrap;
    word-wrap: break-word;
    white-space: -moz-pre-wrap;
    white-space: -pre-wrap;
    white-space: -o-pre-wrap;
    /* end css 3 or browser specific rules */
}

']).
	
charset(H) :-
	H = meta('http-equiv'='Content-Type',
		 content='text/html; charset=utf-8').

	
custom_style('WD').
custom_style('LC').
custom_style('CR').
custom_style('PR').
custom_style('REC').

w3c_icon(H) :-
	H = p(a(href='http://www.w3.org/',
		img(src='http://www.w3.org/Icons/w3c_home',
		    alt='W3C', height='48', width='72'))).
	
style(Doc, H) :-
	must(doc(Doc, classAbbr, Code)),
	H = [ link(rel=stylesheet, type='text/css', href='http://www.w3.org/StyleSheets/TR/base.css'),
	      +if(custom_style(Code),
		  link(rel=stylesheet, type='text/css', href=['http://www.w3.org/StyleSheets/TR/W3C-', Code, '.css'])
		 )
	    ].

versions(Doc, H) :-
	H = dl(
	       +forall(member(pair(Prop, Label),
			      [ pair(thisVersion, 'This version:'),
				pair(latestVersion, 'Latest version:'),
				pair(previousVersion, 'Previous version:')
			      ]),
		       +if(doc(Doc, Prop, URL),
			   [ dt(Label),
			     dd(a(href=URL, URL)) ]
			  )
		      )
	      ).


	/*
	 H = dl(dt('This version:'),
	       dd(a(href='http://www.w3.org/TR/2006/WD-rdf-sparql-protocol-20060125/', 'http://www.w3.org/TR/2006/WD-rdf-sparql-protocol-20060125/')),
	       dt('Latest version:'),
	       dd(a(href='http://www.w3.org/TR/rdf-sparql-protocol/',
		    'http://www.w3.org/TR/rdf-sparql-protocol/')),
	       dt('Previous version:'),
	       dd(a(href='http://www.w3.org/TR/2005/WD-rdf-sparql-protocol-20050914/', 'http://www.w3.org/TR/2005/WD-rdf-sparql-protocol-20050914/'))
	      ).
	  */
	
editors(Doc, H) :-
	doc(Doc, editors, Editors),
	H= dl(dt('Editors:'),
	      dd(+forall(member(Editor, Editors),
			 [Editor, br([])]
			)
		)
	      ).

legal(H) :-
	H= p(class=copyright,
	     a(href='http://www.w3.org/Consortium/Legal/ipr-notice#Copyright',
	       'Copyright'),
	     ' ', raw('&copy;'), ' 2007 ',
	     a(href='http://www.w3.org/',
	       acronym(title='World Wide Web Consortium', 'W3C')),
	     sup(raw('&reg;')),
	     ' (',
	     a(href='http://www.csail.mit.edu/',
	       acronym(title='Massachusetts Institute of Technology',
		       'MIT')),
	     ', ',
	     a(href='http://www.ercim.org/', acronym(title='European Research Consortium for Informatics and Mathematics', 'ERCIM')),
	     ', ',
	     a(href='http://www.keio.ac.jp/', 'Keio'),
	     '), All Rights Reserved. W3C ',
	     a(href='http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer', liability),
	     ', ',
	     a(href='http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks', trademark),
	     ' and ',
	     a(href='http://www.w3.org/Consortium/Legal/copyright-documents', 'document use'),
	     ' rules apply.').

abstract(Doc, H) :-
	(   doc(Doc, abstract, Abstract)
	->  H=div(h2(class='no-toc no-num', a(id=abstract, name=abstract, 'Abstract')),
	      Abstract)
	;   H = []
	).

status(Doc, H) :-
	H=div(h2(class='no-toc no-num', a(id=status, name=status,
				'Status of this Document')),
	      StatusText,
	      hr(title='Separator After Status Section')
	     ),
	must(doc(Doc, commentDate, CommentDate)),
	must(doc(Doc, callForComments, CallForComments)),
	once(doc(Doc, otherStatus, OtherStatus); OtherStatus = []),
	Headings = true,
        StatusText = [
	      +if(Headings, h4(class='no-toc no-num', 'May Be Superseded')),
	      p(em('This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the ', a(href='http://www.w3.org/TR/', 'W3C technical reports index'), ' at http://www.w3.org/TR/.')),
	      +if(Headings, h4(class='no-toc no-num', 'Please Comment By ', CommentDate)),
	      p(CallForComments),
	      OtherStatus,
	      +if(Headings, h4(class='no-toc no-num', 'No Endorsement')),
	      p(em('Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.')),
	      +if(Headings, h4(class='no-toc no-num', 'Patents')),
	      p(em('This document was produced by a group operating under the ', a(href='http://www.w3.org/Consortium/Patent-Policy-20040205/', '5 February 2004 W3C Patent Policy'), '. W3C maintains a ', a(rel=disclosure, href='http://www.w3.org/2004/01/pp-impl/38457/status', 'public list of any patent disclosures'), ' made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains ', a(href='http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential', 'Essential Claim(s)'), ' must disclose the information in accordance with ', a(href='http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure', ' section 6 of the W3C Patent Policy'), '.'))
		     ].



toc(Doc, H) :-
	doc(Doc, toc, TOC),
	toc_entries(TOC, 1, [], E),
	H = div(h2(class='no-toc no-num',
		   a(id=contents, name=contents, 'Table of Contents')),
		raw('<!--begin-toc-->'),
		ul(class=toc, E),
		raw('<!--end-toc-->')).

toc_entries([], _, _, []).
toc_entries([section(Tag, Title, Subs)|Tail], Counter, Prefix, [H|TT]) :-
	H=li(class=tocline1,
	     Prefix, int(Counter), '. ',
	     a(href=['#',Tag], class=tocxref, Title),
	     SubToc),
	(   Subs = []
	->  SubToc = []
	;   toc_entries(Subs, 1, [Prefix, int(Counter), '.'], SubsH),
	    SubToc = ul(class=toc, SubsH)
	),
	CounterPlus1 is Counter + 1,
	toc_entries(Tail, CounterPlus1, Prefix, TT).

contents(Doc, H) :-
	doc(Doc, toc, TOC),
	section(Doc, TOC, 2, 1, [], H).

%%  section(+Doc, +RemainingTOC, +Depth, +Counter, +Prefix, -HTML_Out)
%%
%% 
section(_Doc, [], _, _, _, []).
section(Doc, [section(Tag, Title, Subs)|Tail], Depth, Counter, Prefix, [H|TT]) :-
	DepthPlus1 is Depth + 1,
	Shift is Depth - 1,
	debug(section, 'calling page_text on ~q', [Tag]),
	page_text(Doc, Tag, Shift, URI, Text),
	debug(section, 'page_text returned with ~q', [Text]),
	atom_number(DepthAtom, Depth),
	atom_concat('h', DepthAtom, HTag),
	Heading =.. [HTag, id=Tag,  span(class='secno', Prefix, int(Counter), '. '), Title],
	H=div(
	      Heading,
	      Note,
	      Text,
	      SubToc),
	(   must(doc(Doc, classAbbr, Class)),
	    debug(class, '~q', [Class]),
	    Class = 'ED'
	->  Note = p(style='font-size: 75%', em('(Editor\'s Note:  This text is maintained on wiki page ', a(href=URI, Title), ').'))
	;   Note = []
	),
	(   Subs = []
	->  SubToc = []
	;   SubToc = +section(Doc, Subs, DepthPlus1, 1,
			      [Prefix, int(Counter), '.'])
	),
	CounterPlus1 is Counter + 1,
	TT= +section(Doc, Tail, Depth, CounterPlus1, Prefix).

