commit d2043caf3424e14cd7bce98f40867b48768a1b41 Author: zapashcanon Date: Fri Jul 9 12:04:06 2021 +0200 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e35d885 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +_build diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..c2e4660 --- /dev/null +++ b/dune-project @@ -0,0 +1 @@ +(lang dune 2.8) diff --git a/src/any.ml b/src/any.ml new file mode 100644 index 0000000..002aed7 --- /dev/null +++ b/src/any.ml @@ -0,0 +1,13 @@ +let gen in_path out_path = + begin match Bos.OS.Path.must_exist in_path with + | Ok in_path -> + begin match Bos.OS.File.must_exist in_path with + | Ok in_path -> File.gen in_path out_path + | Error _e -> + begin match Bos.OS.Dir.must_exist in_path with + | Ok in_path -> Directory.gen in_path out_path + | Error _e -> Utils.error (Format.sprintf "path `%s` isn't a directory or a file, don't know what to do with it" (Fpath.to_string in_path)) + end; + end; + | Error _e -> Utils.error (Format.sprintf "path `%s` doesn't exist" (Fpath.to_string in_path)) + end diff --git a/src/directory.ml b/src/directory.ml new file mode 100644 index 0000000..d664f7c --- /dev/null +++ b/src/directory.ml @@ -0,0 +1,44 @@ +let rec gen in_path out_path = + match Bos.OS.Dir.must_exist in_path with + | Ok in_path -> + begin match Bos.OS.Dir.contents ~dotfiles:true ~rel:true in_path with + | Ok content -> + begin match Bos.OS.Dir.create ~path:true ~mode:0o755 out_path with + | Ok true -> + let index_f, index_d = List.fold_left (fun (index_f, index_d) filename -> + let in_path = Fpath.append in_path filename in + match Bos.OS.Dir.must_exist in_path with + | Ok path -> + gen in_path (Fpath.append out_path path); + index_f, ((filename, path)::index_d) + | Error _e -> + begin match Bos.OS.File.must_exist in_path with + | Ok in_path -> + let file_path = Fpath.v (Fpath.to_string filename ^ ".html") in + File.gen in_path (Fpath.append out_path file_path); + ((filename, file_path)::index_f), index_d + | Error _e -> Utils.error (Format.sprintf "can't gen `%s`" (Fpath.to_string in_path)) + end + ) ([], []) + content + in + let index_f = List.map (fun (filename, path) -> + Format.sprintf {|
  • %s
  • |} (Fpath.to_string path) (Fpath.to_string filename) + ) index_f in + let index_d = List.map (fun (filename, path) -> + Format.sprintf {|
  • %s
  • |} (Fpath.to_string path) (Fpath.to_string filename) + ) index_d in + let pp_list = Format.pp_print_list ~pp_sep:(fun fmt () -> Format.fprintf fmt "") Format.pp_print_string in + let content = Format.asprintf "" pp_list index_d pp_list index_f in + let content = Format.asprintf "%a" Html.pp content in + let index_path = Fpath.append out_path (Fpath.v "index.html") in + begin match Bos.OS.File.write index_path content with + | Ok () -> () + | Error _e -> Utils.error (Format.sprintf "can't write to file `%s`" (Fpath.to_string index_path)) + end + | Error _ | Ok false -> Utils.error (Format.sprintf "can't create directory `%s` or it already exists" (Fpath.to_string out_path)) + end; + | Error _e -> Utils.error (Format.sprintf "can't get content of directory `%s`" (Fpath.to_string in_path)) + + end + | Error _e -> Utils.error (Format.sprintf "directory `%s` doesn't exist" (Fpath.to_string in_path)) diff --git a/src/dune b/src/dune new file mode 100644 index 0000000..083b0e9 --- /dev/null +++ b/src/dune @@ -0,0 +1,9 @@ +(library + (name hize) + (modules any directory file html utils) + (libraries bos fpath)) + +(executable + (name hize) + (modules hize) + (libraries hize bos fpath)) diff --git a/src/file.ml b/src/file.ml new file mode 100644 index 0000000..d694fac --- /dev/null +++ b/src/file.ml @@ -0,0 +1,13 @@ +let gen in_path out_path = + match Bos.OS.File.must_exist in_path with + | Ok in_path -> + begin match Bos.OS.File.read in_path with + | Ok s -> + let content = Format.asprintf "%a" Html.pp s in + begin match Bos.OS.File.write out_path content with + | Ok () -> () + | Error _e -> Utils.error (Format.sprintf "can't write to file `%s`" (Fpath.to_string out_path)) + end + | Error _e -> Utils.error (Format.sprintf "can't read file `%s`" (Fpath.to_string in_path)) + end + | Error _e -> Utils.error (Format.sprintf "file `%s` doesn't exist" (Fpath.to_string in_path)) diff --git a/src/hize.ml b/src/hize.ml new file mode 100644 index 0000000..1a3975e --- /dev/null +++ b/src/hize.ml @@ -0,0 +1,11 @@ +open Hize + +let () = + if Array.length Sys.argv <> 3 then Utils.error (Format.sprintf "usage: %s " Sys.argv.(0)); + + let in_path = Fpath.(v Sys.argv.(1)) in + let out_path = Fpath.(v Sys.argv.(2)) in + + Any.gen in_path out_path; + + Format.printf "done !@." diff --git a/src/html.ml b/src/html.ml new file mode 100644 index 0000000..3c0fe1f --- /dev/null +++ b/src/html.ml @@ -0,0 +1,14 @@ +let pp fmt content = + Format.fprintf fmt {| + + + + + Title + + +
    %s
    + +|} content diff --git a/src/utils.ml b/src/utils.ml new file mode 100644 index 0000000..b7dbc75 --- /dev/null +++ b/src/utils.ml @@ -0,0 +1,3 @@ +let error e = + Format.eprintf "error: %s@." e; + exit 1