-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_loader.lua
More file actions
49 lines (41 loc) · 1.6 KB
/
Copy pathhtml_loader.lua
File metadata and controls
49 lines (41 loc) · 1.6 KB
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
local M = {}
function M.load(name, replacements)
local base = hs.configdir .. "/html/" .. name .. "/"
local function readFile(filename)
local path = base .. filename
local f = io.open(path, "r")
if not f then
error("html_loader: missing file " .. path)
end
local content = f:read("*a")
f:close()
return content
end
local html = readFile("index.html")
local css = readFile("style.css")
local js = readFile("script.js")
-- Apply replacements to JS content before inlining
if replacements then
for target, replacement in pairs(replacements) do
local pos = string.find(js, target, 1, true)
while pos do
js = string.sub(js, 1, pos - 1) .. replacement .. string.sub(js, pos + #target)
pos = string.find(js, target, pos + #replacement, true)
end
end
end
-- Replace <link rel="stylesheet" href="style.css"> with inlined <style>
local cssTag = '<link rel="stylesheet" href="style.css">'
local cssPos = string.find(html, cssTag, 1, true)
if cssPos then
html = string.sub(html, 1, cssPos - 1) .. "<style>\n" .. css .. "\n </style>" .. string.sub(html, cssPos + #cssTag)
end
-- Replace <script src="script.js"></script> with inlined <script>
local jsTag = '<script src="script.js"></script>'
local jsPos = string.find(html, jsTag, 1, true)
if jsPos then
html = string.sub(html, 1, jsPos - 1) .. "<script>\n" .. js .. "\n </script>" .. string.sub(html, jsPos + #jsTag)
end
return html
end
return M