[OCaml] Mobile-friendly clone of cgit.
Restructure Views library.
lib/views/dune
@@ -0,0 +1,4 @@
1
Added:
(library
2
Added:
(name views)
3
Added:
(public_name ogit.views)
4
Added:
(libraries dream-html git))
lib/views/layouts.ml
@@ -0,0 +1,45 @@
1
Added:
open Dream_html
2
Added:
open HTML
3
Added:
4
Added:
let header =
5
Added:
div []
6
Added:
[
7
Added:
h1 [] [ txt "ogit" ];
8
Added:
h2 [] [ txt "A mobile-friendly Git repository viewer" ];
9
Added:
]
10
Added:
11
Added:
let topnav =
12
Added:
nav
13
Added:
[ id "top" ]
14
Added:
[
15
Added:
ul []
16
Added:
[
17
Added:
li [] [ a [ href "/" ] [ txt "Home" ] ];
18
Added:
li [] [ a [ href "/" ] [ txt "Refs" ] ];
19
Added:
li [] [ a [ href "/" ] [ txt "Log" ] ];
20
Added:
li [] [ a [ href "/" ] [ txt "Tree" ] ];
21
Added:
li [] [ a [ href "/" ] [ txt "Commit" ] ];
22
Added:
li [] [ a [ href "/" ] [ txt "Diff" ] ];
23
Added:
];
24
Added:
]
25
Added:
26
Added:
let footer name =
27
Added:
let today = Unix.localtime (Unix.time ()) in
28
Added:
let year = string_of_int (today.Unix.tm_year + 1900) in
29
Added:
let space = " " in
30
Added:
let footer_text = String.concat space [ "©"; year; name ] in
31
Added:
footer [] [ txt "%s" footer_text ]
32
Added:
33
Added:
let application ?(page_title = "ogit") ?(head_content = null []) ~main_content
34
Added:
() =
35
Added:
html []
36
Added:
[
37
Added:
head [] [ title [] "%s" page_title; head_content ];
38
Added:
body []
39
Added:
[
40
Added:
header;
41
Added:
topnav;
42
Added:
div [ id "main" ] [ main_content ];
43
Added:
footer "Marius PETER";
44
Added:
];
45
Added:
]
lib/views/ogit_root.ml
@@ -0,0 +1,18 @@
1
Added:
open Dream_html
2
Added:
open HTML
3
Added:
4
Added:
let git_directory = Filename.concat (Unix.getenv "HOME") "git"
5
Added:
6
Added:
let repositories_in dir =
7
Added:
let links =
8
Added:
(* if Sys.file_exists dir && Sys.is_directory dir then *)
9
Added:
let repositories = Sys.readdir dir |> Array.to_list
10
Added:
and repo_to_li repo = li [] [ a [ href "%s" repo ] [ txt "%s" repo ] ] in
11
Added:
let repositories_as_items = List.map repo_to_li repositories in
12
Added:
ul [] repositories_as_items
13
Added:
(* else txt "No repositories found in %s" dir *)
14
Added:
in
15
Added:
div [ id "repositories" ] [ links ]
16
Added:
17
Added:
let main_content = div [] [ txt "Hello World!"; repositories_in git_directory ]
18
Added:
let render = Layouts.application ~page_title:"My repositories" ~main_content
lib/views/repo.ml
@@ -0,0 +1,38 @@
1
Added:
open Dream_html
2
Added:
open HTML
3
Added:
open Git_unix (* Import Git_unix for working with repositories *)
4
Added:
5
Added:
let git_directory = Filename.concat (Unix.getenv "HOME") "git"
6
Added:
let repo_path repo_name = Filename.concat git_directory repo_name
7
Added:
8
Added:
let list_commits repo_name =
9
Added:
let repo_dir = repo_path repo_name in
10
Added:
if Sys.file_exists repo_dir && Sys.is_directory repo_dir then
11
Added:
let store = Store.v (Fpath.v repo_dir) |> Result.get_ok in
12
Added:
let head = Store.Head.get store |> Result.get_ok in
13
Added:
let rec read_commits commits acc =
14
Added:
match commits with
15
Added:
| [] -> acc
16
Added:
| hash :: rest ->
17
Added:
let commit = Store.read_commit store hash |> Result.get_ok in
18
Added:
let hash_str = Hash.to_hex hash in
19
Added:
let message = Commit.message commit in
20
Added:
let author = Commit.author commit in
21
Added:
let date = Commit.date commit in
22
Added:
let formatted_commit =
23
Added:
li []
24
Added:
[
25
Added:
txt (Printf.sprintf "%s - %s (%s)" hash_str message author);
26
Added:
txt (" on " ^ date);
27
Added:
]
28
Added:
in
29
Added:
read_commits rest (formatted_commit :: acc)
30
Added:
in
31
Added:
let all_commits = Store.Commit.list store head in
32
Added:
ul [] (read_commits all_commits [])
33
Added:
else ul [] [ li [] [ txt "Error: Repository not found." ] ]
34
Added:
35
Added:
(* Render the repository's commit list as HTML *)
36
Added:
let render repo_name =
37
Added:
let commits = list_commits repo_name in
38
Added:
Layouts.application ~page_title:("Commits for " ^ repo_name) commits