在Mac上使用Pandoc进行递归目录分析 [英] Recursive directory parsing with Pandoc on Mac

查看:110
本文介绍了在Mac上使用Pandoc进行递归目录分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了这个问题,它回答了使用Pandoc执行批量转换的问题,但它没有回答如何使其递归的问题。我预先规定我不是程序员,所以我在这里寻求一些帮助。

Pandoc文档对于将批量文件传递给可执行文件的细节很少,并且基于脚本,它看起来像Pandoc本身不能解析多个文件一次。下面的脚本在Mac OS X中工作得很好,但只处理本地目录中的文件并将结果输出到同一位置。


查找。 -name \ * .md -type f -exec pandoc -o {} .txt {} \;



我使用下面的代码来得到我期待的结果:




查找。 -name \ * .html -type f -exec pandoc -o {} .markdown {} \;



这个简单的脚本,使用安装在Mac OS X 10.7.4上的Pandoc运行,将转换我运行的目录中的所有匹配文件它进行降价并将它们保存在同一目录中。例如,如果我有一个名为 apps.html 的文件,它会将该文件转换为与源文件相同的目录中的 apps.html.markdown

虽然我很高兴能够进行转换,但速度很快,我需要它来处理位于一个目录中的所有文件,并将减价版本放在一组镜像目录进行编辑。最终,这些目录位于Github存储库中。一个分支用于编辑,而另一个分支用于生产/发布。另外,这个简单的脚本保留了原来的扩展名,并附加了新的扩展名。如果我再次进行转换,它将在标记扩展后添加HTML扩展,并且文件大小只会增长并增长。从技术上讲,我所需要做的就是解析一个分支目录并将其与生产目录同步,然后当所有更改,删除和新内容都得到验证时正确的,我可以运行提交发布更改。它看起来像查找命令可以处理所有这些,但我不知道如何正确配置它,即使在阅读Mac OS X和Ubuntu手册页之后。

任何种类的智慧都会深受赞赏。

TC

解决方案

创建以下 Makefile

  TXTDIR =来源
HTMLS = $(通配符* .html)
$ MDS = $(patsubst%.html,$(TXTDIR)/%。markdown,$(HTMLS))

.PHONY:全部

全部:$( MDS)

$(TXTDIR):
mkdir $(TXTDIR)

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

(注意:缩进行必须以TAB开头 - 这可能不会出现通过在上面,因为减价通常会剥离标签)。

然后,您只需键入'make',它将在每个带有.html文件的文件上运行pandoc在工作目录中进行扩展,在来源中生成降价版本。这种方法比使用'find'的好处在于,它只会对自上次运行以来发生更改的文件运行pandoc。


I found this question which had an answer to the question of performing batch conversions with Pandoc, but it doesn't answer the question of how to make it recursive. I stipulate up front that I'm not a programmer, so I'm seeking some help on this here.

The Pandoc documentation is slim on details regarding passing batches of files to the executable, and based on the script it looks like Pandoc itself is not capable of parsing more than a single file at a time. The script below works just fine in Mac OS X, but only processes the files in the local directory and outputs the results in the same place.

find . -name \*.md -type f -exec pandoc -o {}.txt {} \;

I used the following code to get something of the result I was hoping for:

find . -name \*.html -type f -exec pandoc -o {}.markdown {} \;

This simple script, run using Pandoc installed on Mac OS X 10.7.4 converts all matching files in the directory I run it in to markdown and saves them in the same directory. For example, if I had a file named apps.html, it would convert that file to apps.html.markdown in the same directory as the source files.

While I'm pleased that it makes the conversion, and it's fast, I need it to process all files located in one directory and put the markdown versions in a set of mirrored directories for editing. Ultimately, these directories are in Github repositories. One branch is for editing while another branch is for production/publishing. In addition, this simple script is retaining the original extension and appending the new extension to it. If I convert back again, it will add the HTML extension after the markdown extension, and the file size would just grow and grow.

Technically, all I need to do is be able to parse one branches directory and sync it with the production one, then when all changed, removed, and new content is verified correct, I can run commits to publish the changes. It looks like the Find command can handle all of this, but I just have no clue as to how to properly configure it, even after reading the Mac OS X and Ubuntu man pages.

Any kind words of wisdom would be deeply appreciated.

TC

解决方案

Create 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 $@

(Note: The indented lines must begin with a TAB -- this may not come through in the above, since markdown usually strips out tabs.)

Then you just need to type 'make', and it will run pandoc on every file with a .html extension in the working directory, producing a markdown version in 'sources'. An advantage of this method over using 'find' is that it will only run pandoc on a file that has changed since it was last run.

这篇关于在Mac上使用Pandoc进行递归目录分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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