generated from AIML4OS/AIML4OS-template-quarto-r
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport-metadata.lua
More file actions
117 lines (106 loc) · 3.43 KB
/
export-metadata.lua
File metadata and controls
117 lines (106 loc) · 3.43 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
function write_metadata(meta)
local function stringify(val)
return pandoc.utils.stringify(val)
end
local function stringify_array(arr)
local out = {}
for _, item in ipairs(arr) do
table.insert(out, stringify(item))
end
return out
end
local function get_multilingual(base, langs)
local out = {}
local fallback = meta[base] and stringify(meta[base]) or nil
for _, lang in ipairs(langs) do
if meta[base .. "-" .. lang] then
out[lang] = stringify(meta[base .. "-" .. lang])
elseif meta[base .. "Multilingual"] and meta[base .. "Multilingual"][lang] then
out[lang] = stringify(meta[base .. "Multilingual"][lang])
elseif fallback then
out[lang] = fallback
end
end
return out
end
local function extract_urls(deployment)
local result = {}
for _, item in ipairs(deployment) do
for platform, val in pairs(item) do
result[platform] = result[platform] or {}
if type(val) == "table" then
local is_lang_map = true
for _, entry in ipairs(val) do
if type(entry) ~= "table" then
is_lang_map = false
break
end
end
if is_lang_map then
for _, entry in ipairs(val) do
for lang, url in pairs(entry) do
result[platform][lang] = stringify(url)
end
end
else
result[platform] = stringify_array(val)
end
elseif type(val) == "string" then
result[platform] = { default = stringify(val) }
end
end
end
return result
end
local function get_last_git_commit_date(filepath)
local cmd = string.format("git log -1 --format=%%cs -- %q", filepath)
local handle = io.popen(cmd)
if handle then
local result = handle:read("*a")
handle:close()
result = result:gsub("%s+", "")
return result ~= "" and result or nil
end
return nil
end
-- Fix duration if given as a string
local duration = 0
if meta.duration then
local dur_str = stringify(meta.duration)
duration = tonumber(dur_str) or 0
end
-- Authors as flat list of names
local authors = {}
if meta.author and type(meta.author) == "table" then
for _, author in ipairs(meta.author) do
if author.t == "MetaMap" and author.name then
table.insert(authors, stringify(author.name))
else
table.insert(authors, stringify(author))
end
end
end
-- Build metadata
local metadata = {
name = get_multilingual("title", {"fr", "en"}),
abstract = get_multilingual("description", {"fr", "en"}),
authors = authors,
tags = meta.tags and stringify_array(meta.tags) or {},
skills = meta.skills and stringify_array(meta.skills) or {},
timeRequired = duration,
imageUrl = meta.image and stringify(meta.image) or nil,
license = meta.license and stringify(meta.license) or nil,
category = "training courses with R and Python",
deploymentUrl = meta.deploymentURL and extract_urls(meta.deploymentURL) or {},
suggestedRequirements = meta.suggestedRequirements and stringify_array(meta.suggestedRequirements) or {},
lastModification = get_last_git_commit_date(quarto.doc.input_file)
}
-- Write to JSON
local json = pandoc.json.encode(metadata, { indent = true })
local file = io.open("metadata.json", "w")
file:write(json)
file:close()
end
return {
Meta = write_metadata
}