Prolog "markup"

Admin User, created Feb 10. 2024
         
/**
* This file provides markup writing predicates.
*
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
:- ensure_loaded(library(util/format)).
:- multifile markup_display/2.
markup_display('p', block).
markup_display('div', block).
markup_display('title', block).
markup_display('h1', block).
markup_display('h2', block).
markup_display('h3', block).
markup_display('th', block).
markup_display('td', block).
markup_display('br', block).
markup_display('a', inline).
markup_display('span', inline).
markup_display('b', inline).
/*******************************************************************/
/* Tag Output */
/*******************************************************************/
/**
* tag(A):
* tag(W, A):
* The predicate emits the start tag, end tag or self tag A.
* The binary predicate allows specifying a DOM writer W.
*/
% tag(+Atom)
tag(A) :-
current_output(W),
tag(W, A).
% tag(+Stream, +Atom)
tag(W, A) :-
ir_flush_buffer(W),
ir_data_current(W, S),
sys_cell(S, A, W).
/**
* tag_format(T, L):
* tag_format(W, T, L):
* The predicate emits the start tag, end tag or self tag that
* results from formatting the template T with the arguments L.
* The ternary predicate allows specifying a DOM writer W.
*/
% tag_format(+Atom, +List)
tag_format(T, L) :-
current_output(W),
tag_format(W, T, L).
% tag_format(+Stream, +Atom, +List)
tag_format(W, T, L) :-
format_atom(T, L, A),
tag(W, A).
/*******************************************************************/
/* DOM Writer */
/*******************************************************************/
% sys_cell(+Stream, +Atom, +Stream)
sys_cell(S, A, W) :-
sub_atom(A, 0, _, _, '</'), !,
sys_cell_end(S, A, W).
sys_cell(S, A, _) :-
sub_atom(A, _, _, 0, '/>'), !,
sys_cell_self(S, A).
sys_cell(S, A, W) :-
sub_atom(A, 0, _, _, '<'), !,
sys_cell_begin(S, A, W).
sys_cell(S, A, _) :-
sys_cell_entity(S, A).
% sys_cell_begin(+Element, +Atom, +Stream)
sys_cell_begin(S, A, _) :- ir_is_sink(S), !,
sys_tag_name(A, Y),
(markup_display(Y, D) -> true; D = other),
(D = inline -> put_atom(S, A);
D = block -> sys_indent_tab(S), put_atom(S, A);
sys_indent_tab(S), put_atom(S, A), put_code(S, 0'\n),
sys_indent_inc(S)).
sys_cell_begin(S, A, W) :-
sys_tag_name(A, Y),
atom_split(B, '', [A, '</', Y, '>']),
dom_cell_add(S, B),
ir_object_current(S, 'lastElementChild', S2),
ir_data_set(W, S2).
% sys_cell_self(+Element, +Atom)
sys_cell_self(S, A) :- ir_is_sink(S), !,
sys_tag_name(A, Y),
(markup_display(Y, D) -> true; D = other),
(D = inline -> put_atom(S, A);
D = block -> put_atom(S, A), put_code(S, 0'\n), sys_indent_tab(S);
sys_indent_tab(S), put_atom(S, A), put_code(S, 0'\n)).
sys_cell_self(S, A) :-
dom_cell_add(S, A).
% sys_cell_end(+Element, +Atom, +Stream)
sys_cell_end(S, A, _) :- ir_is_sink(S), !,
sys_tag_name(A, Y),
(markup_display(Y, D) -> true; D = other),
(D = inline -> put_atom(S, A);
D = block -> put_atom(S, A), put_code(S, 0'\n);
sys_indent_dec(S),
sys_indent_tab(S), put_atom(S, A), put_code(S, 0'\n)).
sys_cell_end(S, _, W) :-
ir_object_current(S, 'parentElement', S2),
ir_data_set(W, S2).
% sys_cell_entity(+Element, +Atom)
sys_cell_entity(S, A) :- ir_is_sink(S), !,
put_atom(S, A).
sys_cell_entity(S, A) :-
dom_cell_add(S, A).
/*******************************************************************/
/* Tag Name */
/*******************************************************************/
% sys_tag_name(+Atom, -Atom)
sys_tag_name(A, Y) :-
sub_atom(A, P, _, _, ' '), !,
sys_tag_left(A, L),
Q is P-L,
sub_atom(A, L, Q, _, Y).
sys_tag_name(A, Y) :-
sys_tag_left(A, L),
sys_tag_right(A, R),
sub_atom(A, L, _, R, Y).
% sys_tag_left(+Atom, -Integer)
sys_tag_left(A, 2) :-
sub_atom(A, 0, _, _, '</'), !.
sys_tag_left(_, 1).
% sys_tag_right(+Atom, -Integer)
sys_tag_right(A, 2) :-
sub_atom(A, _, _, 0, '/>'), !.
sys_tag_right(_, 1).
/*******************************************************************/
/* DOM Indentation */
/*******************************************************************/
% sys_indent_tab(+Stream)
sys_indent_tab(S) :-
ir_indent_current(S, I),
tab(S, I).
% sys_indent_inc(+Stream)
sys_indent_inc(S) :-
ir_indent_current(S, I),
I2 is I+3,
ir_indent_set(S, I2).
% sys_indent_dec(+Stream)
sys_indent_dec(S) :-
ir_indent_current(S, I),
I2 is I-3,
ir_indent_set(S, I2).
/*******************************************************************/
/* DOM Access */
/*******************************************************************/
% dom_cell_current(-Element)
dom_cell_current(Elem) :-
current_output(Stream),
ir_data_current(Stream, Elem).
% dom_cell_set(+Elem)
dom_cell_set(Elem) :-
current_output(Stream),
ir_data_set(Stream, Elem).
% dom_output_new(-Stream)
dom_output_new(Stream) :-
dom_cell_current(Elem),
dom_output_new(Elem, Stream).
% dom_error_new(-Stream)
dom_error_new(Stream) :-
dom_cell_current(Elem),
dom_error_new(Elem, Stream).
/*******************************************************************/
/* Report Support */
/*******************************************************************/
% sys_html_header(+Stream, +Term)
sys_html_header(S, v(I,A,B,C)) :-
tag(S, '<html author="7">'),
tag(S, '<head>'),
tag_format(S, '<link rel="raw" title="Document" href="~a"/>', [C]),
tag(S, '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>'),
tag(S, '<title editable="nocomment">'), put_atom(S, A), tag(S, '</title>'),
tag(S, '<link href="style.css" rel="stylesheet" type="text/css"/>'),
tag(S, '</head>'),
tag(S, '<body class="showbody">'),
sys_time_atom('%Y-%m-%d %H:%M:%S', I, D),
tag_format(S, '<h1 date="~a">', [D]), put_atom(S, B), tag(S, '</h1>').
sys_html_header(S, v(A,B)) :-
tag(S, '<html>'),
tag(S, '<head>'),
tag(S, '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>'),
tag(S, '<title editable="nocomment">'), put_atom(S, A), tag(S, '</title>'),
tag(S, '<link href="style.css" rel="stylesheet" type="text/css"/>'),
tag(S, '</head>'),
tag(S, '<body class="showbody">'),
tag(S, '<h1>'), put_atom(S, B), tag(S, '</h1>').
% sys_html_footer(+Stream)
sys_html_footer(S) :-
tag(S, '</body>'),
tag(S, '</html>').
/*******************************************************************/
/* Decode Page Options */
/*******************************************************************/
% sys_page_opts(+List, +Pair, -Pair)
sys_page_opts(V, _, _) :- var(V),
throw(error(instantiation_error,_)).
sys_page_opts([X|L], I, O) :- !,
sys_page_opt(X, I, H),
sys_page_opts(L, H, O).
sys_page_opts([], H, H) :- !.
sys_page_opts(L, _, _) :-
throw(error(type_error(list,L),_)).
% sys_page_opt(+Option, +Pair, -Pair)
sys_page_opt(V, _, _) :- var(V),
throw(error(instantiation_error,_)).
sys_page_opt(title(A), v(_,Y), v(A,Y)) :- !,
sys_check_atom(A).
sys_page_opt(header(A), v(X,_), v(X,A)) :- !,
sys_check_atom(A).
sys_page_opt(O, _, _) :-
throw(error(type_error(page_option,O),_)).
/*******************************************************************/
/* Foreign Predicates */
/*******************************************************************/
% ir_is_sink(O):
% defined in foreign(misc/domlib)
% ir_data_current(W, S):
% defined in foreign(misc/domlib)
% ir_data_set(W, S):
% defined in foreign(misc/domlib)
% ir_flush_buffer(W):
% defined in foreign(misc/domlib)
% ir_indent_current(W, I):
% defined in foreign(misc/domlib)
% ir_indent_set(W, I):
% defined in foreign(misc/domlib)
% dom_cell_add(E, T):
% defined in foreign(misc/domlib)
% dom_output_new(S, W):
% defined in foreign(misc/domlib)
% dom_error_new(S, W):
% defined in foreign(misc/domlib)
:- ensure_loaded(foreign(misc/domlib)).