summaryrefslogtreecommitdiff
path: root/lib/views.ml
diff options
context:
space:
mode:
authorMarius Peter <marius.peter@tutanota.com>2025-02-02 17:08:52 +0100
committerMarius Peter <marius.peter@tutanota.com>2025-02-02 17:08:52 +0100
commitaacec6588c3aebffda6c6221b02622576c85c407 (patch)
tree213da645d447ac0e82a9209412f94fa78a8bc594 /lib/views.ml
parent6e8e49ba49beeeb63e1e027fc23e8883a1185109 (diff)
Change Views render function return type.
All Views submodules' render functions now return a Dream_html node promise, rather than the node itself.
Diffstat (limited to 'lib/views.ml')
-rw-r--r--lib/views.ml100
1 files changed, 55 insertions, 45 deletions
diff --git a/lib/views.ml b/lib/views.ml
index 64b003e..b625ca6 100644
--- a/lib/views.ml
+++ b/lib/views.ml
@@ -1,30 +1,18 @@
-type page_data = { title : string; main_content : Dream_html.node list }
+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 =
- 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 header title subtitle =
+ null [ h1 [] [ txt "%s" title ]; h2 [] [ txt "%s" subtitle ] ]
let footer name =
let today = Unix.localtime (Unix.time ()) in
@@ -33,16 +21,21 @@ module Layout = struct
let footer_text = String.concat space [ "©"; year; name ] in
footer [] [ txt "%s" footer_text ]
- let application page =
+ let head_data = { page_title = "Ogit" }
+
+ let application ?(head_data = head_data) body_data =
html []
[
- head [] [ title [] "%s" page.title ];
- link [ rel "stylesheet"; href "/static/styles.css" ];
+ head []
+ [
+ title [] "%s" head_data.page_title;
+ link [ rel "stylesheet"; href "/static/styles.css" ];
+ ];
body []
[
- header;
- topnav;
- div [ id "main" ] page.main_content;
+ header body_data.title body_data.subtitle;
+ body_data.topnav;
+ div [ id "main" ] body_data.content;
footer "Marius PETER";
];
]
@@ -57,13 +50,15 @@ module Ogit_root = struct
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 =
+ let body_data =
{
title = "My repositories";
- main_content = [ repositories_in Config.git_directory ];
+ subtitle = "Repositories for " ^ Config.author;
+ topnav = null [];
+ content = [ repositories_in Config.git_directory ];
}
- let render () = Layout.application page_data
+ let render () = Lwt.return @@ Layout.application body_data
end
module Repo_root = struct
@@ -72,7 +67,22 @@ module Repo_root = struct
open Git
open Lwt.Infix
- let page_promise repo_name =
+ 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 *)
@@ -84,7 +94,7 @@ module Repo_root = struct
(* 2. Resolve HEAD to get the latest commit hash *)
let%lwt commit_hash =
- Git_unix.Store.Ref.resolve repo Reference.head >>= function
+ Git_unix.Store.Ref.resolve repo Reference.master >>= function
| Ok hash -> Lwt.return hash
| Error _ -> Lwt.fail_with "Failed to resolve HEAD"
in
@@ -114,23 +124,23 @@ module Repo_root = struct
(* in *)
(* 6. Assemble the page *)
- let main_content =
- [
- ul []
- [ li [] [ txt "%s" (commit_hash |> Digestif.SHA1.to_raw_string) ] ];
- ]
+ let content =
+ [ ul [] [ li [] [ txt "%s" (commit_hash |> Digestif.SHA1.to_hex) ] ] ]
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
+ 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 main_content = [ txt "foobar" ] in
- let page_data = { title; main_content } in
- Layout.application page_data
+ 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
Copyright 2019--2025 Marius PETER