fix(public directory): not deleting old files

fixed by deleting all html files in the public directory
This commit is contained in:
2026-06-17 17:15:35 +03:00
parent 5fbe2da916
commit a98460360c
+138 -134
View File
@@ -122,160 +122,164 @@ end
-- get index.html content -- get index.html content
local indexFile = io.open("index.html"):read("*all") local indexFile = io.open("index.html"):read("*all")
-- remove and create "public/" directory -- delete html files and create "public/" directory
lfs.rmdir("public") for file in lfs.dir(currentDir .. "/public") do
if file:match("%.html$") then
os.remove(currentDir .. "/public/" .. file)
end
end
lfs.mkdir("public") lfs.mkdir("public")
for index, _ in ipairs(markdownFiles) do for index, _ in ipairs(markdownFiles) do
if markdownFiles[index].options["draft"] ~= "yes" then if markdownFiles[index].options["draft"] ~= "yes" then
local workDir = lfs.currentdir() local workDir = lfs.currentdir()
-- create html file for that md file -- create html file for that md file
local htmlFile = io.open(workDir .. "/public/" .. markdownFiles[index].fileName .. ".html", 'w') local htmlFile = io.open(workDir .. "/public/" .. markdownFiles[index].fileName .. ".html", 'w')
if htmlFile then if htmlFile then
local content = indexFile local content = indexFile
if markdownFiles[index].hasHtml then content = io.open(markdownFiles[index].fileName .. ".html"):read("*all") end if markdownFiles[index].hasHtml then content = io.open(markdownFiles[index].fileName .. ".html"):read("*all") end
-- STYLING -- STYLING
local function styleLists(s) local function styleLists(s)
local lines = {} local lines = {}
for line in (s .. "\n"):gmatch("([^\n]*)\n") do for line in (s .. "\n"):gmatch("([^\n]*)\n") do
table.insert(lines, line) table.insert(lines, line)
end
local result = {}
local inUl = false
local inOl = false
for _, line in ipairs(lines) do
local ulItem = line:match("^[%-%*%+]%s+(.*)")
local olItem = line:match("^%d+%.%s+(.*)")
if ulItem then
if inOl then table.insert(result, "</ol>"); inOl = false end
if not inUl then table.insert(result, "<ul>"); inUl = true end
table.insert(result, "<li>" .. ulItem .. "</li>") -- ulItem!
elseif olItem then
if inUl then table.insert(result, "</ul>"); inUl = false end
if not inOl then table.insert(result, "<ol>"); inOl = true end
table.insert(result, "<li>" .. olItem .. "</li>")
else
if inUl then table.insert(result, "</ul>"); inUl = false end
if inOl then table.insert(result, "</ol>"); inOl = false end
table.insert(result, line)
end end
end
if inUl then table.insert(result, "</ul>") end local result = {}
if inOl then table.insert(result, "</ol>") end local inUl = false
local inOl = false
return table.concat(result, "\n") for _, line in ipairs(lines) do
end local ulItem = line:match("^[%-%*%+]%s+(.*)")
local function styleParagraph(s) local olItem = line:match("^%d+%.%s+(.*)")
local lines = {}
for line in (s .. "\n"):gmatch("([^\n]*)\n") do
table.insert(lines, line)
end
local result = {} if ulItem then
local pLines = {} if inOl then table.insert(result, "</ol>"); inOl = false end
if not inUl then table.insert(result, "<ul>"); inUl = true end
table.insert(result, "<li>" .. ulItem .. "</li>") -- ulItem!
local function flushP() elseif olItem then
if #pLines > 0 then if inUl then table.insert(result, "</ul>"); inUl = false end
table.insert(result, "<p>" .. table.concat(pLines, "\n") .. "</p>") if not inOl then table.insert(result, "<ol>"); inOl = true end
pLines = {} table.insert(result, "<li>" .. olItem .. "</li>")
else
if inUl then table.insert(result, "</ul>"); inUl = false end
if inOl then table.insert(result, "</ol>"); inOl = false end
table.insert(result, line)
end
end end
if inUl then table.insert(result, "</ul>") end
if inOl then table.insert(result, "</ol>") end
return table.concat(result, "\n")
end end
local function styleParagraph(s)
for _, line in ipairs(lines) do local lines = {}
local isBlock = line:match("^<") or line:match("^#") for line in (s .. "\n"):gmatch("([^\n]*)\n") do
local isEmpty = line:match("^%s*$") table.insert(lines, line)
if isBlock or isEmpty then
flushP()
table.insert(result, line)
else
table.insert(pLines, line)
end end
end
flushP()
return table.concat(result, "\n") local result = {}
end local pLines = {}
local function styling(s)
-- blockquote
local lines = {}
for line in (s .. "\n"):gmatch("([^\n]*)\n?") do
local nline = line:gsub("^>%s*(.*)", "<blockquote>%1</blockquote>")
table.insert(lines, nline)
end
s = table.concat(lines, "\n")
-- lists
s = styleLists(s)
-- paragraph
s = styleParagraph(s)
s = s:gsub("<p></p>", "")
-- line breaks
s = s:gsub("\r?\n", "<br>")
-- remove <br> tags around block elements
local blockTags = { "ul", "ol", "li", "blockquote", "p",
"h1", "h2", "h3", "h4", "h5", "h6", "hr" }
for _, tag in ipairs(blockTags) do
s = s:gsub("(</" .. tag .. ">)<br>", "%1")
s = s:gsub("<br>(</" .. tag .. ">)", "%1")
s = s:gsub("(<" .. tag .. ">)<br>", "%1")
s = s:gsub("<br>(<" .. tag .. ">)", "%1")
end
-- self-closing hr
s = s:gsub("(<hr>)<br>", "%1")
s = s:gsub("<br>(<hr>)", "%1")
-- headers
s = s:gsub("######%s*(.-)%s*<br>", "<h6>%1</h6>")
s = s:gsub("#####%s*(.-)%s*<br>", "<h5>%1</h5>")
s = s:gsub("####%s*(.-)%s*<br>", "<h4>%1</h4>")
s = s:gsub("###%s*(.-)%s*<br>", "<h3>%1</h3>")
s = s:gsub("##%s*(.-)%s*<br>", "<h2>%1</h2>")
s = s:gsub("#%s*(.-)%s*<br>", "<h1>%1</h1>")
-- TODO: h1 and h2 using == and --
-- bold and italic text
s = s:gsub("%*%*(.-)%*%*", "<b>%1</b>")
s = s:gsub("%*(.-)%*", "<i>%1</i>")
s = s:gsub("%_%_(.-)%_%_", "<b>%1</b>")
s = s:gsub("%_(.-)%_", "<i>%1</i>")
-- images
s = s:gsub("!%[(.-)%]%((.-)%)", '<img src="%2" alt="%1"></img>')
-- links
s = s:gsub("%[(.-)%]%((.-)%)", '<a href="%2">%1</a>')
-- code
s = s:gsub("`(.-)`", "<code>%1</code>")
-- horizontal rule
s = s:gsub("%-%-%-", "<hr>")
return s local function flushP()
end if #pLines > 0 then
markdownFiles[index].content = styling(markdownFiles[index].content) table.insert(result, "<p>" .. table.concat(pLines, "\n") .. "</p>")
pLines = {}
end
end
-- replace <!-- sitecontents --> with markdown file content for _, line in ipairs(lines) do
local start_idx, end_idx = content:find("<!-- sitecontents -->", 1, true) local isBlock = line:match("^<") or line:match("^#")
if start_idx and end_idx then local isEmpty = line:match("^%s*$")
local first = content:sub(1, start_idx - 1)
local second = content:sub(end_idx + 1) if isBlock or isEmpty then
content = first .. markdownFiles[index].content .. second flushP()
end table.insert(result, line)
while content:find("<!-- title -->", 1, true) do else
start_idx, end_idx = content:find("<!-- title -->", 1, true) table.insert(pLines, line)
end
end
flushP()
return table.concat(result, "\n")
end
local function styling(s)
-- blockquote
local lines = {}
for line in (s .. "\n"):gmatch("([^\n]*)\n?") do
local nline = line:gsub("^>%s*(.*)", "<blockquote>%1</blockquote>")
table.insert(lines, nline)
end
s = table.concat(lines, "\n")
-- lists
s = styleLists(s)
-- paragraph
s = styleParagraph(s)
s = s:gsub("<p></p>", "")
-- line breaks
s = s:gsub("\r?\n", "<br>")
-- remove <br> tags around block elements
local blockTags = { "ul", "ol", "li", "blockquote", "p",
"h1", "h2", "h3", "h4", "h5", "h6", "hr" }
for _, tag in ipairs(blockTags) do
s = s:gsub("(</" .. tag .. ">)<br>", "%1")
s = s:gsub("<br>(</" .. tag .. ">)", "%1")
s = s:gsub("(<" .. tag .. ">)<br>", "%1")
s = s:gsub("<br>(<" .. tag .. ">)", "%1")
end
-- self-closing hr
s = s:gsub("(<hr>)<br>", "%1")
s = s:gsub("<br>(<hr>)", "%1")
-- headers
s = s:gsub("######%s*(.-)%s*<br>", "<h6>%1</h6>")
s = s:gsub("#####%s*(.-)%s*<br>", "<h5>%1</h5>")
s = s:gsub("####%s*(.-)%s*<br>", "<h4>%1</h4>")
s = s:gsub("###%s*(.-)%s*<br>", "<h3>%1</h3>")
s = s:gsub("##%s*(.-)%s*<br>", "<h2>%1</h2>")
s = s:gsub("#%s*(.-)%s*<br>", "<h1>%1</h1>")
-- TODO: h1 and h2 using == and --
-- bold and italic text
s = s:gsub("%*%*(.-)%*%*", "<b>%1</b>")
s = s:gsub("%*(.-)%*", "<i>%1</i>")
s = s:gsub("%_%_(.-)%_%_", "<b>%1</b>")
s = s:gsub("%_(.-)%_", "<i>%1</i>")
-- images
s = s:gsub("!%[(.-)%]%((.-)%)", '<img src="%2" alt="%1"></img>')
-- links
s = s:gsub("%[(.-)%]%((.-)%)", '<a href="%2">%1</a>')
-- code
s = s:gsub("`(.-)`", "<code>%1</code>")
-- horizontal rule
s = s:gsub("%-%-%-", "<hr>")
return s
end
markdownFiles[index].content = styling(markdownFiles[index].content)
-- replace <!-- sitecontents --> with markdown file content
local start_idx, end_idx = content:find("<!-- sitecontents -->", 1, true)
if start_idx and end_idx then if start_idx and end_idx then
local first = content:sub(1, start_idx - 1) local first = content:sub(1, start_idx - 1)
local second = content:sub(end_idx + 1) local second = content:sub(end_idx + 1)
content = first .. markdownFiles[index].options["title"] .. second content = first .. markdownFiles[index].content .. second
end
while content:find("<!-- title -->", 1, true) do
start_idx, end_idx = content:find("<!-- title -->", 1, true)
if start_idx and end_idx then
local first = content:sub(1, start_idx - 1)
local second = content:sub(end_idx + 1)
content = first .. markdownFiles[index].options["title"] .. second
end
end end
end
htmlFile:write(content) htmlFile:write(content)
htmlFile:close() htmlFile:close()
else else
print("Error: Cannot create html file for " .. markdownFiles[index].filePath) print("Error: Cannot create html file for " .. markdownFiles[index].filePath)
end
end end
end end
end