Skip to content

Commit

Permalink
Merge pull request #13 from jasagredo/js/windows
Browse files Browse the repository at this point in the history
Make DocLog work also on Windows
  • Loading branch information
aarroyoc authored Dec 29, 2024
2 parents 82c2d8c + 84c8be9 commit 3d9821c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 32 deletions.
13 changes: 10 additions & 3 deletions doclog.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#!/usr/bin/env bash

DOCLOG=$(realpath $(dirname $0))
SOURCE=$(realpath $1)
OUTPUT=$(realpath $2)

case "$(uname -s)" in
MINGW*)
SOURCE=$(cygpath -w $(realpath $1) | sed -e 's/\\/\\\\/g');
OUTPUT=$(cygpath -w $(realpath $2) | sed -e 's/\\/\\\\/g');;
*)
SOURCE=$(realpath $1);
OUTPUT=$(realpath $2);;
esac

cd $DOCLOG
scryer-prolog -g 'run("'$SOURCE'", "'$OUTPUT'").' -g 'halt' main.pl
scryer-prolog -g "run(\"$SOURCE\", \"$OUTPUT\")." -g 'halt' main.pl
cd -
101 changes: 72 additions & 29 deletions main.pl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

run(SourceFolder, OutputFolder) :-
catch((
portray_clause(doclog(2, 0, 0)),
portray_color(blue, doclog(2, 0, 0)),
assertz(output_folder(OutputFolder)),
assertz(source_folder(SourceFolder)),
path_segments(SourceFolder, S1),
Expand All @@ -31,11 +31,12 @@
generate_nav_lib(NavLib),
generate_nav_learn(NavLearn),
generate_footer(Footer),
Sections = ["nav_lib"-NavLib, "nav_learn"-NavLearn, "footer"-Footer],
Sections = ["nav_lib"-NavLib, "nav_learn"-NavLearn, "footer"-Footer],
generate_page_learn(Sections),
do_copy_files,
generate_page_docs(Sections),
generate_readme(Sections),
portray_color(green, done),
halt), Error, (write(Error), nl, halt(1))).

docs_base_url(BaseURL) :-
Expand All @@ -62,16 +63,28 @@
portray_clause(copy_file(A, B)),
file_copy(A, B).

% We do some path detection which will fail unless all paths are equally
% separated. This relates a base path, a tail path and the segments of the
% union.
%
% canonicalize("C:\\mypath", "my/otherpath", ["C:", "mypath", "my", "otherpath"]).
canonicalize(Base, Path, ResSG) :-
path_segments(Base, BaseSG),
path_segments(Sep, ["", ""]),
Sep = [Separator],
replace_char('\\\\', Separator, Path, Path1),
replace_char('/', Separator, Path1, Path2),
path_segments(Path2, PathSG),
append(BaseSG, PathSG, ResSG).

generate_nav_lib(NavHtml) :-
source_folder(S1),
source_lib_folder(S2),
path_segments(S1, S3),
path_segments(S2, S4),
append(S3, S4, SFSG),
source_lib_folder(S20),
canonicalize(S1, S20, SFSG),
subnav(SFSG, ".", Nav),
member("nav"-NavHtml, Nav).

subnav(Base, Dir, ["name"-Dir, "nav"-Nav, "type"-"dir"]) :-
subnav(Base, Dir, ["name"-Dir, "nav"-Nav, "type"-"dir"]) :-
append(Base, [Dir], DirSg),
path_segments(RealDir, DirSg),
directory_exists(RealDir),
Expand All @@ -96,16 +109,15 @@
files_not_omitted_files(Base, [X|Xs], Ys) :-
source_folder(S1),
source_lib_folder(S2),
path_segments(S1, S3),
path_segments(S2, S4),
append(S3, S4, SFSG),
canonicalize(S1, S2, SFSG),
path_segments(SF, SFSG),
path_segments(Separator, ["", ""]),
findall(FullOmitFile,(
omit(Omit),
member(OmitFile, Omit),
append(SF, ['/', '.', '/'|OmitFile], FullOmitFile)
append([SF, Separator, ".", Separator, OmitFile], FullOmitFile)
), OmitFiles),
append(Base, ['/'|X], File),
append([Base, Separator, X], File),
(
member(File, OmitFiles) ->
Ys = Ys0
Expand All @@ -130,7 +142,6 @@
), Items),
render("nav.html", ["items"-Items], Text),
SubNav = ["name"-Category, "nav"-Text, "type"-"dir"].


