diff --git a/luago.lua b/luago.lua index 46126d5..58169e7 100644 --- a/luago.lua +++ b/luago.lua @@ -122,160 +122,164 @@ end -- get index.html content local indexFile = io.open("index.html"):read("*all") --- remove and create "public/" directory -lfs.rmdir("public") +-- delete html files and create "public/" directory +for file in lfs.dir(currentDir .. "/public") do + if file:match("%.html$") then + os.remove(currentDir .. "/public/" .. file) + end +end lfs.mkdir("public") for index, _ in ipairs(markdownFiles) do if markdownFiles[index].options["draft"] ~= "yes" then - local workDir = lfs.currentdir() - -- create html file for that md file - local htmlFile = io.open(workDir .. "/public/" .. markdownFiles[index].fileName .. ".html", 'w') - if htmlFile then - local content = indexFile - if markdownFiles[index].hasHtml then content = io.open(markdownFiles[index].fileName .. ".html"):read("*all") end + local workDir = lfs.currentdir() + -- create html file for that md file + local htmlFile = io.open(workDir .. "/public/" .. markdownFiles[index].fileName .. ".html", 'w') + if htmlFile then + local content = indexFile + if markdownFiles[index].hasHtml then content = io.open(markdownFiles[index].fileName .. ".html"):read("*all") end - -- STYLING - local function styleLists(s) - local lines = {} - for line in (s .. "\n"):gmatch("([^\n]*)\n") do - 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, ""); inOl = false end - if not inUl then table.insert(result, ""); inUl = false end - if not inOl then table.insert(result, "
    "); inOl = true end - table.insert(result, "
  1. " .. olItem .. "
  2. ") - - else - if inUl then table.insert(result, ""); inUl = false end - if inOl then table.insert(result, "
"); inOl = false end - table.insert(result, line) + -- STYLING + local function styleLists(s) + local lines = {} + for line in (s .. "\n"):gmatch("([^\n]*)\n") do + table.insert(lines, line) end - end - if inUl then table.insert(result, "") end - if inOl then table.insert(result, "") end + local result = {} + local inUl = false + local inOl = false - return table.concat(result, "\n") - end - local function styleParagraph(s) - local lines = {} - for line in (s .. "\n"):gmatch("([^\n]*)\n") do - table.insert(lines, line) - end + for _, line in ipairs(lines) do + local ulItem = line:match("^[%-%*%+]%s+(.*)") + local olItem = line:match("^%d+%.%s+(.*)") - local result = {} - local pLines = {} + if ulItem then + if inOl then table.insert(result, ""); inOl = false end + if not inUl then table.insert(result, ""); inUl = false end + if not inOl then table.insert(result, "
    "); inOl = true end + table.insert(result, "
  1. " .. olItem .. "
  2. ") + + else + if inUl then table.insert(result, ""); inUl = false end + if inOl then table.insert(result, "
"); inOl = false end + table.insert(result, line) + end end + + if inUl then table.insert(result, "") end + if inOl then table.insert(result, "") end + + return table.concat(result, "\n") end - - for _, line in ipairs(lines) do - local isBlock = line:match("^<") or line:match("^#") - local isEmpty = line:match("^%s*$") - - if isBlock or isEmpty then - flushP() - table.insert(result, line) - else - table.insert(pLines, line) + local function styleParagraph(s) + local lines = {} + for line in (s .. "\n"):gmatch("([^\n]*)\n") do + table.insert(lines, 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*(.*)", "
%1
") - table.insert(lines, nline) - end - s = table.concat(lines, "\n") - -- lists - s = styleLists(s) - -- paragraph - s = styleParagraph(s) - s = s:gsub("

", "") - -- line breaks - s = s:gsub("\r?\n", "
") - -- remove
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("()
", "%1") - s = s:gsub("
()", "%1") - s = s:gsub("(<" .. tag .. ">)
", "%1") - s = s:gsub("
(<" .. tag .. ">)", "%1") - end - -- self-closing hr - s = s:gsub("(
)
", "%1") - s = s:gsub("
(
)", "%1") - -- headers - s = s:gsub("######%s*(.-)%s*
", "
%1
") - s = s:gsub("#####%s*(.-)%s*
", "
%1
") - s = s:gsub("####%s*(.-)%s*
", "

%1

") - s = s:gsub("###%s*(.-)%s*
", "

%1

") - s = s:gsub("##%s*(.-)%s*
", "

%1

") - s = s:gsub("#%s*(.-)%s*
", "

%1

") - -- TODO: h1 and h2 using == and -- - -- bold and italic text - s = s:gsub("%*%*(.-)%*%*", "%1") - s = s:gsub("%*(.-)%*", "%1") - s = s:gsub("%_%_(.-)%_%_", "%1") - s = s:gsub("%_(.-)%_", "%1") - -- images - s = s:gsub("!%[(.-)%]%((.-)%)", '%1') - -- links - s = s:gsub("%[(.-)%]%((.-)%)", '%1') - -- code - s = s:gsub("`(.-)`", "%1") - -- horizontal rule - s = s:gsub("%-%-%-", "
") + local result = {} + local pLines = {} - return s - end - markdownFiles[index].content = styling(markdownFiles[index].content) + local function flushP() + if #pLines > 0 then + table.insert(result, "

" .. table.concat(pLines, "\n") .. "

") + pLines = {} + end + end - -- replace with markdown file content - local start_idx, end_idx = content:find("", 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].content .. second - end - while content:find("", 1, true) do - start_idx, end_idx = content:find("", 1, true) + for _, line in ipairs(lines) do + local isBlock = line:match("^<") or line:match("^#") + local isEmpty = line:match("^%s*$") + + if isBlock or isEmpty then + flushP() + table.insert(result, line) + else + 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*(.*)", "
%1
") + table.insert(lines, nline) + end + s = table.concat(lines, "\n") + -- lists + s = styleLists(s) + -- paragraph + s = styleParagraph(s) + s = s:gsub("

", "") + -- line breaks + s = s:gsub("\r?\n", "
") + -- remove
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("()
", "%1") + s = s:gsub("
()", "%1") + s = s:gsub("(<" .. tag .. ">)
", "%1") + s = s:gsub("
(<" .. tag .. ">)", "%1") + end + -- self-closing hr + s = s:gsub("(
)
", "%1") + s = s:gsub("
(
)", "%1") + -- headers + s = s:gsub("######%s*(.-)%s*
", "
%1
") + s = s:gsub("#####%s*(.-)%s*
", "
%1
") + s = s:gsub("####%s*(.-)%s*
", "

%1

") + s = s:gsub("###%s*(.-)%s*
", "

%1

") + s = s:gsub("##%s*(.-)%s*
", "

%1

") + s = s:gsub("#%s*(.-)%s*
", "

%1

") + -- TODO: h1 and h2 using == and -- + -- bold and italic text + s = s:gsub("%*%*(.-)%*%*", "%1") + s = s:gsub("%*(.-)%*", "%1") + s = s:gsub("%_%_(.-)%_%_", "%1") + s = s:gsub("%_(.-)%_", "%1") + -- images + s = s:gsub("!%[(.-)%]%((.-)%)", '%1') + -- links + s = s:gsub("%[(.-)%]%((.-)%)", '%1') + -- code + s = s:gsub("`(.-)`", "%1") + -- horizontal rule + s = s:gsub("%-%-%-", "
") + + return s + end + markdownFiles[index].content = styling(markdownFiles[index].content) + + -- replace with markdown file content + local start_idx, end_idx = content:find("", 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 + content = first .. markdownFiles[index].content .. second + end + while content:find("", 1, true) do + start_idx, end_idx = content:find("", 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 - htmlFile:write(content) - htmlFile:close() - else - print("Error: Cannot create html file for " .. markdownFiles[index].filePath) + htmlFile:write(content) + htmlFile:close() + else + print("Error: Cannot create html file for " .. markdownFiles[index].filePath) + end end end -end