|
| 1 | +-- for code blocks w/ filename create an enclosing div: |
| 2 | +-- <div class="code-with-filename"> |
| 3 | +-- <div class="code-with-filename-file"> |
| 4 | +-- <pre>filename.py</pre> |
| 5 | +-- </div> |
| 6 | +-- <div class="sourceCode" id="cb1" data-filename="filename.py"> |
| 7 | +-- <pre></pre> |
| 8 | +-- </div> |
| 9 | +-- </div> |
| 10 | + |
| 11 | +local function codeBlockWithFilename(el, filename) |
| 12 | + local filenameEl = pandoc.Div({pandoc.Plain{ |
| 13 | + pandoc.RawInline("html", "<pre>"), |
| 14 | + pandoc.Strong{pandoc.Str(filename)}, |
| 15 | + pandoc.RawInline("html", "</pre>") |
| 16 | + }}, pandoc.Attr("", {"code-with-filename-file"})) |
| 17 | + return pandoc.Div( |
| 18 | + { filenameEl, el:clone() }, |
| 19 | + pandoc.Attr("", {"code-with-filename"}) |
| 20 | + ) |
| 21 | +end |
| 22 | + |
| 23 | +function codeFilename() |
| 24 | + return { |
| 25 | + Blocks = function(blocks) |
| 26 | + |
| 27 | + -- transform ast for 'filename' |
| 28 | + local foundFilename = false |
| 29 | + local newBlocks = pandoc.List() |
| 30 | + for _,block in ipairs(blocks) do |
| 31 | + if block.attributes ~= nil and block.attributes["filename"] then |
| 32 | + local filename = block.attributes["filename"] |
| 33 | + if block.t == "CodeBlock" then |
| 34 | + foundFilename = true |
| 35 | + block.attributes["filename"] = nil |
| 36 | + newBlocks:insert(codeBlockWithFilename(block, filename)) |
| 37 | + elseif block.t == "Div" and block.content[1].t == "CodeBlock" then |
| 38 | + foundFilename = true |
| 39 | + block.attributes["filename"] = nil |
| 40 | + block.content[1] = codeBlockWithFilename(block.content[1], filename) |
| 41 | + newBlocks:insert(block) |
| 42 | + else |
| 43 | + newBlocks:insert(block) |
| 44 | + end |
| 45 | + else |
| 46 | + newBlocks:insert(block) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + -- if we found a file name then return the modified list of blocks |
| 51 | + if foundFilename then |
| 52 | + return newBlocks |
| 53 | + else |
| 54 | + return blocks |
| 55 | + end |
| 56 | + end |
| 57 | + } |
| 58 | +end |
0 commit comments