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 Stockin1519G

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

text/plain5543 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 utils = require("utils/utils")
8local git = require("git/git_commands")
9local parse_uri = require("utils/parse_uri")
10
11local parsed_uri = parse_uri()
12local content
13
14if parsed_uri.repo == nil then
15 content = require("pages/index")(yaml_config)
16else -- repo found
17 local repo
18 for _,r in pairs(yaml_config) do
19 if parsed_uri.repo == r.name then
20 repo = r
21 break
22 end
23 end
24 if repo then
25 local repo_dir = repo.location.dev
26 local view = parsed_uri.parts[2] or "tree"
27 local branch
28
29 if pcall(function() -- if branch is real
30 branch = git.get_head(repo_dir, parsed_uri.parts[3]) -- if parts[3] is nil, defaults to "HEAD"
31 end) then
32 local res, status = pcall(function() -- effectively catch any errors, 404 if any
33 if view == "tree" then -- directory display (with automatic README rendering)
34 local path = parsed_uri.parts
35 table.remove(path, 3) -- branch
36 table.remove(path, 2) -- "tree"
37 table.remove(path, 1) -- repo
38 if #path > 0 then
39 path = table.concat(path, "/").."/"
40 else
41 path = ""
42 end
43
44 content = require("pages/tree")(repo, repo_dir, branch, path)
45 elseif view == "blob" then
46 local path = parsed_uri.parts
47 table.remove(path, 3) -- branch
48 table.remove(path, 2) -- "tree"
49 table.remove(path, 1) -- repo
50 if #path > 0 then
51 path = table.concat(path, "/")
52 else
53 path = ""
54 end
55
56 content = require("pages/blob")(repo, repo_dir, branch, path)
57 elseif view == "raw" then
58 -- /repo/raw/branch/[FILE PATH]
59 elseif view == "log" then
60 content = require("pages/log")(repo, repo_dir, branch, ngx.var.arg_n, ngx.var.arg_skip)
61 elseif view == "refs" then
62 content = require("pages/refs")(repo, repo_dir, branch)
63 elseif view == "download" then
64 content = require("pages/download")(repo, repo_dir, branch)
65 elseif view == "commit" then
66 -- /repo/commit/[COMMIT HASH]
67 end
68 end) -- pcall
69
70 if res ~= true then
71 ngx.say(res)
72 ngx.say(status)
73 ngx.exit(ngx.HTTP_NOT_FOUND)
74 return
75 end
76 end
77 end
78end
79
80if content ~= nil then -- TODO: HTML templates from files, static serving
81ngx.say([[<style>
82@import url('https://fonts.googleapis.com/css?family=Fira+Sans:400,400i,700,700i&display=swap');
83*{
84box-sizing:border-box;
85}
86body{
87font-family:'Fira Sans', sans-serif;
88padding-bottom:200px;
89line-height:1.4;
90max-width:1000px;
91margin:20px auto;
92}
93body>h2{
94 margin-top:5px;
95 margin-bottom:0;
96}
97h3{
98margin-bottom:4px;
99}
100td,th{
101padding:2px 5px;
102border:1px solid #858585;
103text-align:left;
104vertical-align:top;
105}
106th{
107border:1px solid #000;
108}
109table.files,table.log,table.blob-lines{
110 width:100%;
111 max-width:100%;
112}
113table{
114 border-collapse:collapse;
115 overflow:auto;
116 font-family: monospace;
117 font-size:14px;
118}
119table.files td:first-child{
120 padding-right:calc(5px + 1em);
121}
122table.files td:not(:nth-child(2)), table.log td:not(:nth-child(4)){
123 width:1%;
124 white-space:nowrap;
125}
126span.q{
127text-decoration:underline;
128text-decoration-style:dotted;
129}
130.q:hover{
131cursor:help;
132}
133tr:hover,th{ /*darker color for table head, hovered-over rows*/
134background-color:#dedede;
135}
136div.markdown{
137width:100%;
138padding:20px 50px;
139border:1px solid #858585;
140border-radius:6px;
141}
142img{
143max-width:100%;
144}
145pre{
146background-color:#eee;
147padding:15px;
148overflow-x:auto;
149border-radius:8px;
150}
151:not(pre)>code{
152background-color:#eee;
153padding:2.5px;
154border-radius:4px;
155}
156
157div.blob {
158 border:1px solid #858585;
159 overflow-x: auto;
160}
161table.blob-lines{
162 font-size:12px;
163 max-width:100%;
164}
165table.blob-lines td{
166 border:none;
167}
168table.blob-lines td:first-child{
169 text-align: right;
170 padding-left:20px;
171 user-select: none;
172 color:#858585;
173 max-width:1%;
174 white-space:nowrap;
175}
176table.blob-lines td:nth-child(2){
177 width:100%;
178 white-space:pre;
179}
180
181a{
182text-decoration:none;
183color: #0077aa;
184}
185a:hover{
186 text-decoration:underline;
187}
188.home-banner h1 {
189 margin-top:40px;
190 margin-bottom:0;
191}
192.home-banner p {
193 margin-top:8px;
194 margin-bottom:30px;
195}
196.repo-section .name {
197 margin-bottom:0;
198}
199.repo-section h3 {
200 margin-top:10px;
201}
202.repo-section .description {
203 margin-top:8px;
204}
205.repo-section .nav {
206 margin-top:10px;
207}
208hr {
209 margin: 20px 0;
210}
211</style>]])
212
213 if parsed_uri.repo then
214 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"/>]]
215 ngx.say("<a style=\"margin-left:-1.35em\" href=\"/\">"..arrow_left_circle.."<span style=\"vertical-align:middle\">Index</span></a>")
216 end
217 ngx.say(content:build())
218 ngx.exit(ngx.HTTP_OK)
219 return
220else
221 ngx.exit(ngx.HTTP_NOT_FOUND) -- default behavior
222 return
223end
224