简体   繁体   English

打印Markdown文件中YAML header的keywords字段的值作为内联列表

[英]Printing the value of the keywords field of the YAML header in a Markdown file as an inline list

This question is a spin-off from Writing thanks and keywords from Markdown to docx via Pandoc .这个问题是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.在我的特定设置中,我通过 Pandoc 将 Markdown 文件的内容转换为 docx 文档。 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. Markdown 文件以 YAML header 打开某些元数据,其中是目标 docx 文档发布者要求的关键字列表。 This list is entered as the value of the keywords YAML field as per the MWE below.根据下面的 MWE,此列表作为keywords YAML 字段的值输入。

My problem is that the list is printed in the target document as a paragraph list;我的问题是该列表作为段落列表打印在目标文档中; ie it starts a new paragraph and each item is printed in its own paragraph.即它开始一个新段落,每个项目都打印在它自己的段落中。 The wished for output is instead an inline list; output 的愿望是一个内联列表; ie, 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我考虑了一个 Lua 过滤器

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.但这会准确地将关键字列表作为段落列表而不是内联列表返回。

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: Pandoc 试图猜测我们想要什么,但在这种情况下我们应该更明确一点:我们在关键字之间穿插逗号和空格分隔符,然后使用它们为关键字构建一个单独的段落:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM