2 Commits

Author SHA1 Message Date
murat a98460360c fix(public directory): not deleting old files
fixed by deleting all html files in the public directory
2026-06-17 17:15:35 +03:00
murat 5fbe2da916 feat: use the html file named same as the md file for template if exists
known bug: /public/ directory doesn't cleaned before recreating html
files
2026-06-17 17:05:26 +03:00
+22 -5
View File
@@ -12,20 +12,32 @@ for file in lfs.dir(currentDir) do
-- ignore "." and ".." files -- ignore "." and ".." files
if not(file:sub(1,2) == ".." or file:sub(1,1) == '.') then if not(file:sub(1,2) == ".." or file:sub(1,1) == '.') then
local filePath = currentDir .. '/' .. file local filePath = currentDir .. '/' .. file
-- match file names ending with ".md"
if file:match("index.md") then haveIndexmd = true end if file:match("index.md") then haveIndexmd = true end
if file:match("index.html") then haveIndexhtml = true end if file:match("index.html") then haveIndexhtml = true end
-- match file names ending with ".md"
if file:match("%.md$") then if file:match("%.md$") then
markdownFileCount = markdownFileCount + 1 markdownFileCount = markdownFileCount + 1
markdownFiles[markdownFileCount] = { markdownFiles[markdownFileCount] = {
options = {}, options = {},
filePath = filePath, filePath = filePath,
fileName = file:sub(1, file:len()-3), fileName = file:sub(1, file:len()-3),
content = "" content = "",
hasHtml = false
} }
end end
end end
end end
-- check html files with same name as the md files
for file in lfs.dir(currentDir) do
if file:match("%.html$") then
for index, value in ipairs(markdownFiles) do
if file:match(value.fileName .. ".html") then
markdownFiles[index].hasHtml = true
print(value.fileName .. "file has a html template file.")
end
end
end
end
-- stop the program if "index.html" or "index.md" doesn't exist -- stop the program if "index.html" or "index.md" doesn't exist
if (not(haveIndexhtml) and not(haveIndexmd)) then return print("Can't find index.html or index.md!") end if (not(haveIndexhtml) and not(haveIndexmd)) then return print("Can't find index.html or index.md!") end
@@ -110,8 +122,12 @@ 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
@@ -121,6 +137,7 @@ for index, _ in ipairs(markdownFiles) do
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
-- STYLING -- STYLING
local function styleLists(s) local function styleLists(s)
@@ -264,5 +281,5 @@ for index, _ in ipairs(markdownFiles) do
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