如何使用 markdown 生成完整的 HTML 文档?

How to produce a complete HTML document using markdown?

我们可以处理包含

的文件code.md
Here is our first program.

```python
s = "Hello, World!"
print s
```

使用

markdown2-2.7 -x fenced-code-blocks code.md > code.html

产生HTML

<p>Here is our first program.</p>

<div class="codehilite"><pre><code><span class="n">s</span> <span class="o">=</span> <span class="s">&quot;Hello, World!&quot;</span>
<span class="k">print</span> <span class="n">s</span>
</code></pre></div>

但是除了链接到 css stylesheet 用于语法突出显示之外,还需要添加 htmlheadbody 标记作为后处理。

markdown2 真的适用于这种工作流程,还是可以更无缝地合并后处理(例如,无需使用额外的脚本包装)?

我正在使用安装在 OS X 上的 markdown2pygments 通过:

sudo port install py27-markdown2
sudo port install py27-pygments

Most implementations of Markdown (including the original Perl implementation), only output an HTML fragment which you are then expected to pass to a template or otherwise wrap in an HTML <body>. If you search around, there are a few implementations that may include a feature to optionally output an entire document, but they are rare. Pandoc 可能是最突出的一个,但这也比 Markdown 解析器要多得多。

如果你想继续使用已经从命令行获得的实现,下面应该可以工作(其中 head.htmlfoot.html 分别包含 Markdown 片段之前和之后的所有内容):

cat head.html > code.html
markdown2-2.7 -x fenced-code-blocks code.md >> code.html 
cat foot.html >> code.html

请注意,>> 运算符附加到文件末尾而不是覆盖它。

当然,这不会在您的 <head> 中给您一个 <title>,我想这就是 Markdown 没有正式提供此功能的原因。这也是为什么通常首选模板系统的原因。

所以要回答你的问题,是的,除了少数极少数例外,你应该编写一个脚本来包装 Markdown 以获得完整的 HTML 文档。