Prolog "walker"

Admin User, created Jan 31. 2024
         
/**
* Modern Albufeira Prolog Interpreter
*
* 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/files)).
:- ensure_loaded(library(util/format)).
:- ensure_loaded(library(misc/markup)).
:- ensure_loaded('../../player/grind/colorize.p').
:- ensure_loaded('../../playerpy/grind/colorize.p').
:- ensure_loaded('../../playerj/grind/colorize.p').
:- ensure_loaded('colorize/xml.p').
:- ensure_loaded('colorize/css.p').
:- ensure_loaded('colorize/plain.p').
:- ensure_loaded('colorize/properties.p').
:- ensure_loaded('colorize/html.p').
:- ensure_loaded('present/binary.p').
:- ensure_loaded('present/image.p').
:- ensure_loaded('present/audio.p').
:- ensure_loaded('present/archive.p').
/**
* walk_filedirs(A, B):
* walk_filedirs(A, B, O):
* The predicate succeeds. As side effect it recursively copies
* recognized artefacts of the directory A into the directory B
* and generates their web pages including directory listings.
* Already existing artefacts and web pages are silently overwritten.
* The ternary predicate allows specifying an option list.
*/
% walk_filedirs(+Atom, +Atom)
walk_filedirs(InDir, OutDir) :-
walk_filedirs(InDir, OutDir, []).
% walk_filedirs(+Atom, +Atom, +List)
walk_filedirs(InDir, OutDir, Opts) :-
ensure_directory(OutDir),
sys_walk_filedirs(InDir, OutDir),
sys_walk_package(InDir, OutDir, Opts).
% sys_walk_filedirs(+Atom, +Atom)
sys_walk_filedirs(InDir, OutDir) :-
directory_member(InDir, Name),
sys_walk_alter(Name, Name2),
sub_atom(Name2, Pos1, _, Pos2, '.'),
sub_atom(Name2, 0, Pos1, _, Base),
sub_atom(Name2, _, Pos2, 0, Ext),
atom_concat(InDir, Name, InName),
sys_walk_member(Ext, InName, OutDir, Base),
fail.
sys_walk_filedirs(InDir, OutDir) :-
directory_member(InDir, Name),
\+ sub_atom(Name, _, _, _, '.'),
\+ sys_walk_stop(Name),
atom_concat(Name, '/', Name2),
atom_concat(InDir, Name2, InName),
atom_concat(OutDir, Name2, OutName),
walk_filedirs(InName, OutName),
fail.
sys_walk_filedirs(_, _).
/*****************************************************************/
/* Walk Package */
/*****************************************************************/
/**
* sys_walk_package(A, B):
* sys_walk_package(A, B, O):
* The predicate succeeds. As side effect it generates the listing
* of the directory A into the directory B. An already existing
* listing silently overwritten. The ternary predicate allows
* specifying an option list.
*/
% sys_walk_package(+Atom, +Atom)
sys_walk_package(InDir, OutDir) :-
sys_walk_package(InDir, OutDir, []).
% sys_walk_package(+Atom, +Atom, +List)
sys_walk_package(InDir, OutDir, Opts) :-
sub_atom(InDir, 0, _, 1, InDir2),
file_base_name(InDir2, Name),
sys_walk_header('', Name, Header),
sys_page_opts(Opts, v(Name,Header), Res),
atom_concat(OutDir, 'package.html', OutName),
setup_once_cleanup(
(open(OutName, write, OutStream), dom_output_new(OutStream, DomWriter)),
sys_walk_dest(InDir, DomWriter, Res),
close(DomWriter)).
% sys_walk_dest(+Atom, +Stream, +Term)
sys_walk_dest(InDir, OutStream, Res) :-
sys_html_header(OutStream, Res),
sys_walk_listing(InDir, OutStream),
sys_html_footer(OutStream).
% sys_walk_listing(+Atom, +Stream)
sys_walk_listing(InDir, S) :-
directory_member(InDir, Name),
\+ sub_atom(Name, _, _, _, '.'),
\+ sys_walk_stop(Name),
tag(S, '<p>'),
format_atom('~c/package.html', [Name], Link),
sys_walk_namelink('', Name, Link, S),
tag(S, '<br/>'),
atom_concat(InDir, Name, InName),
sys_walk_userdate(InName, S),
tag(S, '</p>'),
fail.
sys_walk_listing(InDir, S) :-
directory_member(InDir, Name),
sys_walk_alter(Name, Name2),
sub_atom(Name2, Pos1, _, Pos2, '.'),
sub_atom(Name2, 0, Pos1, _, Base),
sub_atom(Name2, _, Pos2, 0, Ext),
tag(S, '<p>'),
format_atom('~c.html', [Base], Link),
sys_walk_namelink(Ext, Base, Link, S),
tag(S, '<br/>'),
atom_concat(InDir, Name, InName),
sys_walk_userdate(InName, S),
tag(S, '</p>'),
fail.
sys_walk_listing(_, _).
/*****************************************************************/
/* Adduce Utility */
/*****************************************************************/
% sys_walk_namelink(+Atom, +Atom, +Atom, +Stream)
sys_walk_namelink(Ext, Base, Link, S) :-
tag(S, '<b>'),
tag_format(S, '<a href="~a">', [Link]),
sys_walk_header(Ext, Base, Header),
write(S, Header),
tag(S, '</a>'),
tag(S, '</b>').
% sys_walk_userdate(+Atom, +Stream)
sys_walk_userdate(InName, S) :-
file_property(InName, last_modified(Time)),
sys_time_atom('%d/%m/%Y', Time, Date),
write(S, 'Admin User, created '),
write(S, Date).
/*****************************************************************/
/* Walk Member */
/*****************************************************************/
% sys_walk_member(+Atom, +Atom, +Atom, +Atom)
sys_walk_member(Ext, InName, OutDir, Base) :-
sys_walk_artefact(Ext, InName, OutDir, Base),
sys_walk_styled(Ext, InName, OutDir, Base).
% sys_walk_artefact(+Atom, +Atom, +Atom, +Atom)
sys_walk_artefact('p', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.p']),
copy_text(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('java', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.java']),
copy_text(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('mjs', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.mjs']),
copy_text(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('py', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.py']),
copy_text(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('xml', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.xml']),
copy_text(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('css', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.css']),
copy_text(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('txt', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.txt']),
copy_text(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('properties', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.properties']),
copy_text(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('zip', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.zip']),
copy_binary(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('png', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.png']),
copy_binary(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('svg', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.svg']),
copy_binary(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('mp3', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.mp3']),
copy_binary(InName, OutName),
copy_time(InName, OutName).
sys_walk_artefact('html', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.txt']),
copy_text(InName, OutName),
copy_time(InName, OutName).
% sys_walk_styled(+Atom, +Atom, +Atom, +Atom, +Atom)
sys_walk_styled('p', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('p', Base, Header),
format_atom('~c.p', [Base], Link),
fancy_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('java', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('java', Base, Header),
format_atom('~c.java', [Base], Link),
colorizej_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('mjs', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('mjs', Base, Header),
format_atom('~c.mjs', [Base], Link),
colorize_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('py', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('py', Base, Header),
format_atom('~c.py', [Base], Link),
colorizepy_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('xml', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('xml', Base, Header),
format_atom('~c.xml', [Base], Link),
colorizex_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('css', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('css', Base, Header),
format_atom('~c.css', [Base], Link),
colorizec_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('txt', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('txt', Base, Header),
format_atom('~c.txt', [Base], Link),
colorizep_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('properties', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('properties', Base, Header),
format_atom('~c.properties', [Base], Link),
colorizepr_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('zip', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('zip', Base, Header),
format_atom('~c.zip', [Base], Link),
presentz_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('png', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('png', Base, Header),
format_atom('~c.png', [Base], Link),
presenti_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('svg', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('svg', Base, Header),
format_atom('~c.svg', [Base], Link),
presenti_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('mp3', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('mp3', Base, Header),
format_atom('~c.mp3', [Base], Link),
presenta_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
sys_walk_styled('html', InName, OutDir, Base) :-
atom_split(OutName, '', [OutDir, Base, '.html']),
file_property(InName, last_modified(Time)),
sys_walk_header('html', Base, Header),
format_atom('~c.txt', [Base], Link),
colorizeh_file(InName, OutName, [date(Time),
title(Base),header(Header),orig(Link)]),
set_file_property(OutName, last_modified(Time)).
/*****************************************************************/
/* Walk Utility */
/*****************************************************************/
% sys_walk_header(+Atom, +Atom, -Atom)
sys_walk_header(Ext, Base, Header) :-
sys_walk_lang(Ext, Lang), !,
atom_split(Header, '', [Lang,' "',Base,'"']).
sys_walk_header(Ext, Base, Header) :-
write('Name='), write(Base), write('.'), write(Ext), nl,
atom_split(Header, '', ['Unrecognized "',Base,'"']).
% sys_walk_lang(+Atom, -Atom)
sys_walk_lang('p', 'Prolog').
sys_walk_lang('java', 'Java').
sys_walk_lang('mjs', 'JavaScript').
sys_walk_lang('py', 'Python').
sys_walk_lang('xml', 'Extensible Markup').
sys_walk_lang('css', 'Cascading Styles').
sys_walk_lang('txt', 'Plain Text').
sys_walk_lang('properties', 'Properties').
sys_walk_lang('zip', 'Archive').
sys_walk_lang('png', 'Image').
sys_walk_lang('svg', 'Image').
sys_walk_lang('mp3', 'Audio').
sys_walk_lang('html', 'Hypertext Markup').
sys_walk_lang('', 'Directory').
% sys_walk_stop(-Atom)
sys_walk_stop('__pycache__').
sys_walk_stop('lib').
sys_walk_stop('ant').
sys_walk_stop('classes').
% sys_walk_alter(+Atom, -Atom)
sys_walk_alter('package.html', R) :- !, R = 'index.html'.
sys_walk_alter(R, R).