Is there a way to go from HTML to the Prolog representation of the HTML?

In learning to create JSON files for Cytoscape.js one trick that is of immense benefit is to use cy.json() which can export the graph in the same JSON format used at initialization. This method can be called once the page has successfully loaded.

JSON → cytoscape() → Cytoscape.js objects → cy.json() → JSON

Since the JSON created is the same as used to initialize the Cytoscape.js graph with cytoscape() it serves as both an example for the proper syntax and values needed.

Is there something like this with SWI-Prolog that can take HTML and convert it to the format useful with html//1?


SWI-Prolog representation of HTML → html//1 → HTML → ??? → SWI-Prolog representation of HTML

After seeing Jan W. response.

Here is an example.

HTML → quasiquotation → Spec → html//1 → HTML List → print_html/1 → HTML

File name: Example

:- use_module(library(http/html_write)).

test_01 :-
    To = 'Fred',
    Spec = {|html(To)||<div>Say hello <span>To</Span></div>|},
    DCG = html(Spec),
    format('~nHTML list:~n~w~n',[HTML_list]),
    format('~nHTML: ~w~n',[HTML]).

Example run.

Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.7)

?- working_directory(_,'C:/Users/Groot/Documents/HTML quasi-quotations').

?- ['example 001'].

?- test_01.
[element(div,[],[Say hello ,element(span,[],[Fred])])]

HTML list:
[nl(1),<,div,>,nl(0),Say hello ,<,span,>,Fred,</,span,>,nl(0),</,div,>,nl(1)]

<div>Say hello <span>Fred</span></div>



  1. The Quotation (second parameter of Quasiquotation) has to be ground. This is because the quotation is not converted at run time but at compile time. So if it is a variable it will not be ground at compile time as needed.
  2. If you leave the variables to the quasiquotation unbound the quasiquotation will generate but html//1 will fail.

Quasi quotations, e.g.

{|html(To)||<div>Say hello <span>To</Span></div>|}

This is part of library(http/html_write).

1 Like