Reputation: 412
I want to recursively convert all markdown files under ./src/
to html:
#!/bin/bash
function do_recursive_pandoc {
local markdown_src_file_extension=\*.markdown
local markdown_src_path="$1"
local html_output_path="$2"
mkdir "$html_output_path" 2>/dev/null
for i in $(find $markdown_src_path -name $markdown_src_file_extension 2>/dev/null | cut --delimiter='/' --fields=2- )
do
mkdir "$html_output_path"/$(dirname "$i") 2>/dev/null
pandoc -rmarkdown -whtml "$markdown_src_path"/"$i" --output="$html_output_path"/"$i".html
done
}
do_recursive_pandoc "src" "output"
But if I have space in file paths or names bash will count them as multiple items in for
loop, for example if I have:
./src/dir 1/foo.markdown
this script will make ./output/1
directory instead of making ./output/dir 1/
and tries converting ./src/dir
and ./src/1/foo.markdown
instead of ./src/dir 1/foo.markdown
How I can fix that?
Upvotes: 0
Views: 118
Reputation: 412
This will do the job:
#!/bin/bash
#############################################################################
function do_recursive_pandoc {
local src_path=$(realpath "$1")
local output_path=$(realpath "$2") && mkdir --parents "$output_path" 2>/dev/null
find "$src_path" -name "*.markdown" -not -path "$output_path" -exec bash -c '
i=$1
o="$3""${1#$2}"
mkdir --parents "$(dirname "$o")" 2>/dev/null
if [ -f "$i" ]; then pandoc -rmarkdown -whtml "$i" --output="$o".html ; fi
' " " {} $src_path $output_path \;
}
#############################################################################
do_recursive_pandoc "src" "output"
#############################################################################
I pasted it along with a basic CSS on Gist.
Upvotes: 0
Reputation: 4867
you would be better off piping find into while like this:
find $markdown_src_path -name $markdown_src_file_extension 2>/dev/null | cut --delimiter='/' --fields=2- | while read i; do
Upvotes: 1