Index

joshstock.in / da32c63

Source for serving and static templating/compiling of https://joshstock.in.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
9813 Dec 2020 01:05da32c63Add git blobsJosh Stockin11440G

Blob @ joshstock.in / lua-gitweb / pages / blob.lua

text/plain4544 bytesdownload raw
1-- pages/blob.lua
2-- File blob page builder
3
4-- Copyright (c) 2020 Joshua 'joshuas3' Stockin
5-- <https://joshstock.in>
6
7local utils = require("utils/utils")
8local git = require("git/git_commands")
9
10local builder = require("utils/builder")
11local tabulate = require("utils/tabulate")
12local nav = require("utils/nav")
13
14local _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 {"timestamp", "Time"},
65 {"shorthash", "Hash"},
66 {"subject", "Subject"},
67 {"author", "Author"},
68 {"changed_files", [[<span class="q" title="# of files changed">#</span>]]},
69 {"changed_plus", [[<span class="q" title="Insertions">(+)</span>]]},
70 {"changed_minus", [[<span class="q" title="Deletions">(-)</span>]]},
71 {"gpggood", [[<span class="q" title="GPG signature status
72
73G: Good (valid) signature
74B: Bad signature
75U: Good signature with unknown validity
76X: Good signature that has expired
77Y: Good signature made by an expired key
78R: Good signature made by a revoked key
79E: Signature can't be checked (e.g. missing key)
80N: No signature">GPG?</span>]]}
81 }
82 commits_table_data.rows = {}
83
84 table.insert(commits_table_data.rows, {
85 commit.count,
86 utils.iso8601(commit.timestamp),
87 string.format([[<a href="/%s/commit/%s">%s</a>]], repo.name, commit.hash, commit.shorthash),
88 utils.html_sanitize(commit.subject),
89 string.format([[<a href="mailto:%s">%s</a>]], commit.email, utils.html_sanitize(commit.author)),
90 commit.diff.num,
91 commit.diff.plus,
92 commit.diff.minus,
93 commit.gpggood
94 })
95
96 build:add(tabulate(commits_table_data))
97
98 -- Tree breadcrumb
99 build:add("<h3>Blob @ ")
100
101 local treelinks = {
102 {string.format("/%s/tree/%s", repo.name, branch.name), repo.name}
103 }
104
105 local base_path = treelinks[1][1] -- /repo/tree/branch
106
107 local path_string = ""
108 local path_split = string.split(file_path, "/")
109 local file_name = path_split[#path_split]
110 table.remove(path_split, #path_split)
111
112 for _, part in pairs(path_split) do
113 path_string = path_string.."/"..part
114 table.insert(treelinks, {base_path..path_string, part})
115 end
116 path_string = path_string.."/"..file_name
117 table.insert(treelinks, {
118 string.format("/%s/blob/%s"..path_string, repo.name, branch.name), file_name
119 })
120
121 build:add(nav(treelinks, " / "))
122 build:add("</h3>")
123
124 -- File
125 build:add([[<div class="blob">]])
126
127 local content = git.show_file(repo_dir, branch.name, file_path)
128
129 local text_table = {}
130 text_table.class = "blob-lines"
131 text_table.headers = {}
132 text_table.rows = {}
133 for i, line in pairs(string.split(utils.highlight(content, file_name), "\n")) do
134 table.insert(text_table.rows, {i, line})
135 end
136
137 build:add(tabulate(text_table))
138
139 build:add("</div>")
140
141 return build
142end
143
144return _M
145