1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
local function resolve(base, relative)
if relative:sub(1, 1) == "/" then
return relative
end
local combined = base .. "/" .. relative
local segments = {}
for segment in combined:gmatch("[^/]+") do
if segment == ".." then
if #segments > 0 and segments[#segments] ~= ".." then
table.remove(segments)
else
table.insert(segments, "..")
end
elseif segment ~= "." and segment ~= "" then
table.insert(segments, segment)
end
end
local prefix = (base:sub(1, 1) == "/" or relative:sub(1, 1) == "/") and "/" or ""
return prefix .. table.concat(segments, "/")
end
-- The branch from the query string (?h=...), or nil. cgit doesn't pass it
-- to filters, but Lua filters run in cgit's process, which has the CGI
-- environment.
local function query_head()
local query = os.getenv("QUERY_STRING") or ""
for key, value in query:gmatch("([^&=]+)=([^&]*)") do
if key == "h" then
value = value:gsub("%+", " "):gsub("%%(%x%x)", function(hex)
return string.char(tonumber(hex, 16))
end)
if value ~= "" then
return value
end
end
end
return nil
end
local function url_escape(s)
return (s:gsub("[^%w._~/-]", function(c)
return string.format("%%%02X", c:byte())
end))
end
-- cgit provides html() to Lua filters. It writes in order with cgit's own
-- output, which io.write's buffered stdout doesn't. io.write is for running
-- the filter outside of cgit.
local emit = html or io.write
-- Run md2html and return its output.
local function md2html(markdown)
local input = os.tmpname()
local file = assert(io.open(input, "w"))
file:write(markdown)
file:close()
local handle = io.popen("@md2html@ --github < " .. input, "r")
local output = handle and handle:read("*a")
if handle then
handle:close()
end
os.remove(input)
return output
end
local table_style = [[
<style>
div#cgit div#summary table {
width: 100%;
table-layout: fixed;
margin: 1em 0;
border-collapse: collapse;
}
div#cgit div#summary th,
div#cgit div#summary td {
padding: 0.35em 0.6em;
text-align: left;
vertical-align: top;
}
div#cgit div#summary thead th {
border-bottom: 2px solid #888;
}
div#cgit div#summary tbody td {
border-bottom: 1px solid #ddd;
}
div#cgit div#summary tbody tr:nth-child(even) {
background: #f7f7f7;
}
div#cgit div#summary th + th,
div#cgit div#summary td + td {
border-left: 1px solid #eee;
}
</style>
]]
local buffer = ""
local should_render = true
local current_dir = "" -- Stores the directory prefix of the file being processed
local current_file = ""
function filter_open(...)
local args = {...}
current_file = args[1] or ""
if current_file == "" then
current_file = "README.md"
end
-- Strict route enforcement: Allow README.md OR any file starting with docs/ and ending in .md
if current_file == "README.md" or (current_file:sub(1, 5) == "docs/" and current_file:sub(-3) == ".md") then
should_render = true
-- Extract directory context (e.g., "docs/ops.md" -> "docs/")
-- If it's the root README.md, current_dir stays an empty string ""
if current_file ~= "README.md" then
current_dir = current_file:match("(.*/)") or ""
else
current_dir = ""
end
else
should_render = false
end
buffer = ""
end
function filter_write(str)
if should_render then
buffer = buffer .. str
end
end
function filter_close()
if not should_render then
emit("<p><em>Error: Access Denied. Only README.md and documentation markdown files are viewable here.</em></p>")
return
end
local repo = os.getenv("CGIT_REPO_URL") or ""
local head = query_head()
local query = ""
if head and head ~= os.getenv("CGIT_REPO_DEFBRANCH") then
query = "?h=" .. url_escape(head)
end
local processed = buffer:gsub("%[([^%]]+)%]%(([^:)]+)%)", function(text, path)
-- Ignore protocols (http, https, etc.), absolute paths (/), and anchors (#)
if path:sub(1, 1) == "/" or path:sub(1, 1) == "#" or path:match("^%w+:") then
return string.format("[%s](%s)", text, path)
end
local fragment
path, fragment = path:match("^([^#]*)(.*)$")
path = resolve(current_dir, path)
if path:sub(-3) == ".md" then
return string.format("[%s](/%s/about/%s%s%s)", text, repo, path, query, fragment)
else
return string.format("[%s](/%s/tree/%s%s%s)", text, repo, path, query, fragment)
end
end)
local output = md2html(processed)
if output then
if output:find("<table>", 1, true) then
emit(table_style)
end
emit(output)
else
emit("<p>Error: Could not execute md2html.</p>")
end
end
|