From 113d44202e4296f7732ecc8cd9d8c3c8f4c6198c Mon Sep 17 00:00:00 2001 From: Fierelier Date: Mon, 10 Jun 2024 15:48:25 +0200 Subject: [PATCH] Initial commit --- LICENSE | 10 + generate | 196 ++++++++++++++++++ htroot/data/page/placeholder.txt | 0 .../content.html | 1 + .../content.html | 1 + htroot/data/post/entries.txt | 2 + htroot/data/setting/blog_name.txt | 1 + htroot/data/setting/index_path.txt | 0 htroot/data/setting/page.html | 48 +++++ htroot/data/setting/page_path.txt | 1 + htroot/data/setting/post.html | 8 + htroot/data/setting/post_path.txt | 1 + htroot/data/setting/posts_per_page.txt | 1 + htroot/data/setting/sub_path.txt | 1 + readme.txt | 20 ++ 15 files changed, 291 insertions(+) create mode 100644 LICENSE create mode 100755 generate create mode 100644 htroot/data/page/placeholder.txt create mode 100644 htroot/data/post/20240610-0948_My first blog post/content.html create mode 100644 htroot/data/post/20240610-1237_My second blog post/content.html create mode 100644 htroot/data/post/entries.txt create mode 100644 htroot/data/setting/blog_name.txt create mode 100644 htroot/data/setting/index_path.txt create mode 100644 htroot/data/setting/page.html create mode 100644 htroot/data/setting/page_path.txt create mode 100644 htroot/data/setting/post.html create mode 100644 htroot/data/setting/post_path.txt create mode 100644 htroot/data/setting/posts_per_page.txt create mode 100644 htroot/data/setting/sub_path.txt create mode 100644 readme.txt diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b46802c --- /dev/null +++ b/LICENSE @@ -0,0 +1,10 @@ +MIT License + +Copyright (c) 2024 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/generate b/generate new file mode 100755 index 0000000..cec0f0a --- /dev/null +++ b/generate @@ -0,0 +1,196 @@ +#!/usr/bin/env lua +local basepath = (debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])") or "./"):sub(1,-2) +table.unpack = table.unpack or unpack -- Lua 5.1 compat + +function main() + local dataPath = arg[1] + local settingPath = p("data","setting") + local settings = {} + + settings.blog_name = readSetting(p(dataPath,settingPath,"blog_name.txt"),removeNewline) + settings.index_path = readSetting(p(dataPath,settingPath,"index_path.txt"),removeNewline) + settings.sub_path = readSetting(p(dataPath,settingPath,"sub_path.txt"),removeNewline) + settings.page_html = readSetting(p(dataPath,settingPath,"page.html"),removeNewline) + settings.page_path = readSetting(p(dataPath,settingPath,"page_path.txt"),removeNewline) + settings.post_html = readSetting(p(dataPath,settingPath,"post.html"),removeNewline) + settings.post_path = readSetting(p(dataPath,settingPath,"post_path.txt"),removeNewline) + settings.posts_per_page = readSetting(p(dataPath,settingPath,"posts_per_page.txt"),tonumber) + print("* Settings:") + for i,s in pairs(settings) do + print("** " ..i.. ":\n" ..tostring(s)) + end + print("* Reading post entry list ...") + local file = io.open(p(dataPath,settings.post_path,"entries.txt"),"r") + local posts = {} + for line in file:lines() do + table.insert(posts,line) + end + file:close() + table.sort(posts) + tableReverse(posts) + + local postnr = settings.posts_per_page + local post_env = nil + local pagenr = 0 + local pagef = nil + local page_env = nil + local page = nil + local maxPage = math.ceil(#posts / settings.posts_per_page) + + local function dumpPost() + page = applyEnv(settings.page_html,page_env) + pagef:write(page) + pagef:close() + end + + print("* Creating pages ...") + + for i,s in ipairs(posts) do + -- PAGE + if postnr == settings.posts_per_page then + postnr = 0 + pagenr = pagenr + 1 + if pagef ~= nil then + dumpPost() + end + print("** Page: " ..tostring(pagenr).. " ...") + + page_env = {} + page_env.BLOG = settings.blog_name + page_env.PAGE = tostring(pagenr) + if pagenr > 1 then + page_env.LINK_PREVIOUS = p(settings.sub_path,settings.page_path,tostring(pagenr - 1).. ".html") + else + page_env.LINK_PREVIOUS = p(settings.sub_path,settings.page_path,tostring(pagenr).. ".html") + end + + if pagenr >= maxPage then + page_env.LINK_NEXT = p(settings.sub_path,settings.page_path,tostring(pagenr).. ".html") + else + page_env.LINK_NEXT = p(settings.sub_path,settings.page_path,tostring(pagenr + 1).. ".html") + end + page_env.POSTS = "" + + pagef = io.open(p(dataPath,settings.page_path,tostring(pagenr).. ".html"),"w") + end + + -- POST + print("*** " ..s.. " ...") + local stamp,title = table.unpack(stringSplit(s,"_",1)) + stamp = stringSplit(stamp,"-",1) + stamp.date = {} + stamp.date.year = tonumber(string.sub(stamp[1],1,4)) + stamp.date.month = tonumber(string.sub(stamp[1],5,6)) + stamp.date.day = tonumber(string.sub(stamp[1],7,8)) + stamp[1] = nil + + stamp.time = {} + stamp.time.hour = tonumber(string.sub(stamp[2],1,2)) + stamp.time.minute = tonumber(string.sub(stamp[2],3,4)) + stamp[2] = nil + print("**** Title: " ..title) + print("**** Date: " ..tableJoin({stamp.date.year,stamp.date.month,stamp.date.day},"/")) + print("**** Time: " ..tableJoin({stamp.time.hour,stamp.time.minute},":")) + + post_env = {} + post_env.TITLE = title + post_env.DATE_YEAR = tostring(stamp.date.year) + post_env.DATE_MONTH = padNumber(stamp.date.month,2) + post_env.DATE_DAY = padNumber(stamp.date.day,2) + post_env.TIME_HOUR = padNumber(stamp.time.hour,2) + post_env.TIME_MINUTE = padNumber(stamp.time.minute,2) + post_env.LINK_RAW = p(settings.sub_path,settings.post_path,s,"content.html") + post_env.POST = readFile(p(dataPath,settings.post_path,s,"content.html")) + page_env.POSTS = page_env.POSTS .. applyEnv(settings.post_html,post_env) .. "\n" + postnr = postnr + 1 + end + if pagef ~= nil then dumpPost() end +end + +function padNumber(nr,len) + nr = tostring(nr) + local curLen = string.len(nr) + while curLen < len do + nr = "0" .. nr + curLen = curLen + 1 + end + return nr +end + +function applyEnv(str,env) + for i,v in pairs(env) do + str = string.gsub(str,"%$%$" ..i.. "%$%$",v) + end + return str +end + +function readSetting(path,converter) + local data = readFile(path) + if converter == nil or converter == tostring then + return data + end + return converter(data) +end + +function readFile(path) + local file = io.open(path,"r") + local data = file:read("*a") + file:close() + return data +end + +function tableReverse(tbl) + local start = 1 + local stop = #tbl + + while start < stop do + tbl[start], tbl[stop] = tbl[stop], tbl[start] + start = start + 1 + stop = stop - 1 + end +end + +function stringSplit(str, sep, limit) + sep = sep or " " + limit = limit or 0 + + local splits = 0 + local sep_escaped = sep:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]","%%%1") + local parts = {} + for part in str:gmatch("([^" .. sep_escaped .. "]+)") do + if limit > 0 and splits > limit then + parts[splits] = parts[splits] .. sep .. part + else + splits = splits + 1 + table.insert(parts, part) + end + end + return parts +end + +function tableJoin(tbl, sep) + local length = #tbl + if length == 0 then return "" end + + local str = tostring(tbl[1]) + local index = 2 + while index <= length do + str = str .. sep .. tostring(tbl[index]) + index = index + 1 + end + return str +end + +function pathJoin(...) + return tableJoin({...},"/") +end +p = pathJoin + +function removeNewline(str) + str = string.gsub(str,"\n","") + str = string.gsub(str,"\r","") + return str +end + +main() + diff --git a/htroot/data/page/placeholder.txt b/htroot/data/page/placeholder.txt new file mode 100644 index 0000000..e69de29 diff --git a/htroot/data/post/20240610-0948_My first blog post/content.html b/htroot/data/post/20240610-0948_My first blog post/content.html new file mode 100644 index 0000000..af5626b --- /dev/null +++ b/htroot/data/post/20240610-0948_My first blog post/content.html @@ -0,0 +1 @@ +Hello, world! diff --git a/htroot/data/post/20240610-1237_My second blog post/content.html b/htroot/data/post/20240610-1237_My second blog post/content.html new file mode 100644 index 0000000..f1ada30 --- /dev/null +++ b/htroot/data/post/20240610-1237_My second blog post/content.html @@ -0,0 +1 @@ +Here is yet another post, to test the layout. diff --git a/htroot/data/post/entries.txt b/htroot/data/post/entries.txt new file mode 100644 index 0000000..bb26764 --- /dev/null +++ b/htroot/data/post/entries.txt @@ -0,0 +1,2 @@ +20240610-0948_My first blog post +20240610-1237_My second blog post diff --git a/htroot/data/setting/blog_name.txt b/htroot/data/setting/blog_name.txt new file mode 100644 index 0000000..8001766 --- /dev/null +++ b/htroot/data/setting/blog_name.txt @@ -0,0 +1 @@ +My blog diff --git a/htroot/data/setting/index_path.txt b/htroot/data/setting/index_path.txt new file mode 100644 index 0000000..e69de29 diff --git a/htroot/data/setting/page.html b/htroot/data/setting/page.html new file mode 100644 index 0000000..e1a053f --- /dev/null +++ b/htroot/data/setting/page.html @@ -0,0 +1,48 @@ + + +$$BLOG$$, page $$PAGE$$ + + + +
$$BLOG$$
+
Page $$PAGE$$: Previous, Next
+
+$$POSTS$$ +
Page $$PAGE$$: Previous, Next
+ + diff --git a/htroot/data/setting/page_path.txt b/htroot/data/setting/page_path.txt new file mode 100644 index 0000000..38f8143 --- /dev/null +++ b/htroot/data/setting/page_path.txt @@ -0,0 +1 @@ +data/page diff --git a/htroot/data/setting/post.html b/htroot/data/setting/post.html new file mode 100644 index 0000000..27f3816 --- /dev/null +++ b/htroot/data/setting/post.html @@ -0,0 +1,8 @@ +
+
$$TITLE$$
+
$$DATE_YEAR$$/$$DATE_MONTH$$/$$DATE_DAY$$ $$TIME_HOUR$$:$$TIME_MINUTE$$ - Share
+
+$$POST$$ +
+
+
diff --git a/htroot/data/setting/post_path.txt b/htroot/data/setting/post_path.txt new file mode 100644 index 0000000..4196db0 --- /dev/null +++ b/htroot/data/setting/post_path.txt @@ -0,0 +1 @@ +data/post diff --git a/htroot/data/setting/posts_per_page.txt b/htroot/data/setting/posts_per_page.txt new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ b/htroot/data/setting/posts_per_page.txt @@ -0,0 +1 @@ +5 diff --git a/htroot/data/setting/sub_path.txt b/htroot/data/setting/sub_path.txt new file mode 100644 index 0000000..b783af2 --- /dev/null +++ b/htroot/data/setting/sub_path.txt @@ -0,0 +1 @@ +/blog diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..955bb8c --- /dev/null +++ b/readme.txt @@ -0,0 +1,20 @@ +A simple static blog generator in Lua, without external dependencies. +Tested on Lua 5.1+ + +NOTE: Things are not being escaped yet, expect broken stuff, take care. + +Put the data in htroot into your destination folder. Then run ./generate . + +Edit data/setting/sub_path.txt to point to the right sub-directory on your web server, otherwise the links will be broken. + +How to make a post: +* Create this file: data/post/YYYYMMDD-HHMM_My post/content.html +* Add the post to the entry list (data/post/entries.txt), like so: +YYYYMMDD-HHMM_My post +(Sorting does not matter) + +How to change the layout/style: +* Edit data/setting/page.html for the page layout and style +* Edit data/setting/post.html for the post layout (don't put style here) +* You can put $$VARIABLES$$ inside these html files, to reference certain things +