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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
--
-- Copyright (c) 2021-2023 Zeping Lee
-- Released under the MIT license.
-- Repository: https://github.com/zepinglee/citeproc-lua
--
local csl = {}
local citeproc = require("citeproc")
local util = citeproc.util
require("lualibs")
local core = require("citeproc-latex-core")
local latex_parser = require("citeproc-latex-parser")
csl.initialized = "false"
csl.id_list = {}
csl.id_map = {} -- Boolean map for checking if id in list
csl.uncited_id_list = {}
csl.uncited_id_map = {}
csl.citations_pre = {}
csl.preview_mode = false -- Whether to use citeproc:preview_citation
function csl.init(style_name, bib_files, lang)
bib_files = util.split(util.strip(bib_files), "%s*,%s*")
csl.engine = core.init(style_name, bib_files, lang)
if csl.engine then
csl.initialized = "true"
else
return
end
-- csl.init is called via \AtBeginDocument and it's executed after
-- loading .aux file. The csl.id_list are already registered.
csl.engine:updateItems(csl.id_list)
if core.uncite_all_items then
for _, item in ipairs(core.item_list) do
if not csl.uncited_id_map[item.id] then
table.insert(csl.uncited_id_list, item.id)
csl.uncited_id_map[item.id] = true
end
end
end
csl.engine:updateUncitedItems(csl.uncited_id_list)
csl.style_class = csl.engine:get_style_class()
end
function csl.get_style_class()
tex.sprint(csl.style_class)
end
function csl.register_citation_info(citation_info)
local citation = core.make_citation(citation_info)
if citation.citationID == "@nocite" then
for _, cite_item in ipairs(citation.citationItems) do
if cite_item.id == "*" then
-- \nocite all items
core.uncite_all_items = true
elseif not csl.uncited_id_map[cite_item.id] then
table.insert(csl.uncited_id_list, cite_item.id)
csl.uncited_id_map[cite_item.id] = true
end
end
else
for _, cite_item in ipairs(citation.citationItems) do
if not csl.id_map[cite_item.id] then
table.insert(csl.id_list, cite_item.id)
csl.id_map[cite_item.id] = true
end
end
end
end
function csl.enable_linking()
csl.engine:enable_linking()
end
---comment
---@param citation_info string
function csl.cite(citation_info)
-- "citationID={ITEM-UNAVAILABLE@1},citationItems={{id={ITEM-UNAVAILABLE}}},properties={noteIndex={1}}"
if not csl.engine then
csl.error("CSL engine is not initialized.")
end
-- util.debug(citation_info)
local citation = core.make_citation(citation_info)
-- util.debug(citation)
local citation_str
if csl.preview_mode then
-- TODO: preview mode in first pass of \blockquote of csquotes
-- citation_str = csl.engine:preview_citation(citation)
citation_str = ""
else
citation_str = csl.engine:process_citation(citation)
end
-- util.debug(citation_str)
-- tex.sprint(citation_str)
-- tex.setcatcode(35, 12) -- #
-- tex.setcatcode(37, 12) -- %
token.set_macro("l__csl_citation_tl", citation_str)
table.insert(csl.citations_pre, {citation.citationID, citation.properties.noteIndex})
end
function csl.nocite(ids_string)
local uncited_ids = util.split(ids_string, "%s*,%s*")
for _, uncited_id in ipairs(uncited_ids) do
if uncited_id == "*" then
if csl.engine then
for id, _ in pairs(core.item_dict) do
if not csl.uncited_id_map[id] then
table.insert(csl.uncited_id_list, id)
csl.uncited_id_map[id] = true
end
end
csl.engine:updateUncitedItems(csl.uncited_id_list)
else
core.uncite_all_items = true
end
else
if not csl.uncited_id_map[uncited_id] then
table.insert(csl.uncited_id_list, uncited_id)
csl.uncited_id_map[uncited_id] = true
if csl.engine then
csl.engine:updateUncitedItems(csl.uncited_id_list)
end
end
end
end
end
function csl.bibliography(filter_str)
if not csl.engine then
util.error("CSL engine is not initialized.")
return
end
-- util.debug(filter)
local result = core.make_bibliography(csl.engine, filter_str)
-- util.debug(result)
-- token.set_macro("g__csl_bibliography_tl", result)
tex.print(util.split(result, "\n"))
end
function csl.set_categories(categories_str)
core.set_categories(csl.engine, categories_str)
end
return csl
|