如何从不同文件的 makefile 调用 pandoc?

How to call pandoc from makefile on different files?

我正在做一个写作项目,想在文件上使用 make 运行ning pandoc。到目前为止,我已经尝试像使用 bash 脚本一样将参数传递给 make

例如:

$ make chapter 2

在 make 文件中,chapter 是目标,2 是参数。

我不知道 makefile 是否可以使用 cli 参数。我没能在 documentation.

中找到我要找的东西

到目前为止,我已经尝试运行用这个食谱制作。

chapter:
    @pandoc -s -o ".epub" ".md"

我得到了这个错误

pandoc: .md: openBinaryFile: does not exist (No such file or directory)
make: *** [Makefile:2: chapter] Error 1

这是为了将我正在编写的一本书的部分内容转换成 epub。我愿意接受其他方法来做到这一点,因为令牌不起作用。

在 Make 中表达这一点的自然方式是将所有章节枚举为目标,通常作为 make all 的依赖项。

所以基本上

src := $(wildcard *.md)
epubs := $(patsubst %.md,%.epub,$(src))

.PHONY: all
all: $(epubs)

%.epub: %.md
    pandoc -s -o $@ $<

如果你有一章的来源是ch4.md,你可以说make ch4.epub。您不能真正传入不是文件名或目标名称的参数,并且这些参数不能包含空格。

我想你可以添加一个假的,比如

.PHONY: 2
2: ch2.epub

能够说出 make 2 并表示 make ch2.epub。如果文件名是这样系统命名的,你可以概括为

short := $(patsubst ch%.md,%,$(src))
.PHONY: $(short)
$(short): %: ch%.epub

不要在前面使用@,它只会让事情变得更难。如果您不想看到输出并且不想破坏您的 Makefile.

,您可以使用 make -s

In the make file chapter is the target and 2 would be the argument

$ make chapter num=2

make 命令行上对变量的赋值会覆盖 makefile 中的任何定义 (是的,这些变量实际上变成了只读的)。 这表明生成文件类似于:

num = $(error You must set $$num to the chapter number you want (make chapter num=4))

.PHONY: chapter
chapter:
    pandoc -s -o "${num}.epub" "${num}.md"

这是怎么回事? 好吧,如果你忘记设置 num, 当 make 扩展 chapter 的配方时 $(error) 将导致 make 停止。

$ make
Makefile:5: *** You must set $num to the chapter number you want (make chapter num=4).  Stop.

你原来的例子呢?

$ make chapter num=2
pandoc -s -o "2.epub" "2.md"

提示

  • 我很少推荐使用 @ 前缀 — 如果用户不想看到 [=50],可以使用 make-s =] 命令
  • 不要对 make 撒谎 — 特别是,您的规则不会生成名为 chapter 的文件,所以请告诉 make 通过标记目标 .PHONY