Add post metadata JSON file

This commit is contained in:
Talia 2022-03-25 17:00:48 +01:00
parent 1a3a8088db
commit e4b971c8ad
Signed by: darkwiiplayer
GPG key ID: 7808674088232B3E

View file

@ -1,8 +1,10 @@
local arrr = require 'arrr' local arrr = require 'arrr'
local cmark = require 'cmark'
local fun = require 'fun'
local json = require 'cjson'
local restia = require 'restia' local restia = require 'restia'
local shapeshift = require 'shapeshift' local shapeshift = require 'shapeshift'
local yaml = require 'lyaml' local yaml = require 'lyaml'
local cmark = require 'cmark'
local params do local params do
local is = shapeshift.is local is = shapeshift.is
@ -37,6 +39,16 @@ local templates = restia.config.bind('templates', {
}) })
package.loaded.templates = templates package.loaded.templates = templates
-- General purpose utility functions
local function split(str, pattern)
local result = {}
for item in str:gmatch(pattern) do
table.insert(result, item)
end
return result
end
local function read_post(file) local function read_post(file)
local content = io.open(file):read("*a") local content = io.open(file):read("*a")
local head, body = restia.utils.frontmatter(content) local head, body = restia.utils.frontmatter(content)
@ -83,6 +95,10 @@ for file in restia.utils.files(params.input, "%.md$") do
post.head.timestamp = parsedate(post.head.date) post.head.timestamp = parsedate(post.head.date)
if "string" == type(post.head.tags) then
post.head.tags = split(post.head.tags, "%a+")
end
post.head.slug = post.head.title post.head.slug = post.head.title
:gsub(' ', '_') :gsub(' ', '_')
:lower() :lower()
@ -115,4 +131,24 @@ if params.delete then
restia.utils.delete(params.output) restia.utils.delete(params.output)
end end
local function transform(tab)
return function(data)
local success, result = shapeshift.table(tab, "keep")(data)
return result
end
end
local function drop() return true, nil end
-- Generate Post Metadata
tree["posts.json"] = json.encode(
fun
.iter(posts)
:map(transform {
body = drop;
head = shapeshift.table({ file = drop }, 'keep');
})
:totable()
)
restia.utils.builddir(params.output, tree) restia.utils.builddir(params.output, tree)