Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting all files in a folder to md using pandoc on Mac

Tags:

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.

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?

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.

(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).)

like image 744
apc Avatar asked Sep 30 '14 17:09

apc


People also ask

How do I convert Word to markdown in pandoc?

As you can see, first, write pandoc to execute any pandoc command, and then write the file name of a file with extension which you want to convert to a markdown file. The -o stands for output file name and provide name of the file with extension to which it should be converted.

Can pandoc convert PDF to Word?

You can use the program pandoc on the SCF Linux and Mac machines (via the terminal window) to convert from formats such as HTML, LaTeX and Markdown to formats such as HTML, LaTeX, Word, OpenOffice, and PDF, among others.

Can pandoc read PDF?

You can't. You can try opening the PDF in Word or Google Docs and saving in a format from which pandoc can convert directly.


1 Answers

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

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.

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.

Edit: 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.

like image 155
Luke W. Johnston Avatar answered Oct 12 '22 02:10

Luke W. Johnston