/**
 * 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(sets)).
:- ensure_loaded(library(text/format)).
:- ensure_loaded(library(text/charsio)).

/*******************************************************************/
/* Markup Length                                                   */
/*******************************************************************/

/**
 * tag_length(A, N):
 * The predicate succeeds in N with the length of the text
 * inside the XML markup A. Given whitespace is counted but no
 * extra whitespace is added for pretty printing.
 */
% tag_length(+Atom, -Integer)
tag_length(A, N) :-
    sys_tag_split(A, L),
    foldl(sys_tag_delta, L, 0, N).

% sys_tag_delta(+Atom, +Integer, -Integer)
sys_tag_delta(A, N, N) :-
   sub_atom(A, 0, _, _, '</'), !.
sys_tag_delta(A, N, N) :-
   sub_atom(A, _, _, 0, '/>'), !.
sys_tag_delta(A, N, N) :-
   sub_atom(A, 0, _, _, '<'), !.
sys_tag_delta(A, N, M) :-
   xml_escape(B, A),
   atom_length(B, L),
   M is N+L.

/*******************************************************************/
/* Markup Output                                                   */
/*******************************************************************/

/**
 * tag(A):
 * tag(W, A):
 * The predicate emits the XML markup A. The binary
 * predicate allows specifying an ANSI terminal, a DOM
 * destination or a markup writer W.
 */
% tag(+Atom)
tag(A) :-
   current_output(W),
   tag(W, A).

% tag(+Stream, +Atom)
tag(W, A) :-
   ir_object_current(W, 'data', S),
   (ir_is_sink(S) ->
      (ir_object_current(S, 'data', H), ir_is_element(H) ->
         put_atom(S, A);
         sys_tag_split(A, L),
         maplist(sys_tag_xml(S), L));
      sys_tag_split(A, L),
      maplist(sys_tag_ascii(W), L)).

/**
 * tag_format(T, L):
 * tag_format(W, T, L):
 * The predicate emits the XML markup that results
 * from formatting the arguments L with the template T. The
 * binary predicate allows specifying an ANSI terminal, a DOM
 * destination or a markup 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).

/*******************************************************************/
/* XML Mapping                                                     */
/*******************************************************************/

% sys_tag_xml(+Stream, +Atom)
sys_tag_xml(S, A) :-
   sub_atom(A, 0, _, _, '</'), !,
   sys_tag_name(A, Y),
   (sys_html_display(Y, D) -> true; D = other),
   (D = inline -> put_atom(S, A);
    D = block -> put_atom(S, A), put_atom(S, '\n');
       sys_indent_dec(S),
       sys_indent_tab(S), put_atom(S, A), put_atom(S, '\n')).
sys_tag_xml(S, A) :-
   sub_atom(A, _, _, 0, '/>'), !,
   sys_tag_name(A, Y),
   (sys_html_display(Y, D) -> true; D = other),
   (D = inline -> put_atom(S, A);
    D = block -> put_atom(S, A), put_atom(S, '\n'), sys_indent_tab(S);
       sys_indent_tab(S), put_atom(S, A), put_atom(S, '\n')).
sys_tag_xml(S, A) :-
   sub_atom(A, 0, _, _, '<'), !,
   sys_tag_name(A, Y),
   (sys_html_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_atom(S, '\n'),
       sys_indent_inc(S)).
sys_tag_xml(S, A) :-
   put_atom(S, A).

:- multifile(sys_html_display/2).
sys_html_display('p', block).
sys_html_display('div', block).
sys_html_display('title', block).
sys_html_display('h1', block).
sys_html_display('h2', block).
sys_html_display('h3', block).
sys_html_display('th', block).
sys_html_display('td', block).
sys_html_display('br', block).
sys_html_display('li', block).
sys_html_display('dt', block).
sys_html_display('dd', block).

sys_html_display('i', inline).
sys_html_display('u', inline).
sys_html_display('span', inline).
sys_html_display('b', inline).
sys_html_display('a', inline).