generate_footer(Footer) :-
current_time(T),
Expand Down Expand Up @@ -174,7 +185,7 @@
readme_file(R1),
append(S2, [R1], R2),
path_segments(ReadmeFile, R2),
project_name(ProjectName),
project_name(ProjectName),
docs_base_url(BaseURL),
output_folder(OutputFolder),
path_segments(OutputFolder, OutputFolderSg),
Expand All @@ -190,11 +201,9 @@
generate_page_docs(Sections) :-
source_folder(S1),
source_lib_folder(S2),
path_segments(S1, S3),
path_segments(S2, S4),
append(S3, S4, Base),
canonicalize(S1, S2, Base),
path_segments(DocsFolder, Base),
output_folder(OutputFolder),
output_folder(OutputFolder),
make_directory_path(OutputFolder),
directory_files(DocsFolder, Files),
path_segments(OutputFolder, Output),
Expand All @@ -220,7 +229,7 @@
path_segments(Output, OutputSg),
path_segments(File, FileSg),
file_exists(File),
portray_clause(process_file(File)),
portray_color(green, process_file(File)),
open(File, read, FileStream),
read_term(FileStream, Term, []),
(
Expand All @@ -233,7 +242,6 @@
; true
),
close(FileStream).


process_file(Base0, Output0, Sections, SearchWriteStream, Dir0) :-
append(Base0, [Dir0], DirSg),
Expand All @@ -252,7 +260,6 @@
predicates_clean([X|Xs], Ys, [X|Ops]) :-
X = op(_,_,_),
predicates_clean(Xs, Ys, Ops).


append_predicates_search_index(Output, PublicPredicates, Ops, SearchWriteStream) :-
output_folder(OF),
Expand Down Expand Up @@ -281,13 +288,27 @@
escape_js(Xs0),
{ append("\\\\", Xs0, Xs) }.

replace_char(_, _, [], []).
replace_char(X, Y, [X|Xs], [Y|Ys]) :-
replace_char(X, Y, Xs, Ys).
replace_char(X, Y, [Z|Xs], [Z|Ys]) :-
X \= Z,
replace_char(X, Y, Xs, Ys).

% let's try to document every text comment we see
% Later, we'll add public predicates that have no documentation
document_file(InputFile, OutputFile, ModuleName, PublicPredicates, Ops, Sections) :-
phrase_from_file(seq(FileText), InputFile),
phrase(documented_predicates(Predicates0, Ops), FileText),
public_undocumented_predicates(Predicates0, Ops, PublicPredicates, PublicUndocumented),
portray_clause(undocumented_public_predicates(PublicUndocumented)),
( PublicUndocumented \= [],
% Only portray if there are undocumented items
portray_color(yellow, undocumented_public_predicates(PublicUndocumented))
;
green(Green),
reset(Reset),
format("~sFile fully documented.~s~n", [Green, Reset])
),
maplist(document_predicate(Ops), PublicUndocumented, Predicates1),
append(Predicates0, Predicates1, Predicates),
phrase(module_description(ModuleDescriptionMd), FileText),
Expand All @@ -297,13 +318,14 @@
docs_base_url(BaseURL),
source_folder(S1),
source_lib_folder(S2),
path_segments(S1, S3),
path_segments(S2, S4),
append(S3, S4, S5),
canonicalize(S1, S2, S5),
path_segments(SF, S5),
websource(WebSourceBase),
append(SF, ExtraFile, InputFile),
append(['/'|LibraryUse], ".pl", ExtraFile),
path_segments(Separator, ["", ""]),
append([Separator, LibraryUse, ".pl"], ExtraFile),
% The use_module directive always has to use forward slashes!
replace_char('\\', '/', LibraryUse, LibraryUse1),
append(WebSourceBase, ExtraFile, WebSource),
Vars0 = [
"project_name"-ProjectName,
Expand All @@ -312,7 +334,7 @@
"module_description"-ModuleDescriptionHtml,
"predicates"-Predicates,
"websource"-WebSource,
"library"-LibraryUse
"library"-LibraryUse1
],
append(Vars0, Sections, Vars),
render("page.html", Vars, HtmlOut),
Expand Down Expand Up @@ -370,7 +392,6 @@
predicate_string(Predicate, Ops, PredicateString),
\+ member(["predicate"-PredicateString|_], Documented),
public_undocumented_predicates(Documented, Ops, Public, Undocumented).


predicate_documentation(Predicate, Name, Description) -->
"%% ", seq(Name), "\n%", { \+ member('\n', Name) },
Expand Down Expand Up @@ -470,7 +491,7 @@

dirs_only([F|Fs], Output, FOs) :-
append(Output, [F], OutputFile),
path_segments(File, OutputFile),
path_segments(File, OutputFile),
\+ directory_exists(File),
dirs_only(Fs, Output, FOs).

Expand All @@ -491,3 +512,25 @@

string_without([], _) -->
[].

red(X) :-
chars_utf8bytes(A, [27]),
append(A, "[0;31m", X).
green(X) :-
chars_utf8bytes(A, [27]),
append(A, "[0;32m", X).
yellow(X) :-
chars_utf8bytes(A, [27]),
append(A, "[0;33m", X).
blue(X) :-
chars_utf8bytes(A, [27]),
append(A, "[0;34m", X).
reset(X) :-
chars_utf8bytes(A, [27]),
append(A, "[0m", X).

portray_color(Color, X) :-
call(Color, A), reset(B),
phrase(portray_clause_(X), S),
append(S1, "\n", S),
format("~s~s~s~n", [A,S1,B]).

0 comments on commit 3d9821c

Please sign in to comment.