type head_data = { page_title : string } type body_data = { title : string; subtitle : string; topnav : Dream_html.node; content : Dream_html.node list; } module Layout = struct open Dream_html open HTML let header title subtitle = null [ h1 [] [ txt "%s" title ]; h2 [] [ txt "%s" subtitle ] ] 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 head_data = { page_title = "Ogit" } let application ?(head_data = head_data) body_data = html [] [ head [] [ title [] "%s" head_data.page_title; link [ rel "stylesheet"; href "/static/styles.css" ]; ]; body [] [ header body_data.title body_data.subtitle; body_data.topnav; div [ id "main" ] body_data.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 body_data = { title = "My repositories"; subtitle = "Repositories for " ^ Config.author; topnav = null []; content = [ repositories_in Config.git_directory ]; } let render () = Lwt.return @@ Layout.application body_data end module Repo_root = struct open Dream_html open HTML open Git open Lwt.Infix 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 render 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.master >>= 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 content = [ ul [] [ li [] [ txt "%s" (commit_hash |> Digestif.SHA1.to_hex) ] ] ] in let title = repo_name in let subtitle = Git_unix.(repo |> Store.root |> Fpath.to_string) in let body_data = { title; subtitle; topnav; content } in Lwt.return @@ Layout.application body_data end module Repo_tree = struct open Dream_html open HTML let render repo_name = let title = repo_name and content = [ txt "foobar" ] in let subtitle = "Dinglefops" in let topnav = null [] in let body_data = { title; subtitle; topnav; content } in Lwt.return @@ Layout.application body_data end