Skip to content

Commit 989cd86

Browse files
committed
Use a Lua script for all transformations
1 parent 02c6106 commit 989cd86

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

inara.lua

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
#!/usr/bin/env pandoc-lua
2+
3+
PANDOC_VERSION:must_be_at_least {3,2}
4+
local arg = arg or {[0] = 'inara.lua'}
5+
local input_file = arg[1] or 'paper.md'
6+
7+
-- Imports
8+
local io = require 'io'
9+
local pandoc = require 'pandoc'
10+
local List = require 'pandoc.List'
11+
local mediabag = require 'pandoc.mediabag'
12+
local path = require 'pandoc.path'
13+
local system = require 'pandoc.system'
14+
local template = require 'pandoc.template'
15+
local utils = require 'pandoc.utils'
16+
-- Functions
17+
local run_lua_filter = utils.run_lua_filter
18+
local read = pandoc.read
19+
20+
-- Base settings
21+
local input_format = 'markdown'
22+
local paper_directory = path.directory(input_file)
23+
local output_directory =
24+
path.normalize(
25+
path.join {
26+
system.get_working_directory(),
27+
paper_directory
28+
}
29+
)
30+
31+
local data_dir =
32+
path.normalize(
33+
path.join {
34+
system.get_working_directory(),
35+
path.directory(arg[0]),
36+
'data'
37+
}
38+
)
39+
40+
local function read_file (filename)
41+
local fh = io.open(filename, 'r')
42+
local contents = fh:read 'a'
43+
fh:close()
44+
return contents
45+
end
46+
47+
local fh = io.open(input_file)
48+
local doc = read(fh:read 'a', input_format)
49+
fh:close()
50+
doc = system.with_working_directory(
51+
paper_directory,
52+
function () return mediabag.fill(doc) end
53+
)
54+
55+
-- Set some specific metadata
56+
doc.meta.lang = 'en-US' -- articles must be written in American English
57+
doc.meta['footer-csl'] = 'footer.csl'
58+
59+
local shared_filters = List {
60+
'parse-latex.lua',
61+
'inline-cited-references.lua',
62+
'normalize-metadata.lua',
63+
'time.lua',
64+
'normalize-author-names.lua',
65+
'substitute-in-format.lua',
66+
}
67+
68+
local formats = {
69+
jats = {
70+
format = 'jats_publishing',
71+
filters = List {
72+
'resolve-references.lua',
73+
'remove-references-heading.lua',
74+
'orcid-uri.lua',
75+
},
76+
},
77+
pdf = {
78+
format = 'latex',
79+
filters = List{
80+
'draft.lua',
81+
'self-citation.lua',
82+
},
83+
write_output = function (tex, outfile)
84+
system.with_temporary_directory(
85+
'inara-pdf',
86+
function (dirname)
87+
system.with_working_directory(dirname, function ()
88+
mediabag.write(dirname)
89+
local texfh = io.open(path.join{dirname, 'inara-paper.tex'}, 'w')
90+
texfh:write(tex)
91+
texfh:close()
92+
os.execute 'latexmk -lualatex inara-paper.tex'
93+
local pdffile = path.join{dirname, 'inara-paper.pdf'}
94+
local fh = io.open(outfile, 'w')
95+
fh:write(io.open(pdffile):read'a')
96+
fh:close()
97+
end)
98+
end
99+
)
100+
end
101+
},
102+
}
103+
104+
local function get_template (conf)
105+
return conf.template
106+
and io.open(path.join{data_dir, conf.template}):read 'a'
107+
or template.default(conf.format)
108+
end
109+
110+
-- FIXME!!
111+
local journal_metadata = {
112+
['csl'] = 'apa.csl',
113+
['joss_resource_url'] = 'N/A',
114+
['journal'] = {
115+
['abbrev-title'] = 'JOSS',
116+
['alias'] = 'joss',
117+
['url'] = 'https://joss.theoj.org',
118+
['doi'] = '10.21105/joss',
119+
['title'] = 'Journal of Open Source Software',
120+
['publisher-name'] = 'Open Journals',
121+
['issn'] = '2475-9066',
122+
['eissn'] = '2475-9066',
123+
},
124+
['link-citations'] = true,
125+
['copyright'] = {
126+
['statement'] =
127+
"Authors of papers retain copyright and release the work under a" ..
128+
"Creative Commons Attribution 4.0 International License (CC BY 4.0)",
129+
['holder'] = 'The article authors',
130+
['year'] = '2024',
131+
['type'] = 'open-access',
132+
['link'] = 'https://creativecommons.org/licenses/by/4.0/',
133+
['text'] =
134+
"Authors of papers retain copyright and release the work under a" ..
135+
"Creative Commons Attribution 4.0 International License (CC BY 4.0)"
136+
},
137+
['aas_logo_path'] = 'joss/aas-logo.png',
138+
['europar_logo_path'] = 'joss/europar-logo.png',
139+
['logo_path'] = 'joss/logo.png',
140+
}
141+
for key, value in pairs(journal_metadata) do
142+
doc.meta[key] = value
143+
end
144+
local default_article_info = {
145+
['archive_doi'] = 'DOI unavailable',
146+
['editor_url'] = 'https://example.com',
147+
['repository'] = 'NO_REPOSITORY',
148+
['review_issue_url'] = 'N/A',
149+
['citation_author'] = '¿citation_author?',
150+
['editor_name'] = 'Pending Editor',
151+
['issue'] = '¿ISSUE?',
152+
['page'] = '¿PAGE?',
153+
['published_at'] = '1970-01-01',
154+
['reviewers'] = List{'Pending Reviewers'},
155+
['submitted_at'] = '1970-01-01',
156+
['volume'] = '¿VOL?',
157+
['doi_batch_id'] = 'N/A',
158+
['formatted_doi'] = 'DOI unavailable',
159+
['paper_url'] = 'NO PAPER URL',
160+
['draft'] = true,
161+
}
162+
for key, value in pairs(default_article_info) do
163+
doc.meta[key] = value
164+
end
165+
doc.meta['footer-csl'] = io.open(path.join{data_dir, 'resources', 'footer.csl'})
166+
167+
local target_formats = List {'jats', 'pdf'}
168+
169+
local doc_orig = doc:clone()
170+
for _, format in ipairs(target_formats) do
171+
doc = doc_orig:clone()
172+
local format_conf = assert(formats[format], 'Unsupported format: ' .. format)
173+
local output_format = format_conf.format
174+
FORMAT = output_format
175+
system.with_working_directory(
176+
paper_directory,
177+
function ()
178+
for _, filter_name in ipairs(shared_filters) do
179+
local filter_path = path.join{data_dir, 'filters', filter_name}
180+
PANDOC_SCRIPT_FILE = filter_path
181+
doc = run_lua_filter(doc, filter_path)
182+
end
183+
184+
for _, filter_name in ipairs(format_conf.filters) do
185+
local filter_path = path.join{data_dir, 'filters', filter_name}
186+
PANDOC_SCRIPT_FILE = filter_path
187+
doc = run_lua_filter(doc, filter_path)
188+
end
189+
end
190+
)
191+
192+
-- Writer options
193+
local wopts = {
194+
template = get_template(format_conf),
195+
}
196+
local out = pandoc.write(doc, output_format, wopts)
197+
if format_conf.write_output then
198+
format_conf.write_output(out, path.join{output_directory, 'paper.pdf'})
199+
else
200+
print('Not writing', #out, 'characters')
201+
end
202+
end

0 commit comments

Comments
 (0)