~ryanford/git-blog-tup

05f6c922828c70605d9a447078737b4bddf6f468 — Ryan Ford 3 years ago dc8a090
use same structure in post json plugin as api json plugin
2 files changed, 20 insertions(+), 21 deletions(-)

M plugins/to_html
M plugins/to_json
M plugins/to_html => plugins/to_html +1 -1
@@ 59,7 59,7 @@ local template = [[
         </div>
         <% end %>
         <hr>
         <%- body %>
         <%- post.body %>
      </article>
      <% if #post.commits > 1 then %>
         <hr>

M plugins/to_json => plugins/to_json +19 -20
@@ 3,8 3,6 @@
local json = require("dkjson")
local date = require("date")

local null = json.null

local input = io.input(arg[1])
local output = io.output(arg[2])
local post = {


@@ 12,7 10,7 @@ local post = {
   js = {},
}

local function sort_created(a, b)
local function sort_by_published(a, b)
   local files = { a, b }
   for i,f in ipairs(files) do
      local git_log = io.popen('git log --reverse --format="%at" ' .. f .. ' | head -n1')


@@ 36,7 34,7 @@ local files do
   end
   ls:close()

   table.sort(files, sort_created)
   table.sort(files, sort_by_published)
   for i, f in ipairs(files) do
      files[f] = i
   end


@@ 45,14 43,16 @@ end
local path = input:read("*l")
if arg[1] then input:close() end

local title = path:match("([%w-_%%]+).md")
local title = path:match("(.*)%.md"):gsub("-", " "):gsub("%w+%s?", function(w)
   return w:sub(1, 1):upper() .. w:sub(2)
end)
local commits = {}
do
   local proc = io.popen('git log --no-merges --date="iso-local" --format="%an%n%H%n%ad%n%s" ' .. path)
   local git_log = io.popen('git log --no-merges --date="iso-local" --format="%an%n%H%n%ad%n%s" ' .. path)
   local fields = { [0] = "author", "hash", "date", "message" }
   local i, field = 0
   local commit
   for line in proc:lines() do
   for line in git_log:lines() do
      field = fields[i % 4]
      i = i + 1
      commit = math.ceil(i/4)


@@ 65,7 65,13 @@ do
         commits[commit][field] = line
      end
   end
   proc:close()
   git_log:close()
end

local body do
   local fd = io.open(path)
   body = fd:read("*a")
   fd:close()
end

local fname = path:match("[%w-_%%]+.md")


@@ 74,19 80,12 @@ if files[fname] then
   post.prev_post = files[files[fname] - 1]
   post.next_post = files[files[fname] + 1]
end
post.path = path
post.title = title
post.author = #commits > 0 and commits[#commits].author or false
post.published = #commits > 0 and commits[#commits].date or false
post.body = body
post.commits = commits
post.created = #commits > 0 and commits[#commits].date or "draft"
post.author = #commits > 0 and commits[#commits].author or nil
post.generated = date(os.time()):fmt("%b %d, %Y")
post.copyright = "&copy; " .. date(os.time()):fmt("%Y") .. " @OWNER@"
-- mark uncommitted posts as draft
post.title = title:gsub("-", " "):gsub("%w+%s?", function(w)
   return w:sub(1, 1):upper() .. w:sub(2)
end)

local md = io.open(fname)
post.body = md:read("*a")
md:close()

local post_json = assert(json.encode(post))