我建立一个文档准备系统,它应该是能够呈现相当大的文件。
输入采用pandoc风格的降价促销。为了使文档更易于管理,将有每部分文档一个降价文件。例如,文档可能如下所示:
文件: 01_introduction.md
Introduction
============
This is the introduction
Section 1.1
-----------
This is a section
Section 1.2
-----------
This is another section
Run Code Online (Sandbox Code Playgroud)
文件: 02_functionaldescription.md
Functional Description
======================
Section 2.1
-----------
This is a section
Section 2.2
-----------
This is another section
Run Code Online (Sandbox Code Playgroud)
其中一个输出格式是是HTML。我想每个部分生成一个HTML输出文件(对应于输入文件),并生成一个目录主页面。TOC母版页应包含指向其他页面标题的链接。
我没有问题让pandoc产生单独的部分html文件。我什至可以得到它来更正节号,因此它们都适合作为一个大文档的一部分。使用过滤器,我也设法纠正了横断面链接。
问题是内容的主表。如果我将所有单个文件输入一个命令行,就像这样:
pandoc -f markdown -t html --number-sections --toc -s *.md
Run Code Online (Sandbox Code Playgroud)
那么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>
Run Code Online (Sandbox Code Playgroud)
href是假设链接目标位于同一文档中的所有片段。我需要它们指向包含标题的实际文件,如下所示:
<a href="introduction.html#section-1.1">
Run Code Online (Sandbox Code Playgroud)
A无法使过滤器可靠地工作-到达过滤器时,所有文件都已串联在一起,没有任何内容可以显示每个文件的开始或结束位置。
到目前为止,我想出的唯一解决方案涉及使用除pandoc以外的其他方法来生成toc或对toc进行后处理。这些解决方案看起来很复杂,所以我想如果可能的话,以避免它们。
当它到达过滤器时,所有文件都已连接在一起,没有任何内容可以显示每个文件的开始或结束位置。
这是正确的,这意味着只有两个选择:
单独处理每个文件,创建单独的目录。然后,在固定 URL 的同时合并 TOC(相当麻烦..)
利用这样一个事实:在您发布的目录中,顶层中的每个列表项都对应于一个不同的文件。然后,我们可以运行 pandoc 一次来创建一个大文件,然后对大文件应用过滤器,为我们留下正确的目录。
下面我展示了如何使用过滤器执行方法#2:
将此过滤器放入您的文件夹中:https://github.com/sergiocorreia/panfrude-filters/blob/master/filters/fixtoc.py
跑步
pandoc --number-sections --file-scope --toc -s *.md | pandoc -s -f html -o toc.html -F fixtoc.py -M files:"*.md"
这会调用 pandoc 两次,第一次通过创建带有错误链接的 TOC,第二次通过修复 TOC 并删除其他所有内容(基于它收到的元数据,其中包含文件名)。
然后输出存储在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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
516 次 |
最近记录: |