Initial commit
This commit is contained in:
commit
113d44202e
10
LICENSE
Normal file
10
LICENSE
Normal file
@ -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.
|
||||||
|
|
196
generate
Executable file
196
generate
Executable file
@ -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()
|
||||||
|
|
0
htroot/data/page/placeholder.txt
Normal file
0
htroot/data/page/placeholder.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hello, world!
|
@ -0,0 +1 @@
|
|||||||
|
Here is yet another post, to test the layout.
|
2
htroot/data/post/entries.txt
Normal file
2
htroot/data/post/entries.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
20240610-0948_My first blog post
|
||||||
|
20240610-1237_My second blog post
|
1
htroot/data/setting/blog_name.txt
Normal file
1
htroot/data/setting/blog_name.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
My blog
|
0
htroot/data/setting/index_path.txt
Normal file
0
htroot/data/setting/index_path.txt
Normal file
48
htroot/data/setting/page.html
Normal file
48
htroot/data/setting/page.html
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>$$BLOG$$, page $$PAGE$$</title>
|
||||||
|
<style>
|
||||||
|
html, body {
|
||||||
|
background-color: #ffffff;
|
||||||
|
color: #000000;
|
||||||
|
}
|
||||||
|
|
||||||
|
html {
|
||||||
|
background-color: #eeeeee;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: auto;
|
||||||
|
margin-top: 3em;
|
||||||
|
margin-bottom: 3em;
|
||||||
|
max-width: 640px;
|
||||||
|
padding: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.title {
|
||||||
|
font-size: 3em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.subtitle {
|
||||||
|
font-size: 1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.post_title {
|
||||||
|
font-size: 2em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.post_subtitle {
|
||||||
|
font-size: 1em;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="title">$$BLOG$$</div>
|
||||||
|
<div class="subtitle">Page $$PAGE$$: <a href="$$LINK_PREVIOUS$$">Previous</a>, <a href="$$LINK_NEXT$$">Next</a></div>
|
||||||
|
<hr>
|
||||||
|
$$POSTS$$
|
||||||
|
<div class="subtitle">Page $$PAGE$$: <a href="$$LINK_PREVIOUS$$">Previous</a>, <a href="$$LINK_NEXT$$">Next</a></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
1
htroot/data/setting/page_path.txt
Normal file
1
htroot/data/setting/page_path.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
data/page
|
8
htroot/data/setting/post.html
Normal file
8
htroot/data/setting/post.html
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<div class="post">
|
||||||
|
<div class="post_title">$$TITLE$$</div>
|
||||||
|
<div class="post_subtitle">$$DATE_YEAR$$/$$DATE_MONTH$$/$$DATE_DAY$$ $$TIME_HOUR$$:$$TIME_MINUTE$$ - <a href="$$LINK_RAW$$">Share</a></div>
|
||||||
|
<div class="post_content">
|
||||||
|
$$POST$$
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<hr>
|
1
htroot/data/setting/post_path.txt
Normal file
1
htroot/data/setting/post_path.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
data/post
|
1
htroot/data/setting/posts_per_page.txt
Normal file
1
htroot/data/setting/posts_per_page.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
5
|
1
htroot/data/setting/sub_path.txt
Normal file
1
htroot/data/setting/sub_path.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
/blog
|
20
readme.txt
Normal file
20
readme.txt
Normal file
@ -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 <destination>.
|
||||||
|
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user