在Mac上使用Pandoc将文件夹中的所有文件转换为md [英] Converting all files in a folder to md using pandoc on Mac

查看:451
本文介绍了在Mac上使用Pandoc将文件夹中的所有文件转换为md的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将整个目录从html转换为markdown.目录树很高,因此有些文件向下嵌套了两层和三层.

I am trying to convert an entire directory from html into markdown. The directory tree is quite tall, so there are files nested two and three levels down.

John MacFarlane在回答此问题时,建议使用以下Makefile:

In answering this question, John MacFarlane suggested using the following Makefile:

TXTDIR=sources
HTMLS=$(wildcard *.html)
MDS=$(patsubst %.html,$(TXTDIR)/%.markdown, $(HTMLS))

.PHONY : all

all : $(MDS)

$(TXTDIR) :
    mkdir $(TXTDIR)

$(TXTDIR)/%.markdown : %.html $(TXTDIR)
    pandoc -f html -t markdown -s $< -o $@

现在,这似乎不在子目录内.有没有简单的方法可以修改它以便处理整个树?

Now, this doesn't seem to go inside subdirectories. Is there any easy way to modify this so that it will process the entire tree?

我不需要这个在make中.我要寻找的只是一种获取初始目录的镜像的方法,其中每个html文件都由在该文件上运行pandoc的输出所代替.

I don't need this to be in make. All I'm looking for is a way of getting a mirror of the initial directory where each html file is replaced by the output of running pandoc on that file.

(我怀疑这些行中的内容应该会有所帮助,但我距离确信如果自己尝试自己不会破坏事情.GNU make的时候我是文盲.)

(I suspect something along these lines should help, but I'm far from confident that I won't break things if I try to go at it on my own. I'm illiterate when it comes to GNU make).)

推荐答案

由于您提到您不介意使用make ,因此可以尝试bash.

Since you mentioned you don't mind not using make, you can try bash.

我修改了此 answer 中的代码,并在父目录中使用:

I modified the code from this answer, use in the parent directory:

find ./ -iname "*.md" -type f -exec sh -c 'pandoc "${0}" -o "${0%.md}.pdf"' {} \;

它在我测试时就可以使用,因此它应该对您有用.

It worked when I tested it, so it should work for you.

根据请求有什么想法如何指定输出文件夹?(使用html作为原始文件,使用md作为输出):

As per the request Any ideas how to specify the output folder? (Using html as the original file and md as the output):

find ./ -iname "*.html" -type f -exec sh -c 'pandoc "${0}" -o "./output/$(basename ${0%.html}.md)"' {} \;

我已经对此进行了测试,并且对我有用.

I have tested this and it works for me.

根据注释,{} \;find-exec选项一起使用时,或多或少地用作文件名应位于的占位符.像在其中一样,展开发现放置在命令中的文件名. \;结束-exec.有关更多说明,请参见此处.

As per a comment, the {} \; when used with find and the -exec option is used as a, more or less, placeholder for where the filename should be. As in it expands the filenames found to be placed in the command. The \; ends the -exec. See here for more explanation.

这篇关于在Mac上使用Pandoc将文件夹中的所有文件转换为md的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