/*******************************************************************/
/* Ansi Mapping                                                    */
/*******************************************************************/

% sys_tag_ascii(+Stream, +Atom)
sys_tag_ascii(W, A) :-
   sub_atom(A, 0, _, _, '</'), !,
   ir_object_current(W, last, L),
   ir_object_current(W, offset, O),
   sys_tag_name(A, Y),
   (sys_ansi_end(Y, C) -> true; C = ''),
   (sys_ansi_display(Y, D) -> true; D = other),
   (D = inline -> put_atom(W, C);
    D = block -> put_atom(W, C), put_atom(W, '\n');
       sys_indent_dec(W)),
   ir_object_set(W, offset, O),
   ir_object_set(W, last, L).
sys_tag_ascii(W, A) :-
   sub_atom(A, _, _, 0, '/>'), !,
   ir_object_current(W, last, L),
   ir_object_current(W, offset, O),
   sys_tag_name(A, Y),
   (sys_ansi_display(Y, D) -> true; D = other),
   (D = block -> put_atom(W, '\n'); true),
   ir_object_set(W, offset, O),
   ir_object_set(W, last, L).
sys_tag_ascii(W, A) :-
   sub_atom(A, 0, _, _, '<'), !,
   ir_object_current(W, last, L),
   ir_object_current(W, offset, O),
   sys_tag_name(A, Y),
   (sys_ansi_begin(Y, A, C) -> true; C = ''),
   (sys_ansi_display(Y, D) -> true; D = other),
   (D = inline -> put_atom(W, C);
    D = block -> sys_indent_dec(W), sys_indent_tab(W),
       sys_indent_inc(W), put_atom(W, C);
       sys_indent_inc(W)),
   ir_object_set(W, offset, O),
   ir_object_set(W, last, L).
sys_tag_ascii(W, A) :-
   xml_escape(B, A),
   put_atom(W, B).

:- multifile(sys_ansi_display/2).
sys_ansi_display('div', block).
sys_ansi_display('li', block).
sys_ansi_display('dt', block).
sys_ansi_display('dd', block).

sys_ansi_display('i', inline).
sys_ansi_display('u', inline).
sys_ansi_display('span', inline).
sys_ansi_display('a', inline).

% sys_ansi_begin(+Atom, +Atom, -Atom)
sys_ansi_begin(li, '<li>', ' - ').
sys_ansi_begin(dd, '<dd>', '   ').
sys_ansi_begin(i, '<i>', '\x1B\[3m').
sys_ansi_begin(u, '<u>', '\x1B\[4m').
sys_ansi_begin(div, Tag, Esc) :-
   sub_atom(Tag, 0, Len, _, '<div class="'),
   sub_atom(Tag, _, Len2, 0, '">'),
   sub_atom(Tag, Len, _, Len2, Class),
   sys_emulated(sys_color, Scheme),
   sys_ansi_style(Class, Scheme, Hex),
   format_atom('\x1b\[48;2;~d;~d;~dm', [Hex >> 16,
       (Hex >> 8) /\ 0xFF, Hex  /\ 0xFF], Esc).
sys_ansi_begin(span, Tag, Esc) :-
   sub_atom(Tag, 0, Len, _, '<span class="'),
   sub_atom(Tag, _, Len2, 0, '">'),
   sub_atom(Tag, Len, _, Len2, Class),
   sys_emulated(sys_color, Scheme),
   sys_ansi_style(Class, Scheme, Hex),
   format_atom('\x1b\[38;2;~d;~d;~dm', [Hex >> 16,
       (Hex >> 8) /\ 0xFF, Hex  /\ 0xFF], Esc).
