-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.lua
52 lines (44 loc) · 1.39 KB
/
metadata.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
local filters = PANDOC_SCRIPT_FILE:match '.*/'
package.path = filters..'?.lua;'..filters..'../libexec/?.lua;'..package.path
local utils = require 'utils'
local metadata = {}
local stdout = utils.sh 'git log -1 --tags=v[0-9]* --date=format:%B\\ %Y'
metadata.author = stdout:match 'Author:%s+([^\n\r]+)'
metadata.date = stdout:match 'Date:%s+([^\n\r]+)'
function Header (h)
local text = utils.stringify (h.content)
if h.level == 1 then
metadata.header = text:sanitize()
elseif text:upper():match '^NAME$' then
metadata.title = nil
metadata.header = nil
elseif text:upper():match '^AUTHOR' then metadata.author = nil
end
return h
end
function Doc (body)
body.blocks:map (function (block, i)
if block.t == 'Header' and
utils.stringify (block.content):upper():match '^SYNOPSIS$' then
pandoc.walk_block (body.blocks[i+1], {
Code = function (line)
if not metadata.title then
metadata.title = line.text:match '^(%w+)' -- `command
end
end,
Str = function (section)
if not metadata.section then
metadata.section = section.text:match '%((%d)%)' or '1' -- (n)
end
end
})
end
end)
end
function Meta (data)
for key, value in pairs (metadata) do
if data[key] == nil then data[key] = value end
end
return data
end
return {{ Header = Header }, { Doc = Doc }, { Meta = Meta }}