当我显示/隐藏解决方案时,将文本添加到 Lua pandoc Span 和 Div 的开头

问题描述 投票:0回答:1

我正在尝试为 pandoc 编写一个 Lua 过滤器,以显示基于 span 和 div 元素的元数据变量的解决方案。我能够使用这个这个问题/答案获得基于元数据过滤器的渲染答案。

现在,我想做两件事来使答案脱颖而出:

  1. 我想将文本
    "ANSWER: "
    作为 Span 和 Div 中的第一个元素
  2. 我想使用 Strong 渲染每个
    Span
    Div
    内的文本。

此 Lua 过滤器用于显示/隐藏解决方案:

-- A lua filter to render answers based on YAML block entry `show-answers`
-- 
-- The class `.answers` will render when `show-answers` is True and not if 
-- `show-answers` option is False (note captialization)
--
-- Barely adapted from: https://stackoverflow.com/a/75837627/12586249

local show_answers = nil

function Pandoc (doc)
  show_answers = doc.meta['show-answers']
  return doc
end

function Span (span)
  if span.classes:includes 'answer' then
    return show_answers and span.content or {}
  end
end

function Div (div)
  if div.classes:includes 'answer' then
    return show_answers and div.content or {}
  end
end


return {
  {Pandoc = Pandoc},
  {Div = Div},
  {Span = Span}
}

我尝试添加文本(如此解决方案建议),但这会删除内联数学,并且代码块也需要包含在答案中。

我尝试将字符串

"ANSWER: "
添加到范围的内容中:

function Span (span)
  if span.classes:includes 'answer' then
    new_content = {{"ANSWER: "} .. span.content}
    return show_answers and new_content or {}
  end
end

但我收到以下错误:

Inline, list of Inlines, or string expected, got table

这是一个使用过滤器的最小降价示例:

---
title: A Test of Answer Reveal
show-answers: True
---

## Question 1

What is the equatio of a line? [A line follows the formula $y = mx + b$]{.answer}

## Question 2 

Randomly generate a normally distributed population of weights with a mean weight among women of 150 pounds and 190 pounds among men with standard deviation of 25 pounds.

::: { .answer }
We need to set our $\beta_0$ to equal 150 and $\beta_1$ to equal 40, then generate the population and assume approximately equal shares of men and women:

```{r}
N <- 1e6L
beta0 <- 140
beta1 <- 40
sigma <- 25

pop <- rnorm(N, beta0 + sample(c(0,1), N, replace = TRUE) * beta1, sigma)

```
:::
lua pandoc
1个回答
0
投票

让我们从 Span 元素开始,因为那里一切都更容易。要获得粗体文本,只需用

pandoc.Strong
包裹内容即可 - pandoc 将粗体文本称为“强烈强调”。

function Span (span)
  if span.classes:includes 'answer' then
    return show_answers and pandoc.Strong(span.content) or {}
  end
end

添加

ANSWER: 
的代码几乎是正确的,它只是将结果包裹在太多的大括号中。去掉多余的支架应该可以:

function Span (span)
  if span.classes:includes 'answer' then
    return show_answers
      and pandoc.Strong({"ANSWER: "} .. span.content)
      or {}
  end
end

现在开始讨论div。那里稍微复杂一些,因为我们需要遍历div中的所有段落以将它们加粗。也可能是 div 不是以段落开头而是以列表开头,因此在前面添加

ANSWER
字符串变得更加困难。

我们通过在所有答案元素上使用“本地”过滤器来解决第一个问题,然后使用

:walk
:

应用它
local make_bold = function (element)
  element.content = {pandoc.Strong(element.content)}
  return element
end

function Div (div)
  if div.classes:includes 'answer' then
    return show_answers
      and div.content:walk{Plain = make_bold, Para = make_bold}
      or {}
  end
end

现在最后是一些将字符串添加到块列表前面的代码。可以用在

Div
过滤功能中:

local function prepend_string (str, blks)
  -- Check if we can add the string to the contents of the first element
  if blks[1] and pandoc.utils.type(blks[1].content) == "Inlines" then
    blks[1].content:insert(1, str)
    return blks
  else
    -- otherwise we add a new `Plain` element with the string
    return {pandoc.Plain{str}} .. blks
  end
end
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.