forked from bojieli/ai-agent-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrossref.lua
More file actions
87 lines (79 loc) · 2.87 KB
/
Copy pathcrossref.lua
File metadata and controls
87 lines (79 loc) · 2.87 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
-- crossref.lua — internal cross-reference links for the book.
--
-- Keeps the existing manual numbering (圖N-M, 第N章) but turns every in-text
-- reference into a clickable internal link, and drops a \label anchor on each
-- figure and chapter. Uses raw LaTeX \label / \hyperref so it does not depend
-- on LaTeX counters (the displayed text is the manual number verbatim).
--
-- Topdown traversal: Image returns `false` to skip its own caption, so figure
-- captions are anchored but NOT self-linkified.
local chap = 0
local function fig_label(n, m) return 'fig:' .. n .. '-' .. m end
local function chap_label(n) return 'chap:' .. n end
-- Replace 圖N-M / 第N章 occurrences inside a plain string with a list of
-- inlines (Str segments + RawInline hyperref links).
local function linkify(text)
local out = {}
local i = 1
local len = #text
while i <= len do
-- earliest of the two patterns from position i
local fs, fe, fn, fm = text:find('圖(%d+)%-(%d+)', i)
local cs, ce, cn = text:find('第(%d+)章', i)
-- choose the nearest match
local pick
if fs and (not cs or fs <= cs) then pick = 'fig'
elseif cs then pick = 'chap' end
if not pick then
table.insert(out, pandoc.Str(text:sub(i)))
break
end
local ms = (pick == 'fig') and fs or cs
local me = (pick == 'fig') and fe or ce
if ms > i then table.insert(out, pandoc.Str(text:sub(i, ms - 1))) end
if pick == 'fig' then
table.insert(out, pandoc.RawInline('latex',
'\\crossreflink{' .. fig_label(fn, fm) .. '}{圖' .. fn .. '-' .. fm .. '}'))
else
table.insert(out, pandoc.RawInline('latex',
'\\crossreflink{' .. chap_label(cn) .. '}{第' .. cn .. '章}'))
end
i = me + 1
end
return out
end
return {
{
traverse = 'topdown',
Header = function(el)
if el.level == 1 and not el.classes:includes('unnumbered') then
chap = chap + 1
el.content:insert(pandoc.RawInline('latex', '\\label{' .. chap_label(chap) .. '}'))
end
return el
end,
-- pandoc 3.x: a standalone image is a Figure block carrying the caption.
Figure = function(el)
local cap = pandoc.utils.stringify(el.caption.long)
local n, m = cap:match('圖%s*(%d+)%-(%d+)')
if n and m then
el.identifier = fig_label(n, m) -- LaTeX writer emits \label{fig:N-M}
end
return el, false -- do not descend into caption (no self-links)
end,
-- Fallback for any inline image that still carries its own caption.
Image = function(el)
local cap = pandoc.utils.stringify(el.caption)
local n, m = cap:match('圖%s*(%d+)%-(%d+)')
if n and m and el.identifier == '' then
el.identifier = fig_label(n, m)
end
return el, false
end,
Str = function(el)
if el.text:find('圖%d') or el.text:find('第%d+章') then
return linkify(el.text)
end
end,
}
}