如何在Pandoc生成的HTML中包含元标记?

时间:2014-08-20 17:14:20

标签: pandoc

我正在使用Pandoc来编写网站的内容。如何在文档中包含元标记(特别是描述和关键字标记),而不更改传递给Pandoc的命令行参数?

我的意思是,我可以在文档文本中以某种方式包含元标记吗?我不想传递命令行选项,因为有几个不同的页面,我想要从Emacs中发送到Pandoc的不同关键字,并且定制每个页面都是个问题。< / p>

3 个答案:

答案 0 :(得分:12)

我发现在--self-contained命令中添加-spandoc选项可以在顶部的YAML中按文件定义标题内容。

例如:

$ cat foo.md
---
title: Foo
header-includes:
    <meta name="keywords" content="Foo,Bar" />
    <meta name="description" content="My description" />
---

# Bar #

Baz
$ pandoc -s -o foo.html foo.md
$ cat foo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="generator" content="pandoc" />
  <title>Foo</title>
  <style type="text/css">code{white-space: pre;}</style>
  <meta name="keywords" content="Foo,Bar" /> <meta name="description" content="My description" />
</head>
<body>
<div id="header">
<h1 class="title">Foo</h1>
</div>
<h1 id="bar">Bar</h1>
<p>Baz</p>
</body>
</html>

答案 1 :(得分:4)

好的 - 所以David Cain建议的选项1似乎是一个相当简单的解决方案。我的实现有点难看,但有效:

首先,使用字段名称以下划线结尾的YAML标头添加标题行。 Pandoc手册说这些标识符将被忽略。

---
head_: <meta name="description" content="x is super cool">
head_: <meta name="keywords"    content="cool,cold,temperature,super things">
---

让Emacs在当前缓冲区中搜索它并将该行保存到文件中。

(defvar my-markdown-header-file "head.html")

(defun my-markdown-add-headers ()
  (if (file-exists-p my-markdown-header-file)
      (delete-file   my-markdown-header-file))
  (append-to-file "" nil my-markdown-header-file)
  (save-excursion
    (goto-char 1)
    (while (re-search-forward "head_:" nil t)
      ;; get the first and last positions:
      (let ((start (point))
            (end   (progn (end-of-line) (point))))
      ;; include this line, and a newline after it:
      (append-to-file start end my-markdown-header-file)
      (append-to-file "\n"  nil my-markdown-header-file)))))

(add-hook 'markdown-before-export-hook 'my-markdown-add-headers)

(我的elisp能力不是那么好,所以可能有更好的方法来写这个)

最后 - 在Emacs降价模式中使用pandoc -s -H head.html作为降价命令。

感谢David Cain建议-H选项!

编辑作为奖励,我们可以在标题中包含任何内容,包括favicon!

head_: <link rel="icon" type="image/x-icon" href="favicon.ico" />

答案 2 :(得分:1)

使用Pandoc插入元标记的标准方法是使用-H选项。

使用-H / --include-in-header

  1. input.md

    ### Header
    Body text
    
  2. header.html

    <meta name="description" content="My dummy HTML page">
    
  3. 创建文件:

    pandoc -s input.md -o out.html -H header.html
    
  4. 模板

    但是,如果您反对使用命令行参数,则可以使用Templates。 Pandoc通过将已解析的数据插入预定义的模板来构建您的文档:

    $ pandoc -D html
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"$if(lang)$ lang="$lang$" xml:lang="$lang$"$endif$>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta http-equiv="Content-Style-Type" content="text/css" />
      <meta name="generator" content="pandoc" />
    $for(author-meta)$
      <meta name="author" content="$author-meta$" />
     ... (and so on)
    

    没有理由不能修改模板以满足您的需求。 Pandoc已经从文档正文中提取了一些有用的元数据(例如作者)。您可以修改它以对某些自定义元标记执行相同操作。这显然涉及修改Pandoc的来源。

    另一种解决方案(不涉及任何Haskell编码)将使Emacs从每个文件中解析出元数据,然后将余数传递给Pandoc进行渲染。这留下了两种合理的方法:

    1. 在每个文档中允许HTML标题的一个部分,提取此部分并自动将其插入-H
    2. 定义您自己的元数据格式:编写模板以相应地放置元数据,提取此元数据,然后使用-V传递变量值。
    3. 我选择选项1,因为它更简单。

      结论

      据我所知,没有一定程度的修改(无论是自定义Emacs例程,修改Pandoc源还是其他脚本),都没有简单的方法可以做到这一点。我能想到的最简单方法是自动提取原始HTML的一部分,并将其插入到-H的最终文档中。