Add text to start of Lua pandoc Span and Div when I show/hide solutions

I am trying to write a Lua filter for pandoc to show solutions based on a metadata variable for both span and div elements. I was able to get the answers to render based on the metadata filter using this this question/answer.

Now, I would like to do two things to make the answer stand out:

  1. I would like to prepend the text "ANSWER: " as the first element in both the Span and Div
  2. I would like to render the text inside each Span and Div using strong.

This Lua filter works to show/hide the solutions:

-- 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: /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}
}

I have tried to add text (like this solution suggests), but that removes inline math and code blocks also need to be included in the answers.

I tried prepending the string "ANSWER: " to the content of the span:

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

but I received the following error: Inline, list of Inlines, or string expected, got table.

Here is a minimal markdown example that would use the filter:

---
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)

```
:::

Let’s start with Span elements, because everything is easier there. To get bold text, it’s enough to wrap the content with pandoc.Strong – pandoc calls bold text “strongly emphasized”.

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

The code to add ANSWER: was almost correct, it just wraps the result in too many curly braces. Removing the extra braces should work:

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

Now on to divs. It’s slightly more complicated there, because we need to traverse all paragraphs in the div to make them bold. It could also be that the div doesn’t start with a paragraph but with a list, so prepending the ANSWER string becomes more difficult.

We solve the first issue by using a “local” filter on all answer elements and then apply it with :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

And now finally, some code to prepend a string to a list of blocks. It can be used in the Div filter function:

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

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật