使用 make 翻译 reST 文件

发布于 2024-10-10 17:59:43 字数 1126 浏览 3 评论 0原文

我在 help_pages/ 目录中有一堆文本文件,全部采用 reST 格式。我正在尝试向我的 GNU make Makefile 添加一些规则:

  1. 运行 help_pages 目录(和子目录)中扩展名为“.rst”的每个文件' 通过一个输出有效 html 文件的应用程序(现在我只是使用 rst2html )。
  2. 将这些输出 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:

  1. 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 using rst2html).
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾`听者〃 2024-10-17 17:59:43

“我花了一整天的时间试图学习 Makefile 的复杂之处,但最终我放弃了。” 你不知道这有多有趣。

这应该可以做到:

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)
# and the destinations
HELP_INSTALL = $(patsubst help_pages%,public%,$(HELP_TARGETS))

# do the translation.
$(HELP_TARGETS): %.html: %.rst
    rst2html 
lt; $@          

# do the installation
$(HELP_INSTALL):public/%:help_pages/%
    cp 
lt; $@

.PHONY: help install

help: $(HELP_TARGETS)

install:$(HELP_INSTALL)

现在,如果您想做所有事情,只需 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:

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)
# and the destinations
HELP_INSTALL = $(patsubst help_pages%,public%,$(HELP_TARGETS))

# do the translation.
$(HELP_TARGETS): %.html: %.rst
    rst2html 
lt; $@          

# do the installation
$(HELP_INSTALL):public/%:help_pages/%
    cp 
lt; $@

.PHONY: help install

help: $(HELP_TARGETS)

install:$(HELP_INSTALL)

Now if you want to do everything, just make install. Or if you want to create the html files without copying them into public/, just make help. You'll find that the dependencies work correctly.

百善笑为先 2024-10-17 17:59:43

make 使用目录分隔符对路径名进行“智能”处理,这使得编写跨不同目录执行操作的规则变得非常痛苦。

一种解决方法是将复制作为转换的副作用,例如添加一些内容作为第二个命令可能会起作用:

echo $@ | cpio -pdl public/html

顺便说一句,.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:

echo $@ | cpio -pdl public/html

BTW, .PHONY is in capitals.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文