% This LaTeX document was generated using the LaTeX backend of PlDoc, % The SWI-Prolog documentation system \subsubsection{Repositioning HTML for CSS and javascript links} \label{sec:html-post} Modern HTML commonly uses CSS and Javascript. This requires $<$link$>$ elements in the HTML $<$head$>$ element or $<$script$>$ elements in the $<$body$>$. Unfortunately this seriously harms re-using HTML DCG rules as components as each of these components may rely on their own style sheets or JavaScript code. We added a `mailing' system to reposition and collect fragments of HTML. This is implemented by \dcgref{html_post}{2}, \dcgref{html_receive}{1} and \dcgref{html_receive}{2}. \begin{description} \dcg[det]{html_post}{2}{+Id, :HTML} Reposition \arg{HTML} to the receiving \arg{Id}. The \dcgref{html_post}{2} call processes \arg{HTML} using \dcgref{html}{1}. Embedded \bsl{}-commands are executed by \predref{mailman}{1} from \predref{print_html}{1} or \predref{html_print_length}{2}. These commands are called in the calling context of the \dcgref{html_post}{2} call. A typical usage scenario is to get required CSS links in the document head in a reusable fashion. First, we define \dcgref{css}{1} as: \begin{code} css(URL) --> html_post(css, link([ type('text/css'), rel('stylesheet'), href(URL) ])). \end{code} Next we insert the \textit{unique} CSS links, in the pagehead using the following call to \predref{reply_html_page}{2}: \begin{code} reply_html_page([ title(...), \html_receive(css) ], ...) \end{code} \dcg[det]{html_receive}{1}{+Id} Receive posted HTML tokens. Unique sequences of tokens posted with \dcgref{html_post}{2} are inserted at the location where \dcgref{html_receive}{1} appears. \begin{tags} \mtag{See also}- The local predicate \dcgref{sorted_html}{1} handles the output of \dcgref{html_receive}{1}. \\- \dcgref{html_receive}{2} allows for post-processing the posted material. \end{tags} \dcg[det]{html_receive}{2}{+Id, :Handler} This extended version of \dcgref{html_receive}{1} causes \arg{Handler} to be called to process all messages posted to the channal at the time output is generated. \arg{Handler} is called as below, where \arg{PostedTerms} is a list of Module:Term created from calls to \dcgref{html_post}{2}. Module is the context module of html_post and Term is the unmodified term. Members in \arg{PostedTerms} are in the order posted and may contain duplicates. \begin{code} phrase(Handler, PostedTerms, HtmlTerms, Rest) \end{code} Typically, \arg{Handler} collects the posted terms, creating a term suitable for \dcgref{html}{1} and finally calls \dcgref{html}{1}. \end{description} The library predefines the receiver channel \const{head} at the end of the \const{head} element for all pages that write the html \const{head} through this library. The following code can be used anywhere inside an HTML generating rule to demand a javascript in the header: \begin{code} js_script(URL) --> html_post(head, script([ src(URL), type('text/javascript') ], [])). \end{code} This mechanism is also exploited to add XML namespace (\const{xmlns}) declarations to the (outer) \const{html} element using \dcgref{xhml_ns}{2}: \begin{description} \dcg{xhtml_ns}{2}{+Id, +Value} Demand an xmlns:id=\arg{Value} in the outer html tag. This uses the \predref{html_post}{2} mechanism to post to the \const{xmlns} channel. Rdfa (\url{http://www.w3.org/2006/07/SWD/RDFa/syntax/)}, embedding RDF in (x)html provides a typical usage scenario where we want to publish the required namespaces in the header. We can define: \begin{code} rdf_ns(Id) --> { rdf_global_id(Id:'', Value) }, xhtml_ns(Id, Value). \end{code} After which we can use \dcgref{rdf_ns}{1} as a normal rule in \dcgref{html}{1} to publish namespaces from \file{library(semweb/rdf_db)}. Note that this macro only has effect if the dialect is set to \const{xhtml}. In \const{html} mode it is silently ignored. The required \const{xmlns} receiver is installed by \dcgref{html_begin}{1} using the \const{html} tag and thus is present in any document that opens the outer \const{html} environment through this library. \end{description}