Index

joshstock.in / 4deb1aa

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
9512 Dec 2020 00:224deb1aaUpdate lua-gitwebJosh Stockin1131180G

Blob @ joshstock.in / lua-gitweb / app.lua

text/plain4047 bytesdownload raw
1-- app.lua
2-- Entry point for git HTTP site implementation
3
4-- Copyright (c) 2020 Joshua 'joshuas3' Stockin
5-- <https://joshstock.in>
6
7local lyaml = require("lyaml")
8local utils = require("utils")
9local git = require("git_commands")
10local request = require("request")
11local tabulate = require("tabulate")
12local html = require("html")
13
14local parsed_uri = request.parse_uri()
15local content
16
17if parsed_uri.repo == nil then
18 -- home page, list of repositories
19else -- repo found
20 local repo
21 for _,r in pairs(yaml_config) do
22 if parsed_uri.repo == r.name then
23 repo = r
24 break
25 end
26 end
27 if repo then
28 local repo_dir = repo.location.dev
29 local view = parsed_uri.parts[2] or "tree"
30 local branch
31 if pcall(function() -- if branch is real
32 branch = git.get_head(repo_dir, parsed_uri.parts[3]) -- if parts[3] is nil, defaults to "HEAD"
33 end) then
34 if view == "tree" then -- directory display (with automatic README rendering)
35 -- /repo/tree/branch/[DIRECTORY PATH]
36
37 local path = parsed_uri.parts
38 table.remove(path, 3) -- branch
39 table.remove(path, 2) -- "tree"
40 table.remove(path, 1) -- repo
41 if #path > 0 then
42 path = table.concat(path, "/").."/"
43 else
44 path = ""
45 end
46
47 content = html.tree(repo, repo_dir, branch, path)
48
49 elseif view == "blob" then
50 -- /repo/blob/branch/[FILE PATH]
51 elseif view == "raw" then
52 -- /repo/raw/branch/[FILE PATH]
53 elseif view == "log" then
54 -- /repo/log/branch?n=[COMMITS PER PAGE]&skip=[COMMITS TO SKIP]
55
56 content = html.log(repo, repo_dir, branch, 40, 0)
57
58 elseif view == "refs" then
59
60 content = html.refs(repo, repo_dir, branch)
61
62 elseif view == "download" then
63
64 content = html.download(repo, repo_dir, branch)
65
66 elseif view == "commit" then
67 -- /repo/commit/[COMMIT HASH]
68 end
69 end
70 end
71end
72
73if content ~= nil then
74ngx.say([[<style>
75@import url('https://fonts.googleapis.com/css?family=Fira+Sans:400,400i,700,700i&display=swap');
76@import url('https://fonts.googleapis.com/css?family=Fira+Mono:400,400i&display=swap');
77*{
78box-sizing:border-box;
79}
80body{
81font-family:'Fira Sans', sans-serif;
82padding-bottom:200px;
83line-height:1.4;
84max-width:1000px;
85margin:20px auto;
86}
87body>h2{
88 margin-top:5px;
89 margin-bottom:0;
90}
91h3{
92margin-bottom:4px;
93}
94td,th{
95padding:2px 5px;
96border:1px solid #858585;
97text-align:left;
98vertical-align:top;
99}
100th{
101border:1px solid #000;
102}
103table.log,table.files{
104 width:100%;
105}
106table{
107border-collapse:collapse;
108overflow:auto;
109font-family:'Fira Mono', monospace;
110font-size:14px;
111}
112table.log td:not(:nth-child(3)){
113max-width:1%;
114white-space:nowrap;
115}
116table.tree td:not(:nth-child(2)){
117max-width:1%;
118white-space:nowrap;
119}
120span.q{
121text-decoration:underline;
122text-decoration-style:dotted;
123}
124.q:hover{
125cursor:help;
126}
127tr:hover,th{ /*darker color for table head, hovered-over rows*/
128background-color:#dedede;
129}
130div.markdown{
131width:100%;
132padding:20px 50px;
133border:1px solid #858585;
134border-radius:6px;
135}
136img{
137max-width:100%;
138}
139pre{
140background-color:#eee;
141padding:15px;
142overflow-x:auto;
143border-radius:8px;
144}
145:not(pre)>code{
146background-color:#eee;
147padding:2.5px;
148border-radius:4px;
149}
150a{
151text-decoration:none;
152color: #0077aa;
153}
154a:hover{
155 text-decoration:underline;
156}
157</style>]])
158
159 local arrow_left_circle = [[<img style="width:1.2em;height:1.2em;vertical-align:middle;margin-right:0.2em" src="https://joshuas3.s3.amazonaws.com/svg/arrow-left.svg"/>]]
160 ngx.say("<a style=\"margin-left:-1.35em\" href=\"/\">"..arrow_left_circle.."<span style=\"vertical-align:middle\">Index</span></a>")
161 ngx.say(content.body)
162 ngx.exit(ngx.HTTP_OK)
163 return
164else
165 ngx.exit(ngx.HTTP_NOT_FOUND) -- default behavior
166 return
167end
168