|
1 |
| --- Pandoc Lua filter to extract LaTeX index commands from HTML comments |
2 |
| --- This allows index entries to be hidden from MkDocs while being processed for PDF output |
| 1 | +-- Pandoc Lua filter to extract LaTeX index commands |
| 2 | +-- This allows index entries to be included in PDF output while being invisible in MkDocs |
3 | 3 |
|
4 |
| -function extractIndexFromComments(elem) |
5 |
| - -- Handle RawInline and RawBlock elements with any format |
6 |
| - if elem.tag == "RawInline" or elem.tag == "RawBlock" then |
| 4 | +function processIndexCommands(elem) |
| 5 | + -- Handle RawInline and RawBlock elements with LaTeX format |
| 6 | + if elem.tag == "RawInline" and elem.format == "latex" then |
7 | 7 | local text = elem.text or ""
|
8 |
| - -- Match HTML comments containing \index commands |
9 |
| - local index_cmd = text:match("<!%-%-%s*\\index{(.-)}.-->") |
10 |
| - if index_cmd then |
11 |
| - return pandoc.RawInline("latex", "\\index{" .. index_cmd .. "}") |
12 |
| - end |
13 |
| - -- Match full \index{...} commands in comments (allowing spaces) |
14 |
| - local full_index = text:match("<!%-%-%s*(%\\index{.-})%s*-->") |
15 |
| - if full_index then |
16 |
| - return pandoc.RawInline("latex", full_index) |
| 8 | + -- If it contains \index commands, pass them through |
| 9 | + if text:match("\\index{.-}") then |
| 10 | + return elem -- Pass through as-is |
17 | 11 | end
|
18 | 12 | end
|
19 | 13 | return elem
|
20 | 14 | end
|
21 | 15 |
|
22 |
| --- Also try to catch HTML comments that might be processed as plain text |
23 |
| -function extractFromPlainText(elem) |
24 |
| - local text = "" |
| 16 | +-- Convert standalone \index{} commands in text to raw LaTeX |
| 17 | +function convertIndexToRaw(elem) |
25 | 18 | if elem.tag == "Str" then
|
26 |
| - text = elem.text or "" |
27 |
| - elseif elem.tag == "Para" then |
28 |
| - text = pandoc.utils.stringify(elem) |
29 |
| - end |
30 |
| - |
31 |
| - if text and text:match("<!%-%-%s*\\index{.-}%s*-->") then |
32 |
| - -- Replace HTML comments with LaTeX index commands |
33 |
| - local new_text = text:gsub("<!%-%-%s*(\\index{.-})%s*-->", "%1") |
34 |
| - if new_text ~= text then |
35 |
| - return pandoc.RawInline("latex", new_text) |
| 19 | + local text = elem.text or "" |
| 20 | + -- If the entire string is just \index commands, convert to raw LaTeX |
| 21 | + if text:match("^\\index{.-}+$") then |
| 22 | + return pandoc.RawInline("latex", text) |
| 23 | + end |
| 24 | + -- If it contains \index commands mixed with other text, extract them |
| 25 | + if text:match("\\index{.-}") then |
| 26 | + local parts = {} |
| 27 | + local last_end = 1 |
| 28 | + for index_cmd in text:gmatch("(\\index{.-})") do |
| 29 | + local start_pos, end_pos = text:find(index_cmd, last_end, true) |
| 30 | + if start_pos > last_end then |
| 31 | + table.insert(parts, pandoc.Str(text:sub(last_end, start_pos - 1))) |
| 32 | + end |
| 33 | + table.insert(parts, pandoc.RawInline("latex", index_cmd)) |
| 34 | + last_end = end_pos + 1 |
| 35 | + end |
| 36 | + if last_end <= #text then |
| 37 | + table.insert(parts, pandoc.Str(text:sub(last_end))) |
| 38 | + end |
| 39 | + return parts |
36 | 40 | end
|
37 | 41 | end
|
38 | 42 | return elem
|
39 | 43 | end
|
40 | 44 |
|
41 | 45 | return {
|
42 | 46 | {
|
43 |
| - RawInline = extractIndexFromComments, |
44 |
| - RawBlock = extractIndexFromComments, |
45 |
| - Str = extractFromPlainText, |
46 |
| - Para = extractFromPlainText |
| 47 | + RawInline = processIndexCommands, |
| 48 | + Str = convertIndexToRaw |
47 | 49 | }
|
48 | 50 | }
|
0 commit comments