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-2025, VU University Amsterdam
    7                              SWI-Prolog Solutions b.v.
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(html_quasi_quotations, [ html/4 ]).   37:- use_module(library(sgml)).   38:- use_module(library(apply)).   39:- use_module(library(error)).   40:- use_module(library(lists)).   41:- 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.
         |}
   77:- quasi_quotation_syntax(html).   78
   79html(Content, Vars, Dict, DOM) :-
   80    must_be(list, Dict),
   81    include(qq_var(Vars), Dict, QQDict),
   82    with_quasi_quotation_input(
   83        Content, In,
   84        load_html(In, DOM0,
   85                  [ max_errors(0),
   86                    syntax_errors(print),
   87                    case_preserving_attributes(true)
   88                  ])),
   89    xml_content(QQDict, DOM0, DOM).
   90
   91qq_var(Vars, _=Var) :- member(V, Vars), V == Var, !.
   92
   93xml_content(Dict, [Name], [Var]) :-
   94    atom(Name),
   95    memberchk(Name=Var, Dict),
   96    !.
   97xml_content(Dict, Content0, Content) :-
   98    maplist(xml_content_element(Dict), Content0, Content).
   99
  100xml_content_element(Dict,
  101                    element(Tag, Attrs0, Content0),
  102                    element(Tag, Attrs, Content)) :-
  103    !,
  104    maplist(xml_attribute(Dict), Attrs0, Attrs),
  105    xml_content(Dict, Content0, Content).
  106xml_content_element(_, Element, Element).
  107
  108xml_attribute(Dict, Attr=Name, Attr=Var) :-
  109    memberchk(Name=Var, Dict),
  110    !.
  111xml_attribute(_, Attr, Attr).
  112
  113
  114                /*******************************
  115                *           SANDBOX            *
  116                *******************************/
  117
  118:- multifile sandbox:safe_primitive/1.  119
  120sandbox:safe_primitive(html_quasi_quotations:html(_,_,_,_))