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 sytax 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 -> quasi-quotation -> 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 can not be a lone variable.
  2. If you leave the variables to the quasi-quotation unbound the quasi-quotation 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