Skip to content

Commit

Permalink
Use a Lua script for all transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
tarleb committed May 28, 2024
1 parent 02c6106 commit 989cd86
Showing 1 changed file with 202 additions and 0 deletions.
202 changes: 202 additions & 0 deletions inara.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
#!/usr/bin/env pandoc-lua

PANDOC_VERSION:must_be_at_least {3,2}
local arg = arg or {[0] = 'inara.lua'}
local input_file = arg[1] or 'paper.md'

-- Imports
local io = require 'io'
local pandoc = require 'pandoc'
local List = require 'pandoc.List'
local mediabag = require 'pandoc.mediabag'
local path = require 'pandoc.path'
local system = require 'pandoc.system'
local template = require 'pandoc.template'
local utils = require 'pandoc.utils'
-- Functions
local run_lua_filter = utils.run_lua_filter
local read = pandoc.read

-- Base settings
local input_format = 'markdown'
local paper_directory = path.directory(input_file)
local output_directory =
path.normalize(
path.join {
system.get_working_directory(),
paper_directory
}
)

local data_dir =
path.normalize(
path.join {
system.get_working_directory(),
path.directory(arg[0]),
'data'
}
)

local function read_file (filename)
local fh = io.open(filename, 'r')
local contents = fh:read 'a'
fh:close()
return contents
end

local fh = io.open(input_file)
local doc = read(fh:read 'a', input_format)
fh:close()
doc = system.with_working_directory(
paper_directory,
function () return mediabag.fill(doc) end
)

-- Set some specific metadata
doc.meta.lang = 'en-US' -- articles must be written in American English
doc.meta['footer-csl'] = 'footer.csl'

local shared_filters = List {
'parse-latex.lua',
'inline-cited-references.lua',
'normalize-metadata.lua',
'time.lua',
'normalize-author-names.lua',
'substitute-in-format.lua',
}

local formats = {
jats = {
format = 'jats_publishing',
filters = List {
'resolve-references.lua',
'remove-references-heading.lua',
'orcid-uri.lua',
},
},
pdf = {
format = 'latex',
filters = List{
'draft.lua',
'self-citation.lua',
},
write_output = function (tex, outfile)
system.with_temporary_directory(
'inara-pdf',
function (dirname)
system.with_working_directory(dirname, function ()
mediabag.write(dirname)
local texfh = io.open(path.join{dirname, 'inara-paper.tex'}, 'w')
texfh:write(tex)
texfh:close()
os.execute 'latexmk -lualatex inara-paper.tex'
local pdffile = path.join{dirname, 'inara-paper.pdf'}
local fh = io.open(outfile, 'w')
fh:write(io.open(pdffile):read'a')
fh:close()
end)
end
)
end
},
}

local function get_template (conf)
return conf.template
and io.open(path.join{data_dir, conf.template}):read 'a'
or template.default(conf.format)
end

-- FIXME!!
local journal_metadata = {
['csl'] = 'apa.csl',
['joss_resource_url'] = 'N/A',
['journal'] = {
['abbrev-title'] = 'JOSS',
['alias'] = 'joss',
['url'] = 'https://joss.theoj.org',
['doi'] = '10.21105/joss',
['title'] = 'Journal of Open Source Software',
['publisher-name'] = 'Open Journals',
['issn'] = '2475-9066',
['eissn'] = '2475-9066',
},
['link-citations'] = true,
['copyright'] = {
['statement'] =
"Authors of papers retain copyright and release the work under a" ..
"Creative Commons Attribution 4.0 International License (CC BY 4.0)",
['holder'] = 'The article authors',
['year'] = '2024',
['type'] = 'open-access',
['link'] = 'https://creativecommons.org/licenses/by/4.0/',
['text'] =
"Authors of papers retain copyright and release the work under a" ..
"Creative Commons Attribution 4.0 International License (CC BY 4.0)"
},
['aas_logo_path'] = 'joss/aas-logo.png',
['europar_logo_path'] = 'joss/europar-logo.png',
['logo_path'] = 'joss/logo.png',
}
for key, value in pairs(journal_metadata) do
doc.meta[key] = value
end
local default_article_info = {
['archive_doi'] = 'DOI unavailable',
['editor_url'] = 'https://example.com',
['repository'] = 'NO_REPOSITORY',
['review_issue_url'] = 'N/A',
['citation_author'] = '¿citation_author?',
['editor_name'] = 'Pending Editor',
['issue'] = '¿ISSUE?',
['page'] = '¿PAGE?',
['published_at'] = '1970-01-01',
['reviewers'] = List{'Pending Reviewers'},
['submitted_at'] = '1970-01-01',
['volume'] = '¿VOL?',
['doi_batch_id'] = 'N/A',
['formatted_doi'] = 'DOI unavailable',
['paper_url'] = 'NO PAPER URL',
['draft'] = true,
}
for key, value in pairs(default_article_info) do
doc.meta[key] = value
end
doc.meta['footer-csl'] = io.open(path.join{data_dir, 'resources', 'footer.csl'})

local target_formats = List {'jats', 'pdf'}

local doc_orig = doc:clone()
for _, format in ipairs(target_formats) do
doc = doc_orig:clone()
local format_conf = assert(formats[format], 'Unsupported format: ' .. format)
local output_format = format_conf.format
FORMAT = output_format
system.with_working_directory(
paper_directory,
function ()
for _, filter_name in ipairs(shared_filters) do
local filter_path = path.join{data_dir, 'filters', filter_name}
PANDOC_SCRIPT_FILE = filter_path
doc = run_lua_filter(doc, filter_path)
end

for _, filter_name in ipairs(format_conf.filters) do
local filter_path = path.join{data_dir, 'filters', filter_name}
PANDOC_SCRIPT_FILE = filter_path
doc = run_lua_filter(doc, filter_path)
end
end
)

-- Writer options
local wopts = {
template = get_template(format_conf),
}
local out = pandoc.write(doc, output_format, wopts)
if format_conf.write_output then
format_conf.write_output(out, path.join{output_directory, 'paper.pdf'})
else
print('Not writing', #out, 'characters')
end
end

0 comments on commit 989cd86

Please sign in to comment.