使用标题栏将多个文件编译为一个文件 [英] Compile multiple files into one with title blocks

查看:43
本文介绍了使用标题栏将多个文件编译为一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将多个 pandoc 文件编译成一个输出文档,其中每个输入文件都有一个标题块.

I'd like to know how to compile multiple pandoc files into one output document, where each input file has a title block.

例如假设我有两个文件:

E.g. suppose I have two files:

ch1.md:

% Chapter 1
% John Doe
% 1 Jan 2014
Here is chapter 1.

ch2.md:

% Chapter 2
% Jane Smith
% 3 Jan 2014
Here is chapter 2.

通常,您可以通过将多个输入文件提供给 pandoc 来编译它们:

Typically with multiple input files you can compile them by providing them to pandoc:

pandoc ch1.md ch2.md --standalone -o output.html

但是 pandoc 在编译之前连接输入文件,这意味着只有 first 标题块(来自 ch1.md)被适当地设置样式.我希望 每个 标题块的样式都适当(例如,在 html 中,标题块的第一行的样式为 <h1 class="title">,第二个

依此类推).

However pandoc concatenates the input files before compiling, meaning only the first title block (from ch1.md) is styled appropriately. I would like each title block to be styled appropriately (e.g. in html, the first line of the title block is styled with <h1 class="title">, the second <h2 class="author"> and so on).

(注意:我还尝试将每一章单独编译,然后使用 pandoc 将它们连接在一起.这会删除 1 之后章节的标题样式,但保留作者/日期的样式).

(Note: I have also tried compiling each chapter as standalone separately, then concatenating these together using pandoc. This removes the title styling for chapters after 1, though keeps styling for the authors/date).

为什么?我可以:

  • 将每一章编译成单独的文档,并适当标记作者/标题/日期
  • 一起编译整个文档,并为每一章适当标记作者/标题/日期(可以使用--chapters选项)

我可以直接在每个章节文件中用#"(h1)指定标题,用##"(h2)指定作者,用###"(h3)指定日期,但这意味着 pandoc 没有知道"我的文档的标题/标题/日期是什么,所以(例如)如果我编译成乳胶它不会使用 \date{}\author{} 适当标记.

I could just specify the heading with '#' (h1), author with '##' (h2), and date with '###' (h3) in each chapter file directly but this means pandoc doesn't "know" what the title/heading/date of my document are, so (e.g.) if I compile to latex it won't use the \date{} or \author{} tags appropriately.

推荐答案

我写了一个 pandoc 过滤器 在每个章节的文件上运行时,插入标题块作为标题(标题为 1 级,作者为 2 级,日期为 3 级.这就是 HTML 编写者所做的).

I wrote a pandoc filter that when run on each individual chapter's file, inserts the title block as headings (level 1 for title, level 2 for author, level 3 for date. This is what the HTML writer does).

这让您可以单独在每一章上运行 pandoc(以生成 pandoc 输出加上格式化的标题块),然后在所有章节上运行 pandoc一起以编译单个文档.

This lets you run pandoc on each chapter individually (to produce the pandoc'd output plus the formatted title block), and then run pandoc on all the chapters together to compile the single document.

过滤器在这里的要点(我对故障代码等不承担任何责任):https://gist.github.com/mathematicalcoffee/e4f25350449e6004014f

The filter is here on gist (I take no responsibility for malfunctioning code, etc): https://gist.github.com/mathematicalcoffee/e4f25350449e6004014f

如果你希望它的格式不同,你可以修改它(例如,作者/日期出现在目录中,因为它们是标题,这不太正确......但这是一个不同的问题,因为它发生也使用默认的 HTML 编写器).

You could modify it if you wanted it to format differently (for example like this the author/date appear in the table of contents since they are headings, which is not quite right... but that's a different problem as it happens with the default HTML writer too).

我的工作流程现在是这样的:

My workflow is now something like this:

FORMAT=latex  # as understood by -t <format> in pandoc
FLAGS=--toc   # other flags for pandoc, --smart, etc
OUT=pdf       # output extension
for f in Chapter*.md; do \
    pandoc $FLAGS -t $FORMAT --filter ./chapter.hs $f; \
    echo ""; \
done | pandoc $FLAGS --standalone -o thesis.$OUT

我有 chmod +x Chapter.hs 并且它在当前目录中.

where I've chmod +x chapter.hs and it's in the current directory.

(我还有一个 title.txt,我将整个论文的标题块(而不是每章的标题块)突出显示在前面).

(I additionally have a title.txt that I stick out the front with the entire thesis' title block (as opposed to each chapter's title block)).

我从 pandoc 谷歌小组 很棒.

这篇关于使用标题栏将多个文件编译为一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