View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2013-2015, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(html_quasi_quotations, [ html/4 ]).   36:- use_module(library(sgml)).   37:- use_module(library(apply)).   38:- use_module(library(error)).   39:- use_module(library(lists)).   40:- use_module(library(quasi_quotations)).

HTML quasi quotations

This module implements quasi quotations for HTML. Together with library(http/html_write), this allows for inclusion of long HTML fragments in the Prolog source code while replacing attributes and content with variable that come from the surrounding Prolog clause.

This module is included and re-exported from library(http/html_write).

See also
- library(http/js_write) provides quasi quotation for JavaScript. */
 html(+Content, +Vars, +VarDict, -DOM) is det
The predicate html/4 implements HTML quasi quotations. These quotations produce a DOM term that is suitable for html//1 and other predicates that are declared to consume this format. The quasi quoter only accepts valid, but possibly partial HTML documents. The document must begin with a tag. The quoter replaces attributes or content whose value is a Prolog variable that appears in the argument list of the html indicator. If the variable defines content, it must be the only content. Here is an example, replacing both a content element and an attribute. Note that the document is valid HTML.
  html({|html(Name, URL)||
         <p>Dear <span class="name">Name</span>,

         <p>You can <a href="URL">download</a> the requested
         article now.
         |}
   76:- quasi_quotation_syntax(html).   77
   78html(Content, Vars, Dict, DOM) :-
   79    must_be(list, Dict),
   80    include(qq_var(Vars), Dict, QQDict),
   81    with_quasi_quotation_input(
   82        Content, In,
   83        load_html(In, DOM0,
   84                  [ max_errors(0),
   85                    syntax_errors(print),
   86                    case_preserving_attributes(true)
   87                  ])),
   88    xml_content(QQDict, DOM0, DOM).
   89
   90qq_var(Vars, _=Var) :- member(V, Vars), V == Var, !.
   91
   92xml_content(Dict, [Name], [Var]) :-
   93    atom(Name),
   94    memberchk(Name=Var, Dict),
   95    !.
   96xml_content(Dict, Content0, Content) :-
   97    maplist(xml_content_element(Dict), Content0, Content).
   98
   99xml_content_element(Dict,
  100                    element(Tag, Attrs0, Content0),
  101                    element(Tag, Attrs, Content)) :-
  102    !,
  103    maplist(xml_attribute(Dict), Attrs0, Attrs),
  104    xml_content(Dict, Content0, Content).
  105xml_content_element(_, Element, Element).
  106
  107xml_attribute(Dict, Attr=Name, Attr=Var) :-
  108    memberchk(Name=Var, Dict),
  109    !.
  110xml_attribute(_, Attr, Attr)