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?

e.g,

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 001.pl

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

test_01 :-
    To = 'Fred',
    Spec = {|html(To)||<div>Say hello <span>To</Span></div>|},
    format('Spec:~n~w~n',[Spec]),
    DCG = html(Spec),
    phrase(DCG,HTML_list,[]),
    format('~nHTML list:~n~w~n',[HTML_list]),
    with_output_to(string(HTML),print_html(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').
true.

?- ['example 001'].
true.

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

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

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

true.

Note:

  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