From e4b971c8ad382936b53feea409ea7b00dc6107c3 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Fri, 25 Mar 2022 17:00:48 +0100 Subject: [PATCH] Add post metadata JSON file --- build.lua | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/build.lua b/build.lua index 2e69657..7a23d8f 100644 --- a/build.lua +++ b/build.lua @@ -1,8 +1,10 @@ local arrr = require 'arrr' +local cmark = require 'cmark' +local fun = require 'fun' +local json = require 'cjson' local restia = require 'restia' local shapeshift = require 'shapeshift' local yaml = require 'lyaml' -local cmark = require 'cmark' local params do local is = shapeshift.is @@ -37,6 +39,16 @@ local templates = restia.config.bind('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 content = io.open(file):read("*a") 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) + if "string" == type(post.head.tags) then + post.head.tags = split(post.head.tags, "%a+") + end + post.head.slug = post.head.title :gsub(' ', '_') :lower() @@ -115,4 +131,24 @@ if params.delete then restia.utils.delete(params.output) 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)