From 50960c7ccbae4a4e8f4f53010543752635b4d8cd Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Mon, 9 Jun 2025 18:11:34 +0200 Subject: Implement Topnav module with proper routes. --- lib/routes.ml | 27 +++++++++++++++------------ lib/views.ml | 51 +++++++++++++++++++++++++-------------------------- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/lib/routes.ml b/lib/routes.ml index 84c2851..f615168 100644 --- a/lib/routes.ml +++ b/lib/routes.ml @@ -3,27 +3,30 @@ type t = | Root | Repo of string - | Tag of string * string + | Log of string + | Files of string + | Refs of string | Commit of string * string - | Tree of string * string - | Blob of string * string + | File of string * string let%path root_path = "/" -let%path repo_path = "/%s/" -let%path tag_path = "/%s/refs/%s" +let%path repo_path = "/%s/summary/" +let%path log_path = "/%s/log/" +let%path files_path = "/%s/files/" +let%path refs_path = "/%s/refs/" let%path commit_path = "/%s/commit/%s" -let%path tree_path = "/%s/tree/%s" -let%path blob_path = "/%s/blob/%s" +let%path file_path = "/%s/file/%s" -let link_to route contents = +let link_to route ?(other_attrs = []) contents = let open Dream_html in let open HTML in let path = function | Root -> path_attr href root_path | Repo repo -> path_attr href repo_path repo - | Tag (repo, branch) -> path_attr href tag_path repo branch + | Log repo -> path_attr href log_path repo + | Files repo -> path_attr href files_path repo + | Refs repo -> path_attr href refs_path repo | Commit (repo, commit) -> path_attr href commit_path repo commit - | Tree (repo, hash) -> path_attr href tree_path repo hash - | Blob (repo, hash) -> path_attr href blob_path repo hash + | File (repo, hash) -> path_attr href file_path repo hash in - a [ path route ] [ contents ] + a (path route :: other_attrs) [ contents ] diff --git a/lib/views.ml b/lib/views.ml index 029e311..ba87571 100644 --- a/lib/views.ml +++ b/lib/views.ml @@ -13,27 +13,26 @@ type body_data = { module Components = struct open Dream_html - let topnav repo = - let open HTML in - let li_of_a (path, text) = - let is_active = String.ends_with ~suffix:path repo in - let attrs = if is_active then [ id "active" ] else [] in - let url = Printf.sprintf "/%s/%s" repo 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"); - ]; - ] + module Topnav = struct + type t = None | Summary | Log | Files | Refs + + let v ?(active_path = None) repo = + let open HTML in + let nav_items = + [ + (Routes.Repo repo, "Summary", Summary); + (Routes.Log repo, "Log", Log); + (Routes.Files repo, "Files", Files); + (Routes.Refs repo, "Refs", Refs); + ] + in + let li_of_item (route, text, path) = + let is_active = path = active_path in + let attrs = if is_active then [ id "active" ] else [] in + HTML.li attrs [ Routes.link_to route (txt "%s" text) ] + in + nav [ id "top" ] [ ul [] @@ List.map li_of_item nav_items ] + end end module Page = struct @@ -146,7 +145,7 @@ module Repo = struct { title = repo; subtitle = repo_description repo; - topnav = Components.topnav repo; + topnav = Components.Topnav.(v ~active_path:Summary repo); content; } @@ -162,7 +161,7 @@ module Repo = struct { title = repo; subtitle = repo_description repo; - topnav = Components.topnav repo; + topnav = Components.Topnav.(v ~active_path:Refs repo); content; } @@ -178,7 +177,7 @@ module Repo = struct { title = repo; subtitle = repo_description repo; - topnav = Components.topnav repo; + topnav = Components.Topnav.(v ~active_path:Log repo); content; } @@ -195,7 +194,7 @@ module Repo = struct { title; subtitle = repo_description repo; - topnav = Components.topnav repo; + topnav = Components.Topnav.(v ~active_path:Files repo); content; } @@ -232,7 +231,7 @@ module Repo = struct { title; subtitle = repo_description repo; - topnav = Components.topnav repo; + topnav = Components.Topnav.v repo; content; } end -- cgit v1.2.3