I’m trying to write a lua filter for Quarto/pandoc that removes all code blocks that do not match the target languages as defined in the yaml header of the document. This is the filter I got from another answer:
function Pandoc (doc)
if doc.meta.target_lang then
local target_lang = pandoc.utils.stringify(doc.meta.target_lang)
print("target lang is " .. target_lang)
return doc:walk {
CodeBlock = function (cb)
if cb.classes[1] ~= target_lang then
return {}
end
end
}
end
return doc
end
However, this one returns an empty codeblock, which is annoying when rendering to ipynbb
:
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [],
"id": "c0eaebc8-2af1-42bc-840a-dda2e54fddbb"
}
This is the original document:
---
title: Some title
author: Some author
date: last-modified
format:
ipynb:
toc: false
filters:
- langsplit.lua
taget_lang: "python"
---
Here is some text.
```{python test-py}
print("some python code")
```
```{r test-r}
print("some R code")
```