1rick
1rick

Reputation: 51

Batch converting Markdown files with Markdown.pl

Referencing a previous answer, but still having difficulties converting my folder of .md files:

for i in src/*.md; do perl markdown/Markdown.pl --html4tags $i > output/${i%.*}.html; done; 

Unfortunately (for my test file "index.md")it's throwing the error:

line 11: output/src/index.html: No such file or directory

I'm not sure how to get it to direct output to just "output/index.html".

Any thoughts? (I'm not interested in using another soluton like pandoc, just trying to do this in bash)

Upvotes: 4

Views: 359

Answers (3)

Mark Setchell
Mark Setchell

Reputation: 208107

You can avoid the for loop, take advantage of modern multi-core CPUs, test what its going to do in advance without actually doing anything and get everything done in parallel with GNU Parallel like this:

parallel --dry-run perl markdown/Markdown.pl --html4tags {} \> output/{/.}.html ::: src/*md

Sample Output

perl markdown/Markdown.pl --html4tags src/a.md > output/a.html

If that looks correct, run it again but without the --dry-run to do it for real.

Upvotes: 1

user1934428
user1934428

Reputation: 22366

The error message means that the directory output/src, relative to the working directory in which the command is executed, does not exist. You can do a

mkdir -p output/src; for i in ....

Upvotes: 1

erik258
erik258

Reputation: 16302

The expansion of src/*.md will yield elements that all start with src/. You can remove the path of a file, yield only the filename sans directory, with dirname.

Since you're using the ${variable%match} replacement pattern to replace .md with .html, it would probably be easiest to create a new variable, here $j, to hold the results of basename.

for i in src/*.md; do j="$(basename $i)"; perl markdown/Markdown.pl --html4tags $i > output/${j%.*}.html; done; 

Upvotes: 1

Related Questions