diff options
Diffstat (limited to 'lib/views.ml')
-rw-r--r-- | lib/views.ml | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/lib/views.ml b/lib/views.ml new file mode 100644 index 0000000..64b003e --- /dev/null +++ b/lib/views.ml @@ -0,0 +1,136 @@ +type page_data = { title : string; main_content : Dream_html.node list } + +module Layout = struct + open Dream_html + open HTML + + let header = + null + [ + h1 [] [ txt "ogit" ]; + h2 [] [ txt "A mobile-friendly Git repository viewer" ]; + ] + + let topnav = + nav + [ id "top" ] + [ + ul [] + [ + li [] [ a [ href "/" ] [ txt "Home" ] ]; + li [] [ a [ href "/" ] [ txt "Refs" ] ]; + li [] [ a [ href "/" ] [ txt "Log" ] ]; + li [] [ a [ href "/" ] [ txt "Tree" ] ]; + li [] [ a [ href "/" ] [ txt "Commit" ] ]; + li [] [ a [ href "/" ] [ txt "Diff" ] ]; + ]; + ] + + let footer name = + let today = Unix.localtime (Unix.time ()) in + let year = today.Unix.tm_year + 1900 |> string_of_int in + let space = " " in + let footer_text = String.concat space [ "©"; year; name ] in + footer [] [ txt "%s" footer_text ] + + let application page = + html [] + [ + head [] [ title [] "%s" page.title ]; + link [ rel "stylesheet"; href "/static/styles.css" ]; + body [] + [ + header; + topnav; + div [ id "main" ] page.main_content; + footer "Marius PETER"; + ]; + ] +end + +module Ogit_root = struct + open Dream_html + open HTML + + let repositories_in directory = + let repositories = Sys.readdir directory |> Array.to_list + and li_of_repo repo = li [] [ a [ href "%s" repo ] [ txt "%s" repo ] ] in + div [ id "repositories" ] [ ul [] @@ List.map li_of_repo repositories ] + + let page_data = + { + title = "My repositories"; + main_content = [ repositories_in Config.git_directory ]; + } + + let render () = Layout.application page_data +end + +module Repo_root = struct + open Dream_html + open HTML + open Git + open Lwt.Infix + + let page_promise repo_name = + let repo_path = Filename.concat Config.git_directory repo_name in + + (* 1. Open the Git repository *) + let%lwt repo = + Git_unix.Store.v (Fpath.v repo_path) >>= function + | Ok repo -> Lwt.return repo + | Error _ -> Lwt.fail_with "Could not open the Git repository." + in + + (* 2. Resolve HEAD to get the latest commit hash *) + let%lwt commit_hash = + Git_unix.Store.Ref.resolve repo Reference.head >>= function + | Ok hash -> Lwt.return hash + | Error _ -> Lwt.fail_with "Failed to resolve HEAD" + in + + (* (\* 3. Read the commit *\) *) + (* let%lwt commit = *) + (* Commit.v repo commit_hash >>= function *) + (* | Ok commit -> Lwt.return commit *) + (* | Error _ -> Lwt.fail_with "Failed to read the commit." *) + (* in *) + + (* (\* 4. Read the tree from the commit *\) *) + (* let%lwt tree = *) + (* Tree.v commit.Commit.tree >>= function *) + (* | Ok tree -> Lwt.return tree *) + (* | Error _ -> Lwt.fail_with "Failed to read the tree." *) + (* in *) + + (* (\* 5. Generate HTML list items for each tree entry *\) *) + (* let entries = Tree.entries tree in *) + (* let items = *) + (* List.map *) + (* (fun entry -> *) + (* let icon = match entry.Tree.perm with `Dir -> "📁 " | _ -> "📄 " in *) + (* li [] [ a [ href "#" ] [ txt (icon ^ entry.Tree.name) ] ]) *) + (* entries *) + (* in *) + + (* 6. Assemble the page *) + let main_content = + [ + ul [] + [ li [] [ txt "%s" (commit_hash |> Digestif.SHA1.to_raw_string) ] ]; + ] + in + let page_data = { title = repo_name; main_content } in + Lwt.return (Layout.application page_data) + + let render repo_name = page_promise repo_name >>= fun page -> Lwt.return page +end + +module Repo_tree = struct + open Dream_html + + let render repo_name = + let title = repo_name and main_content = [ txt "foobar" ] in + let page_data = { title; main_content } in + Layout.application page_data +end |