html - 使用 pandoc 制作涵盖几个单独输入文件的主目录

标签 html pandoc

我正在构建一个文档准备系统,它应该能够呈现相当大的文档。

输入是 pandoc 风格的 Markdown 。为了使文档更易于管理,文档的每一节将有一个 Markdown 文件。例如,文档可能如下所示:

文件:01_introduction.md

Introduction
============

This is the introduction

Section 1.1
-----------

This is a section

Section 1.2
-----------

This is another section

文件:02_functionaldescription.md

Functional Description
======================

Section 2.1
-----------

This is a section

Section 2.2
-----------

This is another section

输出格式之一是html。我想为每个部分生成一个 HTML 输出文件(对应于输入文件)和一个主目录页。 TOC 主页面应包含指向其他页面标题的链接。

我没有问题让 pandoc 生成单独的部分 html 文件。我什至可以得到它来更正章节编号,这样它们就好像它们是一个大文档的一部分一样。使用过滤器,我也设法更正了节间链接。

问题是主目录。如果我将所有单独的文件一个一个地输入命令行,如下所示:

pandoc -f markdown -t html --number-sections --toc -s *.md

然后输出的 TOC 如下所示:

<ul>
<li><a href="#introduction"><span class="toc-section-number">1</span> Introduction</a><ul>
<li><a href="#section-1.1"><span class="toc-section-number">1.1</span> Section 1.1</a></li>
<li><a href="#section-1.2"><span class="toc-section-number">1.2</span> Section 1.2</a></li>
</ul></li>
<li><a href="#functional-description"><span class="toc-section-number">2</span> Functional Description</a><ul>
<li><a href="#section-2.1"><span class="toc-section-number">2.1</span> Section 2.1</a></li>
<li><a href="#section-2.2"><span class="toc-section-number">2.2</span> Section 2.2</a></li>
</ul></li>
</ul>

href 是假定链接目标位于同一文档中的所有片段。我需要它们指向包含标题的实际文件,如下所示:

<a href="introduction.html#section-1.1">

A 无法使过滤器可靠地工作 - 当它到达过滤器时,所有文件都已连接在一起,没有任何内容可以显示每个文件的开始或结束位置。

到目前为止,我想出的唯一解决方案是使用 pandoc 以外的东西来生成目录,或者对目录进行后处理。这些解决方案看起来很复杂,所以我想尽可能避免使用它们。

最佳答案

by the time it reaches the filter, all the files have been concatenated together with nothing to show where each file begins or ends.

没错,这意味着只有两个选项:

  1. 分别处理每个文件,创建单独的目录。然后,在固定 URL 的同时合并 TOC(非常麻烦..)

  2. 利用您发布的 TOC 中的事实,顶层的每个列表项对应一个不同的文件。然后,我们可以运行一次 pandoc 来创建一个大文件,然后对该大文件应用过滤器,从而为我们留下正确的目录。

下面我展示了如何使用过滤器执行方法 #2:

  1. 将此过滤器放入您的文件夹:https://github.com/sergiocorreia/panflute-filters/blob/master/filters/fixtoc.py

  2. 运行

    pandoc --number-sections --file-scope --toc -s *.md | pandoc -s -f html -o toc.html -F fixtoc.py -M files:"*.md"

这两次调用 pandoc,第一次通过不正确的链接创建目录,第二次通过修复目录并删除其他所有内容(基于它接收到的元数据,其中包含文件名)。

然后输出存储在 toc.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></title>
  <style type="text/css">code{white-space: pre;}</style>
</head>
<body>
<div id="TOC">
<ul>
<li><a href="01_introduction.html#introduction"><span class="toc-section-number">1</span> Introduction</a>
<ul>
<li><a href="01_introduction.html#section-1.1"><span class="toc-section-number">1.1</span> Section 1.1</a></li>
<li><a href="01_introduction.html#remarks"><span class="toc-section-number">1.2</span> Remarks</a></li>
</ul></li>
<li><a href="02_functionaldescription.html#functional-description"><span class="toc-section-number">2</span> Functional Description</a>
<ul>
<li><a href="02_functionaldescription.html#section-2.1"><span class="toc-section-number">2.1</span> Section 2.1</a></li>
<li><a href="02_functionaldescription.html#remarks"><span class="toc-section-number">2.2</span> Remarks</a></li>
</ul></li>
</ul>
</div>
</body>
</html>

关于html - 使用 pandoc 制作涵盖几个单独输入文件的主目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42664314/

相关文章:

html - 有什么方法可以独立于高度增加盒子阴影的宽度吗?

r - 在R中使用pandoc将.docx转换为.md时如何保留标题和副标题?

pandoc - 如何在 pandoc 中使用 latex 宏?

javascript - 更快的图像加载

javascript - 如何将 JavaScript [object String] 转换为 HTMLElement?

javascript - 从 html 转换为 docx 格式时,Pandoc 不渲染 SVG 图像

python - 是否可以使用 yaml 元数据 block 来扩展 pandoc 语法?

pandoc - 是否可以根据变量值在 pandoc 模板中编写条件语句?

javascript - js 从模态中抓取类

html - 如何在html中引入乘法和除法符号?