summaryrefslogtreecommitdiff
path: root/lib/views.ml
blob: 3c1330ef10600837e2a893962d9a2b07fc47242f (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
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" ];
            link
              [ rel "icon"; type_ "image/x-icon"; href "/static/git_icon.svg" ];
          ];
        body []
          [
            header body_data.title body_data.subtitle;
            body_data.topnav;
            div [ id "main" ] body_data.content;
            footer ();
          ];
      ]
end

module Components = struct
  open Dream_html

  let topnav repo_path current_path =
    let open HTML in
    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.equal path "/" 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");
             ];
      ]
end

let ogit_root () =
  let open Dream_html in
  let repositories_in directory =
    let repos =
      Sys.readdir directory |> Array.to_list |> List.sort String.compare
    in
    let li_of_repo repo =
      HTML.(li [] [ a [ href "%s/" repo ] [ txt "%s" repo ] ])
    in
    HTML.(div [ id "repositories" ] [ ul [] @@ List.map li_of_repo repos ])
  in
  let body_data =
    {
      title = "Ogit";
      subtitle = "Repositories for " ^ Config.author;
      topnav = HTML.(null []);
      content = [ repositories_in Config.git_directory ];
    }
  in
  Layout.application body_data

let repo_summary repo_path branches commits =
  let open Git_helpers in
  let open Dream_html in
  let li_of_branch branch =
    HTML.(li [] [ a [ href "%s" branch ] [ txt "%s" branch ] ])
  in
  let li_of_commit commit =
    match commit.message with
    | Some msg ->
        HTML.(
          li []
            [
              a
                [ href "commit/?id=%s" commit.hash ]
                [ txt "%s %s" (short_hash commit.hash) msg ];
            ])
    | None -> HTML.(li [] [ a [ href "" ] [ txt "caca!!" ] ])
  in
  let content =
    HTML.
      [
        h3 [] [ txt "Branches" ];
        ul [] (List.map li_of_branch branches);
        h3 [] [ txt "Recent commits" ];
        ul [] (List.map li_of_commit commits);
      ]
  in
  Layout.application
    {
      title = repo_path;
      subtitle = repo_description repo_path;
      topnav = Components.topnav repo_path "";
      content;
    }

let repo_commit repo_path commit =
  let open Git_helpers in
  let open Dream_html in
  let message = match commit.message with Some msg -> msg | None -> "" in
  let content = HTML.[ h3 [] [ txt "%s" message ] ] in
  let title = Printf.sprintf "%s : %s" repo_path (short_hash commit.hash) in
  Layout.application
    {
      title;
      subtitle = "";
      topnav = Components.topnav repo_path "";
      content;
    }

let error_page message =
  let open Dream_html in
  HTML.(
    html []
      [
        head []
          [
            title [] "Big error";
            link [ rel "stylesheet"; href "/static/styles.css" ];
            link
              [ rel "icon"; type_ "image/x-icon"; href "/static/git_icon.svg" ];
          ];
        body []
          [
            h1 [] [ txt "Major error alert" ];
            h2 [] [ txt "Major alert subtitle" ];
            (* Components.topnav; *)
            div [ id "main" ] [ txt "%s" message ];
          ];
      ])

let repo_tree repo_path = repo_summary repo_path
let repo_blob repo_path = repo_summary repo_path
Copyright 2019--2025 Marius PETER