sys_ansi_begin(a, Tag, Esc) :-
   sub_atom(Tag, 0, Len, _, '<a href="'),
   sub_atom(Tag, _, Len2, 0, '">'),
   sub_atom(Tag, Len, _, Len2, Link),
   atom_join(['\x1B\]8;;', Link, '\x1B\\\'], Esc).

% sys_ansi_end(+Atom, -Atom)
sys_ansi_end(i, '\x1B\[23m').
sys_ansi_end(u, '\x1B\[24m').
sys_ansi_end(div, '\x1B\[K\x1B\[49m').
sys_ansi_end(span, '\x1B\[39m').
sys_ansi_end(a, '\x1B\]8;;\x1B\\\').

% sys_ansi_style(+Atom, +Atom, -Integer)
sys_ansi_style('vr', dark, 0x66EE66) :- !.
sys_ansi_style('vr', _, 0x008888).
sys_ansi_style('cs', dark, 0xDD1199) :- !.
sys_ansi_style('cs', _, 0xCC0088).
sys_ansi_style('cm', dark, 0xAAAAAA) :- !.
sys_ansi_style('cm', _, 0x555555).
sys_ansi_style('kw', dark, 0x7777FF) :- !.
sys_ansi_style('kw', _, 0x000088).
sys_ansi_style('error', dark, 0x553333) :- !.
sys_ansi_style('error', _, 0xffdddd).
sys_ansi_style('warning', dark, 0x555533) :- !.
sys_ansi_style('warning', _, 0xffffdd).

/*******************************************************************/
/* Tag Split                                                       */
/*******************************************************************/

% sys_tag_split(+Atom, -List)
sys_tag_split(A, R) :-
   atom_codes(A, L),
   sys_tag_mix(R, L, []).

% sys_tag_mix(+List, -List)
sys_tag_mix([A|R]) --> [0'<], !,
   sys_tag_tag(F),
   {atom_codes(A, [0'<|F])},
   sys_tag_mix(R).
sys_tag_mix([A|R]) --> [X], !,
   sys_tag_text(F),
   {atom_codes(A, [X|F])},
   sys_tag_mix(R).
sys_tag_mix([]) --> [].

% sys_tag_tag(-List, +Stream, +List, -List)
sys_tag_tag([0'>]) --> [0'>], !.
sys_tag_tag([X|F]) --> [X], !,
   sys_tag_tag(F).
sys_tag_tag(_) -->
   {throw(error(syntax_error(missing_angle),_))}.

% sys_tag_text(-List, +List, -List)
sys_tag_text([X|F]) --> [X], {X \== 0'<}, !,
   sys_tag_text(F).
sys_tag_text([]) --> [].

/*******************************************************************/
/* Tag Name                                                        */
/*******************************************************************/

% sys_tag_name(+Atom, -Atom)
sys_tag_name(A, Y) :-
   sub_atom(A, Pos, _, _, ' '), !,
   sys_tag_left(A, Left),
   Len is Pos-Left,
   sub_atom(A, Left, Len, _, Y).
sys_tag_name(A, Y) :-
   sys_tag_left(A, Left),
   sys_tag_right(A, Right),
   sub_atom(A, Left, _, Right, 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_object_current(S, 'indent', I),
   tab(S, I).

% sys_indent_inc(+Stream)
sys_indent_inc(S) :-
   ir_object_current(S, 'indent', I),
   I2 is I+3,
   ir_object_set(S, 'indent', I2).

% sys_indent_dec(+Stream)
sys_indent_dec(S) :-
   ir_object_current(S, 'indent', I),
   I2 is I-3,
   ir_object_set(S, 'indent', I2).

/*******************************************************************/
/* Foreign Predicates                                              */
/*******************************************************************/

% ir_is_sink(O):
% defined in foreign(text/domlib)

% ir_is_element(O):
% defined in foreign(text/domlib)

% dom_output_new(S, W):
% defined in foreign(text/domlib)

% xml_escape(X, Y):
% defined in foreign(text/auxlib)

% percent_encode(X, Y):
% defined in foreign(text/auxlib)

:- ensure_loaded(foreign(text/domlib)).

/****************************************************************/
/* Error Texts                                                  */
/****************************************************************/

% strings(+Atom, +Atom, -Atom)
:- multifile(strings/3).

strings('syntax_error.missing_angle', de, 'Kein XML Tag.').

strings('syntax_error.missing_angle', '', 'Not a XML tag.').
