使用 make 翻译 reST 文件
我在 help_pages/
目录中有一堆文本文件,全部采用 reST 格式。我正在尝试向我的 GNU make Makefile
添加一些规则:
- 运行
help_pages
目录(和子目录)中扩展名为“.rst”的每个文件' 通过一个输出有效 html 文件的应用程序(现在我只是使用 rst2html )。 - 将这些输出 html 文件复制到目标目录,从而重现
help_pages
目录中存在的目录结构。
因此,例如, help_pages/some/dir/foo.rst
首先会被翻译为 help_pages/some/dir/foo.html
,然后复制到 public/help/some/dir/foo.html
。
我花了一整天的时间试图学习 Makefile 的复杂之处,但最终我放弃了。这是我已经拥有的:
# Directory that the .rst files reside in:
HELP_DIR=help_pages
# use 'find' to locate all such files:
HELP_SRCS=$(shell find $(HELP_DIR) -name "*.rst")
# work out what path the intermediate files would be
HELP_TARGETS=$(HELP_SRCS:.rst=.html)
# do the translation.
$(HELP_TARGETS): %.html: %.rst
rst2html $< $@
help: $(HELP_TARGETS)
.phony:
help
这种工作原理 - .rst 文件被转换为 .html 文件,但我不知道如何将它们安装到最终的目标目录中。如果我将 cp 命令添加到主构建配方中,如果目标目录中不存在这些文件,则不会重新安装这些文件。
理想情况下,我想要一个目标来制作帮助文件,一个目标来安装它们,第三个目标(称为“帮助”)依赖于这两个文件。我想这会给我最大的灵活性。
谁能给我一些关于如何实现这一目标的提示?
干杯,
I have a bunch of text files in the help_pages/
directory, all formatted in reST. I'm trying to add to my GNU make Makefile
a couple of rules that:
- Run each file in the
help_pages
directory (and sub-directories) with the extension '.rst' through an app that spits out a valid html file (right now I'm just usingrst2html
). - Copy those output html files to a target directory, reproducing the directory structure that existed in the
help_pages
directory.
So, for example, help_pages/some/dir/foo.rst
would first get translated to help_pages/some/dir/foo.html
, and then copied to public/help/some/dir/foo.html
.
I've spent all day trying to learn the intracacies of Makefiles, but in the end I gave up. Here's what I have already:
# Directory that the .rst files reside in:
HELP_DIR=help_pages
# use 'find' to locate all such files:
HELP_SRCS=$(shell find $(HELP_DIR) -name "*.rst")
# work out what path the intermediate files would be
HELP_TARGETS=$(HELP_SRCS:.rst=.html)
# do the translation.
$(HELP_TARGETS): %.html: %.rst
rst2html lt; $@
help: $(HELP_TARGETS)
.phony:
help
This kind-of works - the .rst files get translated to .html files, but I have no idea how to install them into the final, target directory. If I add a cp
command to the main build recipe, the files are not re-installed if they don't exist in the target directory.
Ideally I'd like a target to make the help files, and one to install them, and a third (called 'help') that depends on these two. I think this will give me the greatest flexibility.
Can anyone give me some tips on how to achieve this?
Cheers,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“我花了一整天的时间试图学习 Makefile 的复杂之处,但最终我放弃了。” 你不知道这有多有趣。
这应该可以做到:
现在,如果您想做所有事情,只需
make install
即可。或者,如果您想创建 html 文件而不将其复制到public/
中,只需make help
即可。您会发现依赖项可以正常工作。"I've spent all day trying to learn the intracacies of Makefiles, but in the end I gave up." You don't know how funny that is.
This should do it:
Now if you want to do everything, just
make install
. Or if you want to create the html files without copying them intopublic/
, justmake help
. You'll find that the dependencies work correctly.make
使用目录分隔符对路径名进行“智能”处理,这使得编写跨不同目录执行操作的规则变得非常痛苦。一种解决方法是将复制作为转换的副作用,例如添加一些内容作为第二个命令可能会起作用:
顺便说一句,
.PHONY
是大写的。make
has "smart" treatment of pathnames with directory separators which makes it a royal pain to write rules that do things across different directories.One workaround is to do the copying as a side effect of the conversion, e.g. adding something this as a second command might work:
BTW,
.PHONY
is in capitals.