| 1 | -- center-v.lua |
| 2 | -- Pandoc Lua filter for converting ~V~ section break symbols |
| 3 | -- into properly centered paragraphs in EPUB/DOCX/HTML output. |
| 4 | -- |
| 5 | -- Handles multiple input patterns that Obsidian might produce: |
| 6 | -- 1. <center>\n~V~\n</center> (HTML blocks wrapping subscript) |
| 7 | -- 2. Raw ~V~ as a paragraph |
| 8 | -- 3. The tilde-based subscript interpretation (~V~ → subscript V) |
| 9 | |
| 10 | local function make_centered() |
| 11 | if FORMAT == "epub" or FORMAT == "epub3" or FORMAT == "html" or FORMAT == "html5" then |
| 12 | return pandoc.RawBlock("html", '<p style="text-align: center;">~V~</p>') |
| 13 | elseif FORMAT == "docx" or FORMAT == "openxml" then |
| 14 | return pandoc.RawBlock("openxml", [[ |
| 15 | <w:p> |
| 16 | <w:pPr><w:jc w:val="center"/></w:pPr> |
| 17 | <w:r><w:t xml:space="preserve">~V~</w:t></w:r> |
| 18 | </w:p> |
| 19 | ]]) |
| 20 | else |
| 21 | return pandoc.Para({pandoc.Str("~V~")}) |
| 22 | end |
| 23 | end |
| 24 | |
| 25 | function Pandoc(doc) |
| 26 | local newblocks = {} |
| 27 | local i = 1 |
| 28 | while i <= #doc.blocks do |
| 29 | local b1 = doc.blocks[i] |
| 30 | local b2 = doc.blocks[i+1] |
| 31 | local b3 = doc.blocks[i+2] |
| 32 | |
| 33 | -- Pattern 1: <center> block + subscript V + </center> block |
| 34 | if b1 and b2 and b3 |
| 35 | and b1.t == "RawBlock" and b1.format == "html" and b1.text:match("^<center>%s*$") |
| 36 | and b2.t == "Plain" |
| 37 | and #b2.content == 1 |
| 38 | and b2.content[1].t == "Subscript" |
| 39 | and #b2.content[1].content == 1 |
| 40 | and b2.content[1].content[1].t == "Str" |
| 41 | and b2.content[1].content[1].text == "V" |
| 42 | and b3.t == "RawBlock" and b3.format == "html" and b3.text:match("^</center>%s*$") |
| 43 | then |
| 44 | table.insert(newblocks, make_centered()) |
| 45 | i = i + 3 |
| 46 | |
| 47 | -- Pattern 2: Single paragraph containing just "~V~" |
| 48 | elseif b1 and b1.t == "Para" |
| 49 | and #b1.content == 1 |
| 50 | and b1.content[1].t == "Str" |
| 51 | and b1.content[1].text == "~V~" |
| 52 | then |
| 53 | table.insert(newblocks, make_centered()) |
| 54 | i = i + 1 |
| 55 | |
| 56 | -- Pattern 3: Plain block with subscript V (standalone, no center tags) |
| 57 | elseif b1 and b1.t == "Plain" |
| 58 | and #b1.content == 1 |
| 59 | and b1.content[1].t == "Subscript" |
| 60 | and #b1.content[1].content == 1 |
| 61 | and b1.content[1].content[1].t == "Str" |
| 62 | and b1.content[1].content[1].text == "V" |
| 63 | then |
| 64 | table.insert(newblocks, make_centered()) |
| 65 | i = i + 1 |
| 66 | |
| 67 | -- Pattern 4: Para with Str "~" + Subscript "V" + Str "~" (tilde interpreted separately) |
| 68 | elseif b1 and b1.t == "Para" |
| 69 | and #b1.content == 3 |
| 70 | and b1.content[1].t == "Str" and b1.content[1].text == "~" |
| 71 | and b1.content[2].t == "Subscript" |
| 72 | and #b1.content[2].content == 1 |
| 73 | and b1.content[2].content[1].t == "Str" |
| 74 | and b1.content[2].content[1].text == "V" |
| 75 | and b1.content[3].t == "Str" and b1.content[3].text == "~" |
| 76 | then |
| 77 | table.insert(newblocks, make_centered()) |
| 78 | i = i + 1 |
| 79 | |
| 80 | else |
| 81 | table.insert(newblocks, b1) |
| 82 | i = i + 1 |
| 83 | end |
| 84 | end |
| 85 | doc.blocks = newblocks |
| 86 | return doc |
| 87 | end |