Merging Code Blocks using Lua Filter? #10809
Unanswered
JoeyEremondi
asked this question in
Q&A
Replies: 2 comments 1 reply
-
The best way may be to improve the filter you already have. Could you please share it with a sample input?
|
Beta Was this translation helpful? Give feedback.
1 reply
-
You need to accumulate the lines in a variable and to format and output them when certain conditions are reached. Fortunately, the formatting of your sample makes this quite easy (e.g. no need to iterate over Inlines). Here is a proof of concept:
```lua
traverse = "topdown" -- Otherwise all Spans will be treated before all Paras.
local accum_code = ""
local function to_code(el)
-- The HTML code uses non breakable spaces to force indentation.
return string.gsub(pandoc.utils.stringify(el), " ", " ")
end
local function to_code_block(s, lang)
return "#+begin_src " .. lang .. "\n" .. s .. "\n#+end_src"
end
local function append_line(orig, appended)
if orig ~= "" then orig = orig .. '\n' end
return orig .. appended
end
function Para(el)
-- We cannot return a RawBlock from a Span: only Blocks can return
-- a List of Blocks. So we return from the surrounding Para.
local first = el.content[1]
if first.classes:includes("c41") or first.classes:includes("c28") then
accum_code = append_line(accum_code, to_code(first))
return {}
elseif first.classes:includes("c4") or first.classes:includes("c2") then
if accum_code ~= "" then
local to_print = accum_code
accum_code = ""
return { pandoc.RawBlock("org", to_code_block(to_print, "racket")) }
else
return {}
end
end
end
```
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to convert an Epub to org-mode, and the issue is that code blocks are just marked in the epub as having a certain class
<c28>
or<c41>
.I've been able to recognize these with a lua filter, but the problem is it turns each line of code into an inline code declaration, and I'd like to merge them into a single begin_src/end_src block. Likewise, if I do markdown instead of org, they end up as inline
code
instead of code blocks.Is there a way to write a Haskell or Lua filter that merges these blocks?
I can code, but I'm extremely new to lua, and relatively new to pandoc and document stuff in general. I'm not new to Haskell, so if that's the easiest way to solve this then that's fine by me.
Beta Was this translation helpful? Give feedback.
All reactions