用子目录的制作费
我正在写书。这些章节将写在Markdown(.MD)中,然后使用Pandoc转换为HTML和PDF(通过乳胶)版本。每章都有一些相关的Python脚本,这些脚本会生成一些图像,并且需要在构建本章之前运行。我正在尝试编写一个将所有章节汇编为这两种格式的makefile。
目前,该项目的结构如下:
project
|--- makefile
|--- chapters
| --- chapter1
| --- main.md
| --- genimage.py
| --- genanotherimage.py
| --- chapter2
|--- main.md
|--- otherimage.py
| --- output
| --- html
| --- chapter1.html
| --- chapter2.html
| --- pdf
| --- chapter1.pdf
| --- chapter2.pdf
我想键入“ MAKE Chapter1”(或类似),并刷新输出/HTML/Chapter1.html和Output/PDF/PDF/Chapther 1.pdf,重新运行所有内容。 PY脚本在相应的目录中是否已更改。理想情况下,我将有一个规则可以并行处理所有章节,而不是每个章节的规则。 (实际命令生成html/pdf是“ pandoc -o output/html/chapch1.html章节1/main.md”等等。)
我对Make and Meking并不十分熟悉,到目前为止,我的尝试一直没有成功。我无法设法在有多个文件要更新的地方实现目标,也没有设法使用模式来处理每个章节。如果这使事情变得更容易,我很乐意重组。
使用makefile是否可以使用此工作流程?我很高兴任何提示开始。我很茫然,甚至只知道在手册中查找的正确事物将非常有帮助。
I am working on a book. The chapters will be written in Markdown (.md), and then converted to both html and pdf (via LaTeX) versions using pandoc. Each chapter has a handful of associated Python scripts that generate some images and need to be run before the chapter is built. I am trying to write a makefile that will compile all the chapters to these two formats.
For now, the project is structured as follows:
project
|--- makefile
|--- chapters
| --- chapter1
| --- main.md
| --- genimage.py
| --- genanotherimage.py
| --- chapter2
|--- main.md
|--- otherimage.py
| --- output
| --- html
| --- chapter1.html
| --- chapter2.html
| --- pdf
| --- chapter1.pdf
| --- chapter2.pdf
I would like to type "make chapter1" (or similar) and have it refresh both output/html/chapter1.html and output/pdf/chapter1.pdf, re-running all the .py scripts in the corresponding directory if they have changed. Ideally I would have one rule that handles all the chapters in parallel rather than a separate one for each one. (The actual command generate the html/pdf is "pandoc -o output/html/chapter1.html chapter1/main.md" and so on.)
I am not very familiar with make and my attempts so far have been very unsuccessful. I can't manage to make a target where there are multiple files to update, and I have not managed to use patterns to handle each chapter with a single rule. I am happy to reorganize somewhat if it makes things easier.
Is this workflow possible with a makefile? I am grateful for any hints to get started; I'm at a loss and even just knowing the right things to look up in the manual would be very helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下基于您显示的项目树并假设 GNU make。它还假设您必须从项目的顶级目录运行
pandoc
和 python 脚本。模式规则可能会有所帮助:主要的微妙之处在于,当 GNU make 遇到具有多个目标的模式规则时,它会认为配方的一次执行会构建所有目标。在我们的例子中,HTML 和 PDF 输出是通过相同的配方执行生成的。
这并不是 100% 完美,因为如果您修改或添加 python 脚本,章节将不会被重建。如果您也需要这个,我们将需要更复杂的 GNU make 功能,例如 二次扩展或
评估
。二次扩展示例:
要了解为什么
$$(wildcard Chapters/$$*/*.py)
中的$$
,请参阅 GNU make 手册。eval
示例:要了解为什么
$$
或$$$$
,请参阅 GNU make 手册。The following is based on the project tree you show and assumes GNU make. It also assumes that you must run
pandoc
and your python scripts from the top level directory of the project. Pattern rules can probably help:The main subtlety is that when GNU make encounters a pattern rule with several targets it considers that one single execution of the recipe builds all targets. In our case the HTML and PDF outputs are produced by the same execution of the recipe.
This is not 100% perfect because a chapter will not be rebuilt if you modify or add python scripts. If you also need this we will need more sophisticated GNU make features like secondary expansion or
eval
.Example with secondary expansion:
To understand why
$$
in$$(wildcard chapters/$$*/*.py)
see the GNU make manual.Example with
eval
:To understand why
$$
or$$$$
see the GNU make manual.