Repository navigation
Expand file tree
/
Copy pathbibhandler.lua
More file actions
540 lines (460 loc) · 17.8 KB
/
Copy pathbibhandler.lua
File metadata and controls
540 lines (460 loc) · 17.8 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
-- Bibliography handler for CSL-JSON citations
--
-- This module converts CSL-JSON bibliography data into formatted HTML citations.
-- It handles various citation formats (journal articles, conference papers, books, etc.)
-- and generates consistent bibliography labels for cross-referencing.
-- ========== DEPENDENCIES ==========
local file_reading = dofile("file_reading.lua")
local utils = dofile("utils.lua")
local ascii_fold_string = utils.ascii_fold_string
local html_escape = utils.html_escape
local print_error = utils.print_error
-- ========== CONSTANTS ==========
local MONTH_NAMES = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
}
local MAX_LABEL_LENGTH = 4
local TEMP_DIR = os.getenv("TEMP_DIR") or "temp"
local DEFAULT_BIB_PATH = os.getenv("REFS_JSON") or (TEMP_DIR .. "/bibliography.json")
local DEFAULT_BIBTEX_PATH = os.getenv("BIBTEX_JSON") or (TEMP_DIR .. "/bibtex-entries.json")
-- ========== MODULE STATE ==========
local bib_cache = nil
local bibtex_cache = nil
local label_cache = nil
local get_bibliography_label -- defined after the bibliography loaders
-- ========== DATE UTILITIES ==========
local function month_number_to_name(month_num)
local num = tonumber(month_num or 0)
return MONTH_NAMES[num]
end
local function extract_month(item)
local date_parts = item.issued and item.issued["date-parts"]
if date_parts and date_parts[1] and date_parts[1][2] then
return month_number_to_name(date_parts[1][2])
end
return nil
end
local function extract_year(item)
local date_parts = item.issued and item.issued["date-parts"]
if date_parts and date_parts[1] and date_parts[1][1] then
local y = tonumber(date_parts[1][1])
if y then return string.format("%.0f", y) end
end
return ""
end
-- ========== AUTHOR NORMALIZATION ==========
local function normalize_authors(author_field)
if author_field == nil then return {} end
local field_type = type(author_field)
if field_type == "string" then
return { { literal = author_field } }
end
if field_type == "table" then
if author_field.family or author_field.given or author_field.literal then
return { author_field }
end
local normalized = {}
for _, author in ipairs(author_field) do
if type(author) == "string" then
table.insert(normalized, { literal = author })
elseif type(author) == "table" then
if author.family or author.given or author.literal then
table.insert(normalized, author)
else
table.insert(normalized, { literal = tostring(author) })
end
end
end
return normalized
end
return {}
end
local function format_author_name(author)
if author.literal and author.literal ~= "" then return author.literal end
local given = author.given or ""
local family = author.family or ""
if given ~= "" and family ~= "" then return given .. " " .. family end
return given ~= "" and given or family
end
local function extract_last_name(author)
if author.family and author.family ~= "" then return author.family end
if author.literal and author.literal ~= "" then
local last_word = author.literal:match("([^%s]+)%s*$")
return last_word or author.literal
end
return author.given or ""
end
-- ========== STRING UTILITIES ==========
local function first_codepoint(s)
return tostring(s):match("[%z\1-\127\194-\244][\128-\191]*") or ""
end
local function first_letters_of_words(s)
local letters = {}
for word in tostring(s):gmatch("%S+") do
table.insert(letters, first_codepoint(word))
end
return table.concat(letters)
end
local function strip_nocase_spans(s)
s = tostring(s or "")
return (s:gsub('<span%s+[^>]*class=["\'][^"\']*nocase[^"\']*["\'][^>]*>(.-)</span>', '%1'))
end
-- ========== LABEL GENERATION ==========
-- Labels follow BibTeX alpha style: the first three letters of a single
-- author's surname, or the surname initials of several authors (at most four,
-- then "+"), followed by the two-digit year. Names are ASCII-folded before
-- truncation so that multibyte letters are never split.
local function make_base_label(authors, year)
local author_key
if #authors == 1 then
author_key = ascii_fold_string(extract_last_name(authors[1])):sub(1, 3)
else
local initials = {}
for _, author in ipairs(authors) do
initials[#initials + 1] = ascii_fold_string(
first_letters_of_words(extract_last_name(author)))
end
author_key = table.concat(initials)
if #author_key > MAX_LABEL_LENGTH then
author_key = author_key:sub(1, MAX_LABEL_LENGTH) .. "+"
end
end
local year_str = tostring(year or "")
local year_suffix = #year_str >= 4 and year_str:sub(3, 4) or ""
return author_key .. year_suffix
end
-- ========== HTML FORMATTING ==========
local function format_page_span(pages)
if not pages or pages == "" then return "" end
local formatted = pages:gsub("%-%-", "–"):gsub("%-", "–")
return '<span class="citePages">:' .. html_escape(formatted) .. '</span>'
end
local function is_safe_bibliography_url(url)
url = tostring(url or ""):match("^%s*(.-)%s*$")
if url == "" then return false end
local scheme = url:match("^([A-Za-z][A-Za-z0-9+.-]*):")
if not scheme then return false end
scheme = scheme:lower()
return scheme == "http" or scheme == "https"
end
local function is_arxiv_container_title(container_title)
return tostring(container_title or ""):lower():match("arxiv") ~= nil
end
--- Scans item fields to find an arXiv ID.
-- Priority: item.eprint > item.arxivid > regex in URL > regex in note
local function find_arxiv_id(item)
-- 1. Check explicit fields
local explicit = item.eprint or item.arxivid or item.arXiv
if explicit and explicit ~= "" then
return (tostring(explicit):gsub("^[Aa][Rr][Xx][Ii][Vv]:", ""))
end
-- 2. Check URL field for "arxiv.org/abs/ID" pattern
if item.URL and item.URL ~= "" then
local match = item.URL:match("arxiv%.org/abs/([%w%.%-%/]+)")
if match then return match end
end
-- 3. Check Note field
if item.note and item.note ~= "" then
local match = item.note:match("arXiv:([%w%.%-%/]+)")
if match then return match end
end
return nil
end
--- Strips resolver prefixes, so "https://dx.doi.org/10.1/x" and
--- "doi:10.1/x" both become "10.1/x".
local function normalize_doi(doi)
doi = tostring(doi or ""):match("^%s*(.-)%s*$")
doi = doi:gsub("^[Hh][Tt][Tt][Pp][Ss]?://[%w%.]*[Dd][Oo][Ii]%.[Oo][Rr][Gg]/", "")
doi = doi:gsub("^[Dd][Oo][Ii]:%s*", "")
return doi
end
--- Creates linked title. Priority: DOI > arXiv (via URL/eprint) > URL
local function format_title_with_link(item)
local title = strip_nocase_spans(item.title or "")
local href = nil
local doi = item.DOI
local arxivid = find_arxiv_id(item)
local url = item.URL
doi = normalize_doi(doi)
if doi ~= "" then
href = "https://doi.org/" .. doi
elseif arxivid then
href = "https://arxiv.org/abs/" .. arxivid
elseif url and url ~= "" then
if is_safe_bibliography_url(url) then
href = url
else
print_error("Unsafe bibliography URL rejected for '%s': %s", title, url)
end
end
if href then
return string.format(
'<a class="citeLinkTitle" href="%s">%s</a>',
html_escape(href),
html_escape(title)
)
else
return string.format('<span class="citeTitle">%s</span>', html_escape(title))
end
end
local function format_venue_info(item)
local parts = {}
local container_title = item["container-title"] or ""
local book_title = item["collection-title"] or item["event"] or item["event-title"] or item.booktitle or ""
local volume = item.volume or ""
local number = item.number or item.issue or ""
local pages = item.page or ""
local year = extract_year(item)
local month = extract_month(item)
local arxivid = find_arxiv_id(item)
-- Use arXiv as the venue only for actual preprints. Published articles may
-- still carry an arXiv URL/eprint, but their journal data should be shown.
if arxivid and (container_title == "" or is_arxiv_container_title(container_title)) then
container_title = "arXiv:" .. arxivid
end
if container_title ~= "" then
-- Journal
table.insert(parts, '<span class="citeJournal">' .. html_escape(container_title) .. '</span>')
if volume ~= "" then table.insert(parts, ', <span class="citeVolume">' .. html_escape(volume) .. '</span>') end
if number ~= "" then table.insert(parts, '<span class="citeNumber">(' .. html_escape(number) .. ')</span>') end
if pages ~= "" then table.insert(parts, format_page_span(pages)) end
if month then table.insert(parts, ', ' .. month .. ' ') else table.insert(parts, ', ') end
table.insert(parts, '<span class="citeYear">' .. html_escape(year) .. '. </span>')
elseif book_title ~= "" then
-- Conference/Book
table.insert(parts, '<span class="citeBooktitle">' .. html_escape(book_title) .. '</span>. ')
local publisher = item.publisher or ""
if publisher ~= "" then table.insert(parts, '<span class="citePublisher">' .. html_escape(publisher) .. '</span>, ') end
table.insert(parts, '<span class="citeYear">' .. html_escape(year) .. '. </span>')
else
-- Report/Thesis/Preprint
local series = item.series or ""
local edition = item.edition or ""
local publisher = item.publisher or ""
if series ~= "" then table.insert(parts, '<span class="citeSeries">' .. html_escape(series) .. ', </span>') end
if publisher ~= "" then table.insert(parts, '<span class="citePublisher">' .. html_escape(publisher) .. '</span>, ') end
if edition ~= "" then table.insert(parts, '<span class="citeEdition">' .. html_escape(edition) .. ',</span> ') end
table.insert(parts, ' <span class="citeYear">' .. html_escape(year) .. '. </span>')
end
return table.concat(parts)
end
local function download_filename_for_key(key)
local filename = tostring(key or ""):gsub("[^%w%._%-]+", "_")
if filename == "" then filename = "citation" end
return filename .. ".bib"
end
local function format_bibtex_controls(key, bibtex_entry)
if not bibtex_entry or bibtex_entry == "" then
print_error("Missing raw BibTeX entry for citation key: %s", key or "")
return ""
end
return table.concat({
'<details class="bibtex-details" data-pagefind-ignore>',
'<summary>.bib</summary>',
'<div class="bibtex-actions">',
'<button type="button" class="bibtex-copy">',
'<img src="icons/icon-clone.svg" class="icon" alt="" aria-hidden="true"/> Copy',
'</button>',
'<button type="button" class="bibtex-download" data-filename="',
html_escape(download_filename_for_key(key)),
'">',
'<img src="icons/icon-download.svg" class="icon" alt="" aria-hidden="true"/> Download',
'</button>',
'</div>',
'<pre class="bibtex-code" data-no-auto-copy="true"><code>',
html_escape(bibtex_entry),
'</code></pre>',
'</details>'
})
end
local function format_bibliography_entry(item, key, label, bibtex_entry)
local entry_id = item.id or (key or "")
local authors = normalize_authors(item.author)
local bib_label = label or get_bibliography_label(entry_id) or entry_id
local parts = {}
table.insert(parts, '<li id="' .. html_escape(entry_id) .. '">')
table.insert(parts, '<span class="citeMeta"><span class="citeKey">[')
table.insert(parts, html_escape(bib_label))
table.insert(parts, ']</span></span><span class="citeText">')
if #authors > 0 then
local linked_names = {}
for _, author in ipairs(authors) do
local name = format_author_name(author)
local scholar_url = "https://scholar.google.com/scholar?q=" .. name:gsub(" ", "+")
table.insert(linked_names, '<a class="citeAuthorLink" href="' .. html_escape(scholar_url) .. '" target="_blank" rel="noopener">' .. html_escape(name) .. '</a>')
end
local author_html
if #linked_names == 1 then
author_html = linked_names[1]
elseif #linked_names == 2 then
author_html = linked_names[1] .. " and " .. linked_names[2]
else
local last = table.remove(linked_names)
author_html = table.concat(linked_names, ", ") .. " and " .. last
end
table.insert(parts, '<span class="citeAuthor">' .. author_html .. '</span>. ')
end
table.insert(parts, format_title_with_link(item) .. ". ")
table.insert(parts, format_venue_info(item))
-- Add the note
if item.note and item.note ~= "" then
table.insert(parts, ' <span class="citeNote">' .. html_escape(item.note) .. '</span>')
end
table.insert(parts, '</span>')
table.insert(parts, format_bibtex_controls(key, bibtex_entry))
table.insert(parts, "</li>")
return table.concat(parts)
end
-- ========== BIBLIOGRAPHY LOADING ==========
local function load_bibliography_json(path)
if bib_cache then return bib_cache end
path = path or DEFAULT_BIB_PATH
local data = file_reading.load_json_file(path, "bibliography", true)
local entry_map = {}
if type(data) == "table" and #data > 0 then
for _, entry in ipairs(data) do
if entry and entry.id then entry_map[entry.id] = entry end
end
else
for id, entry in pairs(data) do
if type(entry) == "table" then
entry.id = entry.id or id
entry_map[entry.id] = entry
end
end
end
bib_cache = entry_map
return bib_cache
end
--- Base label (without a/b suffix) of every entry, cached.
local function bibliography_labels()
if label_cache then return label_cache end
label_cache = {}
for id, entry in pairs(load_bibliography_json()) do
label_cache[id] = make_base_label(normalize_authors(entry.author), extract_year(entry))
end
return label_cache
end
--- Labels for the references cited on one page. Cited entries sharing a
--- base label get suffixes a, b, ... in order of year, title and key; all
--- other entries keep their base label.
local function page_labels(keys)
local base = bibliography_labels()
local bibliography = load_bibliography_json()
local groups, seen = {}, {}
for _, key in ipairs(keys or {}) do
local label = base[key]
if label and not seen[key] then
seen[key] = true
groups[label] = groups[label] or {}
table.insert(groups[label], key)
end
end
local labels = {}
for label, members in pairs(groups) do
if #members == 1 then
labels[members[1]] = label
else
table.sort(members, function(a, b)
local ea, eb = bibliography[a], bibliography[b]
local ya, yb = extract_year(ea), extract_year(eb)
if ya ~= yb then return ya < yb end
local ta = ascii_fold_string(tostring(ea.title or "")):lower()
local tb = ascii_fold_string(tostring(eb.title or "")):lower()
if ta ~= tb then return ta < tb end
return a < b
end)
for index, key in ipairs(members) do
labels[key] = label .. (index <= 26 and string.char(96 + index) or ("_" .. index))
end
end
end
return labels
end
local function load_bibtex_json(path)
if bibtex_cache then return bibtex_cache end
path = path or DEFAULT_BIBTEX_PATH
bibtex_cache = file_reading.load_json_file(path, "raw BibTeX entries", true)
return bibtex_cache
end
-- ========== PUBLIC API ==========
local function build_bibliography_HTML(path, citations, bibtex_path)
local bibliography = load_bibliography_json(path)
local bibtex_entries = load_bibtex_json(bibtex_path)
if type(citations) ~= "table" or #citations == 0 then return "" end
local keys = {}
for _, citation in ipairs(citations) do
keys[#keys + 1] = (type(citation) == "table" and citation.c) or citation
end
local labels = page_labels(keys)
local html_parts = {}
table.insert(html_parts, '<section id="bibliography">\n<h2>Bibliography</h2>\n<ol>\n')
for _, citation in ipairs(citations) do
local cite_key = (type(citation) == "table" and citation.c) or citation
local entry = bibliography[cite_key]
if not entry then
print_error("Missing citation key: %s", cite_key)
else
table.insert(html_parts, format_bibliography_entry(
entry,
cite_key,
labels[cite_key],
bibtex_entries[cite_key]
) .. "\n")
end
end
table.insert(html_parts, "</ol>\n</section>\n")
return table.concat(html_parts)
end
get_bibliography_label = function(id)
return bibliography_labels()[id]
end
local function get_bibliography_tooltip(id)
local bibliography = load_bibliography_json()
local entry = bibliography and bibliography[id]
if not entry then return nil end
local authors = normalize_authors(entry.author)
if not authors or #authors == 0 then return "" end
local names = {}
for _, author in ipairs(authors) do
table.insert(names, extract_last_name(author))
end
local author_str = ""
if #names == 1 then
author_str = names[1]
elseif #names == 2 then
author_str = names[1] .. " and " .. names[2]
elseif #names > 2 then
local last = table.remove(names)
author_str = table.concat(names, ", ") .. " and " .. last
end
local title = strip_nocase_spans(entry.title or "")
local year = extract_year(entry)
return string.format("%s, %s (%s)", author_str, title, year)
end
--- Public, machine-readable bibliography for cross-site use (for example the
--- RealRooted Lean catalog): citation key -> { label, csl, bibtex }, where
--- `label` is the short base label, e.g. "Bre95". A page citing two entries
--- with the same base label shows them with suffixes ("Bre95a", "Bre95b").
local function public_bibliography()
local bibliography = load_bibliography_json() or {}
local bibtex_entries = load_bibtex_json() or {}
local entries = {}
for key, entry in pairs(bibliography) do
entries[key] = {
label = get_bibliography_label(key),
csl = entry,
bibtex = bibtex_entries[key],
}
end
return entries
end
local M = {
build_bibliography_HTML = build_bibliography_HTML,
public_bibliography = public_bibliography,
get_bibliography_label = get_bibliography_label,
page_labels = page_labels,
get_bibliography_tooltip = get_bibliography_tooltip
}
return M