1 | -- pages/blob.lua |
2 | -- File blob page builder |
3 |
|
4 | -- Copyright (c) 2020 Joshua 'joshuas3' Stockin |
5 | -- <https://joshstock.in> |
6 |
|
7 | local utils = require("utils/utils") |
8 | local git = require("git/git_commands") |
9 |
|
10 | local builder = require("utils/builder") |
11 | local tabulate = require("utils/tabulate") |
12 | local nav = require("utils/nav") |
13 |
|
14 | local _M = function(repo, repo_dir, branch, file_path) |
15 |
|
16 | -- Pre checks |
17 | if file_path ~= "" then -- make sure path exists |
18 | local path_tree = git.list_tree(repo_dir, branch.name, file_path) |
19 | if #path_tree.files == 0 then -- no path found |
20 | return nil |
21 | end |
22 | else |
23 | return nil |
24 | end |
25 |
|
26 | local build = builder:new() |
27 |
|
28 | -- Breadcrumb navigation and repository description |
29 | local breadcrumb_nav = { |
30 | {string.format("/%s", repo.name), repo.name}, |
31 | {string.format("/%s/tree/%s", repo.name, branch.name), branch.name}, |
32 | } |
33 | build:add("<h2>"..nav(breadcrumb_nav, " / ").."</h2>") |
34 | build:add("<p>"..repo.description.."</p>") |
35 |
|
36 | -- Navigation links |
37 | local navlinks = { |
38 | {string.format("/%s/download", repo.name), "Download"}, |
39 | {string.format("/%s/refs", repo.name), "Refs"}, |
40 | {string.format("/%s/log/%s", repo.name, branch.name), "Commit Log"}, |
41 | {string.format("/%s/tree/%s", repo.name, branch.name), "<b>Files</b>"} |
42 | } |
43 |
|
44 | for _, special in pairs(repo.specialfiles) do -- create nav items for special files |
45 | local split = string.split(special, " ") |
46 | table.insert(navlinks, { |
47 | string.format("/%s/blob/%s/%s", repo.name, branch.name, split[2]), |
48 | split[1] |
49 | }) |
50 | end |
51 |
|
52 | build:add([[<div class="nav">]]) |
53 | build:add(nav(navlinks)) |
54 | build:add("</div>") |
55 |
|
56 | -- Latest Commit table |
57 | build:add("<h3>Latest Commit</h3>") |
58 |
|
59 | local commit = git.log(repo_dir, branch.name, file_path, 1, 0, true)[1] |
60 |
|
61 | local commits_table_data = {} |
62 | commits_table_data.class = "log" |
63 | commits_table_data.headers = { |
64 | {"count", [[<span class="q" title="Commit number/count">{#}</span>]]}, |
65 | {"timestamp", "Time"}, |
66 | {"shorthash", "Hash"}, |
67 | {"subject", "Subject"}, |
68 | {"author", "Author"}, |
69 | {"changed_files", [[<span class="q" title="# of files changed">#</span>]]}, |
70 | {"changed_plus", [[<span class="q" title="Insertions">(+)</span>]]}, |
71 | {"changed_minus", [[<span class="q" title="Deletions">(-)</span>]]}, |
72 | {"gpggood", [[<span class="q" title="GPG signature status |
73 | |
74 | G: Good (valid) signature |
75 | B: Bad signature |
76 | U: Good signature with unknown validity |
77 | X: Good signature that has expired |
78 | Y: Good signature made by an expired key |
79 | R: Good signature made by a revoked key |
80 | E: Signature can't be checked (e.g. missing key) |
81 | N: No signature">GPG?</span>]]} |
82 | } |
83 | commits_table_data.rows = {} |
84 |
|
85 | table.insert(commits_table_data.rows, { |
86 | git.count(repo_dir, commit.hash), |
87 | utils.iso8601(commit.timestamp), |
88 | string.format([[<a href="/%s/commit/%s">%s</a>]], repo.name, commit.hash, commit.shorthash), |
89 | utils.html_sanitize(commit.subject), |
90 | string.format([[<a href="mailto:%s">%s</a>]], commit.email, utils.html_sanitize(commit.author)), |
91 | commit.diff.num, |
92 | commit.diff.plus, |
93 | commit.diff.minus, |
94 | commit.gpggood |
95 | }) |
96 |
|
97 | build:add(tabulate(commits_table_data)) |
98 |
|
99 | -- Tree breadcrumb |
100 | build:add("<h3>Blob @ ") |
101 |
|
102 | local treelinks = { |
103 | {string.format("/%s/tree/%s", repo.name, branch.name), repo.name} |
104 | } |
105 |
|
106 | local base_path = treelinks[1][1] -- /repo/tree/branch |
107 |
|
108 | local path_string = "" |
109 | local path_split = string.split(file_path, "/") |
110 | local file_name = path_split[#path_split] |
111 | table.remove(path_split, #path_split) |
112 |
|
113 | for _, part in pairs(path_split) do |
114 | path_string = path_string.."/"..part |
115 | table.insert(treelinks, {base_path..path_string, part}) |
116 | end |
117 | path_string = path_string.."/"..file_name |
118 | table.insert(treelinks, { |
119 | string.format("/%s/blob/%s"..path_string, repo.name, branch.name), file_name |
120 | }) |
121 |
|
122 | build:add(nav(treelinks, " / ")) |
123 | build:add("</h3>") |
124 |
|
125 | -- File |
126 | build:add([[<div class="blob">]]) |
127 |
|
128 | local content = git.show_file(repo_dir, branch.name, file_path) |
129 |
|
130 | local text_table = {} |
131 | text_table.class = "blob-lines" |
132 | text_table.headers = {} |
133 | text_table.rows = {} |
134 | for i, line in pairs(string.split(utils.highlight(content, file_name), "\n")) do |
135 | if line ~= "" then |
136 | table.insert(text_table.rows, {i, line}) |
137 | else |
138 | table.insert(text_table.rows, {i, "\n"}) -- preserve newlines for copying/pasting |
139 | end |
140 | end |
141 |
|
142 | build:add(tabulate(text_table)) |
143 |
|
144 | build:add("</div>") |
145 |
|
146 | return build |
147 | end |
148 |
|
149 | return _M |
150 |
|