summaryrefslogtreecommitdiff
path: root/lib/views.ml
blob: 2d116745df218c7c53baf773cf8796811766a70f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
Copyright 2019--2025 Marius PETER