1

This question is a spin-off from Writing thanks and keywords from Markdown to docx via Pandoc.

In my specific setting I am converting the contents of a Markdown file to a docx document via Pandoc. The Markdown file opens with a YAML header for certain metadata among which is a list of keywords as required by the publisher for the target docx document. This list is entered as the value of the keywords YAML field as per the MWE below.

My problem is that the list is printed in the target document as a paragraph list; i.e. it starts a new paragraph and each item is printed in its own paragraph. The wished for output is instead an inline list; i.e., a single (logical, not necessarily typographic) line of items (the keywords) separated by some constant delimiter (in my case, a comma, but this particular is unsubstantial).

How can this be achieved?

---
title: The Title
author: The Author
thanks: |
  The author wishes to thank certain support.
abstract: |
  This is the abstract.
keywords: [one, two, three, four]
---
   
# A Heading

Text body. 

I have considered a Lua filter along the lines of

function Meta (meta)
  meta.abstract = meta.abstract .. {pandoc.Emph("Keywords: ")} .. meta.keywords
  return meta
end

but this returns precisely the list of keywords as a paragraph list rather than an inline list.

Marcos
  • 135
  • 1
  • 5

1 Answers1

2

Pandoc tries to guess what we want, but in this case we should be a bit more explicit: we intersperse comma and space separators between the keywords, then use that to construct a separate paragraph for keywords:

function Meta (meta)
  -- insert comma and space between keywords
  local keywords, sep = pandoc.Inlines{}, pandoc.Inlines ', '
  for i, kw in ipairs(meta.keywords or {}) do
    keywords:extend(kw)
    if i ~= #meta.keywords then
      keywords:extend(sep)
    end
  end

  -- Append keywords paragraph to abstract
  meta.abstract = meta.abstract ..
    {pandoc.Para({pandoc.Emph 'Keywords: '} .. keywords)}
  return meta
end
tarleb
  • 19,863
  • 4
  • 51
  • 80
  • I am getting an error message in the form of `/path_to_script/keywords.lua:12: bad argument #2 to 'concat' (table expected, got Block) stack traceback: /path_to_script/keywords.lua:12: in function 'Meta'` when I call the script from my actual setup, where `keywords.lua` is my name for the file storing the script and `path_to_script` is just a place holder for the path to that file. – Marcos Jan 08 '23 at 15:08
  • I have just checked that the same happens with my test file as per the MWE in the OP. – Marcos Jan 08 '23 at 15:17
  • Oops, forgot some braces. Fixed now. – tarleb Jan 08 '23 at 16:21
  • Thanks so much. Building from your script, I was able to make some amends to fit my preferences and everything works perfectly. – Marcos Jan 08 '23 at 17:59