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 = let today = Unix.localtime (Unix.time ()) in let year = string_of_int (today.Unix.tm_year + 1900) in let footer_text = Printf.sprintf "Copyright %s %s" year Config.author in footer [] [ txt "%s" footer_text ] let default_head_data = { page_title = "Ogit" } let application ?(head_data = default_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; ]; ] end module Topnav = struct open Dream_html open HTML let repo repo_path current_path = let li_of_a (path, text) = let is_active = path = current_path in let attrs = if is_active then [ id "active" ] else [] in let url = if String.length path = 0 then Printf.sprintf "/%s" repo_path else Printf.sprintf "/%s/%s" repo_path path in li attrs [ a [ href "%s" url ] [ txt text ] ] in nav [ id "top" ] [ ul [] @@ List.map li_of_a [ ("", "summary"); ("refs", "refs"); ("log", "log"); ("tree", "tree"); ("commit", "commit"); ("diff", "diff"); ]; ] (* let li_of_a (path, text) = *) (* let is_active = path = current_path in *) (* li (if is_active then [ id "active" ] else []) in *) (* let link_of_path = *) (* if String.length path = 0 then Printf.sprintf "/%s" repo_path *) (* else Printf.sprintf "/%s/%s" repo_path path *) (* in *) (* li [ id "active" ] [ a [ href "%s" link_of_path ] [ txt text ] ] *) (* in *) (* nav *) (* [ id "top" ] *) (* [ *) (* ul [] *) (* @@ List.map li_of_a *) (* [ *) (* ("", "summary"); *) (* ("refs", "refs"); *) (* ("log", "log"); *) (* ("tree", "tree"); *) (* ("commit", "commit"); *) (* ("diff", "diff"); *) (* ]; *) (* ] *) end module Ogit_root = struct open Dream_html open HTML let repositories_in directory = try let repos = Sys.readdir directory |> Array.to_list |> List.sort String.compare in let li_of_repo repo = li [] [ a [ href "%s" repo ] [ txt "%s" repo ] ] in div [ id "repositories" ] [ ul [] @@ List.map li_of_repo repos ] with Sys_error _ -> div [] [ txt "Error: Unable to read repository list." ] let body_data = { title = "Ogit"; subtitle = "Repositories for " ^ Config.author; topnav = null []; content = [ repositories_in Config.git_directory ]; } let render () = Layout.application body_data end module Repo_root = struct open Dream_html open HTML (* let href_for_git_object kind identifier = *) (* let open Printf in *) (* href "%s" @@ *) (* match kind with *) (* | `Branch -> sprintf "/tree/%s" identifier *) (* | `Commit -> sprintf "/commit/%s" identifier *) let render repo_path = let title = repo_path in let subtitle = Filename.concat Config.git_directory repo_path in let all_branches = Git_unhelpers.get_all_branches repo_path in let li_of_branch branch = li [] [ a [ href "%s" branch ] [ txt "%s" branch ] ] in let recent_commits = Git_unhelpers.get_latest_commits repo_path 10 in let li_of_commit commit = li [] [ a [ href "%s" commit ] [ txt "%s" commit ] ] in let content = [ h3 [] [ txt "Branches" ]; ul [] (List.map li_of_branch all_branches); h3 [] [ txt "Recent commits" ]; ul [] (List.map li_of_commit recent_commits); ] in let body_data = { title; subtitle; topnav = Topnav.repo repo_path ""; content } in Layout.application body_data end module Repo_tree = struct open Dream_html open HTML let full_path repo_path = Filename.concat Config.git_directory repo_path (* Helper function to list top-level files and directories *) let ls_repo repo_path = try Sys.readdir (full_path repo_path) |> Array.to_list |> List.filter (fun name -> name <> ".git") (* Exclude .git *) |> List.sort String.compare with Sys_error _ -> [] (* Function to create a link based on file type *) let link_for_entry repo_path entry = let entry_path = Filename.concat (full_path repo_path) entry in if Sys.is_directory entry_path then a [ href "%s" @@ Printf.sprintf "/%s/tree/%s" repo_path entry ] [ txt "%s" (entry ^ "/") ] else a [ href "%s" @@ Printf.sprintf "/%s/blob/%s" repo_path entry ] [ txt "%s" entry ] let li_of_entry repo_path entry = li [] [ link_for_entry repo_path entry ] (* Render function *) let render repo_path = let title = repo_path in let subtitle = "Files" in let repo_entries = ls_repo repo_path in let content = [ ul [] (List.map (li_of_entry repo_path) repo_entries) ] in let body_data = { title; subtitle; topnav = Topnav.repo repo_path "tree"; content } in Layout.application body_data end