-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdx-reader.lua
More file actions
114 lines (94 loc) · 3.43 KB
/
cdx-reader.lua
File metadata and controls
114 lines (94 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
-- Codex Document Format → Pandoc Reader
-- Converts Codex JSON to Pandoc AST for output to any format.
--
-- Usage:
-- pandoc -f cdx-reader.lua output.json -o document.md
-- pandoc -f cdx-reader.lua output.json -o document.tex
-- pandoc -f cdx-reader.lua output.json -o document.html
-- Get the directory containing this script
local script_dir = PANDOC_SCRIPT_FILE and (PANDOC_SCRIPT_FILE:match("(.*/)" ) or "") or ""
-- Load library modules
local function load_lib(name)
local paths = {
script_dir .. "lib/" .. name .. ".lua",
"lib/" .. name .. ".lua",
"cdx-pandoc/lib/" .. name .. ".lua",
}
for _, path in ipairs(paths) do
local f = io.open(path, "r")
if f then
f:close()
return dofile(path)
end
end
error("Cannot find library: " .. name)
end
local reader_blocks = load_lib("reader_blocks")
local reader_inlines = load_lib("reader_inlines")
local reader_academic = load_lib("reader_academic")
-- Initialize cross-module references
reader_blocks.set_inlines(reader_inlines)
reader_academic.set_reader_blocks(reader_blocks)
reader_blocks.set_academic(reader_academic)
-- Reconstruct Pandoc metadata from Dublin Core
local function reconstruct_metadata(dc)
if not dc then return pandoc.Meta({}) end
local meta = {}
local terms = dc.terms or dc
if terms.title then
meta.title = pandoc.MetaInlines({pandoc.Str(terms.title)})
end
-- Handle creator as string or array
if terms.creator then
if type(terms.creator) == "string" then
meta.author = pandoc.MetaList({
pandoc.MetaInlines({pandoc.Str(terms.creator)})
})
elseif type(terms.creator) == "table" then
local authors = {}
for _, a in ipairs(terms.creator) do
table.insert(authors, pandoc.MetaInlines({pandoc.Str(a)}))
end
meta.author = pandoc.MetaList(authors)
end
end
if terms.date then
meta.date = pandoc.MetaInlines({pandoc.Str(terms.date)})
end
if terms.description then
meta.abstract = pandoc.MetaBlocks({pandoc.Para({pandoc.Str(terms.description)})})
end
if terms.language then
meta.lang = pandoc.MetaString(terms.language)
end
if terms.subject then
if type(terms.subject) == "string" then
meta.keywords = pandoc.MetaList({pandoc.MetaString(terms.subject)})
elseif type(terms.subject) == "table" then
local keywords = {}
for _, k in ipairs(terms.subject) do
table.insert(keywords, pandoc.MetaString(k))
end
meta.keywords = pandoc.MetaList(keywords)
end
end
return pandoc.Meta(meta)
end
-- Main reader function
function Reader(input, opts)
local json_str = tostring(input)
local parsed = pandoc.json.decode(json_str)
-- Extract content blocks
local content = parsed.content or parsed
local raw_blocks = content.blocks or {}
-- Pre-process footnotes and register with inlines module
local footnotes = reader_blocks.extract_footnotes(raw_blocks)
reader_inlines.set_footnotes(footnotes)
-- Convert blocks
local pandoc_blocks = reader_blocks.convert(raw_blocks)
-- Clear footnotes after processing
reader_inlines.clear_footnotes()
-- Reconstruct metadata
local meta = reconstruct_metadata(parsed.dublin_core)
return pandoc.Pandoc(pandoc_blocks, meta)
end