pandoc使每个目录成为一章

发布于 2025-01-18 09:16:57 字数 193 浏览 1 评论 0原文

我在不同的目录中有很多 markdown 文件,每个文件都具有相同的格式(# 标题,然后 ## 副标题)。

我可以使 --toc 尊重文件夹布局吗,因为文件夹本身就是章节的名称,每个 markdown 文件都是本章的内容。

到目前为止,pandoc 完全忽略了我的文件夹名称,它的工作原理与将所有 Markdown 文件放在同一个文件夹中相同。

I have a lot of markdown files in various directories each with the same format (# title, then ## sub-title).

can I make the --toc respect the folder layout, in that the folder itself is the name of chapter, and each markdown file is content of this chapter.

so far pandoc totally ignores my folder names, it works the same as putting all the markdown files within the same folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一曲爱恨情仇 2025-01-25 09:16:57

我的方法是在每个文件夹中创建具有一级标题的索引文件,并将其他文件中的标题降级一级。

我使用 Git,默认情况下我使用默认结构,在文件中具有一级标题,但是当我想使用 pandoc 生成电子书时,我通过自动 Linux shell 脚本修改文件。之后,我通过 Git 恢复更改的文件。

这是脚本:

find ./docs/*/ -name "*.md" ! -name "*index.md" -exec perl -pi -e "s/^(#)+\s/#
amp;/g" {} \;

./docs/*/ 表示我只查找 docs 目录的子文件夹内的文件,例如 docs/foo/file1.md,<代码>docs/bar/file2.md。

我也只对 *.md 文件感兴趣,不包括 *index.md 文件。

index.md 文件(我通常将其命名为 00-index.md 以使它们显示在第一个)中,我放置了一个一级标题 # 并且由于这些文件被排除在脚本的 find 部分之外,因此它们的标题不会降级。

接下来,有一个 Perl 的搜索和替换命令,使用正则表达式 s/^(#)+\s/#$&/g 查找从一个或多个 #< 开始的所有行。 /code> 并向其中添加另一个 #

最后,我使用 --toc-depth=2 运行 pandoc,因此目录仅包含第一级和第二级标题。

pandoc ./docs/**/*.md --verbose --fail-if-warnings --toc-depth=2 --table-of-contents -o ./ebook.epub

为了恢复对文件所做的所有更改,我恢复了 Git 存储库中的更改。

git restore .

My approach to this is to create index files in each folder with first level heading and downgrade headings in other files by one level.

I use Git and by default I'm using default structure, having first level headings in files, but when I want to generate ebook using pandoc I'm modifying files via automated Linux shell script. After that, I revert changed files via Git.

Here's the script:

find ./docs/*/ -name "*.md" ! -name "*index.md" -exec perl -pi -e "s/^(#)+\s/#
amp;/g" {} \;

./docs/*/ means I'm looking only for files inside subfolders of docs directory like docs/foo/file1.md, docs/bar/file2.md.

I'm also interested only in *.md files, excluding *index.md files.

In index.md files (that I name usually 00-index.md to make them appear as first), I put a first level heading # and because those files are excluded from find portion of the script, their headings aren't downgraded.

Next, there's a perl's search and replace command with regular expression s/^(#)+\s/#$&/g that looks for all lines starting from one or more # and adds another # to them.

In the end, I'm running pandoc with --toc-depth=2 so the table of content contains only first and second level headings.

pandoc ./docs/**/*.md --verbose --fail-if-warnings --toc-depth=2 --table-of-contents -o ./ebook.epub

To revert all changes made to files, I restore changes in the Git repo.

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