【问题标题】:Read YAML metadata from a Pandoc markdown file从 Pandoc 降价文件中读取 YAML 元数据
【发布时间】:2017-01-14 22:20:12
【问题描述】:

是否可以在没有 Haskell 过滤器或解析 --to=json 输出的情况下从 Markdown 文件中提取 Pandoc 的元数据(标题、日期等)?

JSON 输出对此特别不方便,因为两个词的标题看起来像:

$ pandoc -t json posts/test.md | jq '.meta | .title'
{
  "t": "MetaInlines",
  "c": [
    {
      "t": "Str",
      "c": "Test"
    },
    {
      "t": "Space"
    },
    {
      "t": "Str",
      "c": "post"
    }
  ]
}

所以即使在jq 阅读了标题之后,我们仍然需要重构单词,任何强调、代码或其他任何东西都只会让它变得更加复杂。

【问题讨论】:

    标签: yaml pandoc


    【解决方案1】:

    我们可以为此使用template variable $meta-json$

    将变量粘贴到文件中(带有扩展名,以阻止 Pandoc 查看它自己的目录),然后将其与 pandoc --template=file.ext 一起使用。

    Pandoc 的输出是一个 JSON 对象,其中包含 "title""date""tags" 等键以及它们各自的值来自 markdown 文档,我们可以使用 jq 轻松解析、过滤和操作。

    $ echo '$meta-json$' > /tmp/metadata.pandoc-tpl
    $ pandoc --template=/tmp/metadata.pandoc-tpl | jq '.title,.tags'
    "The Title"
    [
      "a tag",
      "another tag"
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 2015-12-06
      • 2013-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多