2024-04-13 20:11:41 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2024-04-13 21:07:22 +00:00
|
|
|
# Throw error if required paths are not provided
|
|
|
|
if [ $# -ne 3 ]; then
|
|
|
|
echo "Usage: $0 <input_dir> <output_dir> <processed_dir>"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-04-13 23:31:19 +00:00
|
|
|
# Strip trailing slashes from input and output directories
|
2024-04-13 21:07:22 +00:00
|
|
|
input_dir="${1%/}"
|
|
|
|
output_dir="${2%/}"
|
|
|
|
processed_dir="${3%/}"
|
|
|
|
|
|
|
|
# Find all .md files and check if the array is empty
|
|
|
|
mapfile -t md_files < <(find "$input_dir" -type f -name "*.md")
|
|
|
|
if [ ${#md_files[@]} -eq 0 ]; then
|
|
|
|
echo "No Markdown files to process."
|
|
|
|
exit 0
|
|
|
|
fi
|
2024-04-13 20:11:41 +00:00
|
|
|
|
|
|
|
# Iterate over .md files in input directory
|
|
|
|
for file in "$input_dir"/*.md; do
|
|
|
|
file=$(basename "$file")
|
|
|
|
|
2024-04-13 23:31:19 +00:00
|
|
|
# Convert yucky line endings
|
|
|
|
dos2unix "$input_dir/$file"
|
|
|
|
|
2024-04-13 20:11:41 +00:00
|
|
|
# Extract frontmatter and dump it to YAML file
|
|
|
|
echo "id: \"urn:uuid:$(uuidgen)\"" > "$output_dir/${file%.md}.yaml"
|
|
|
|
awk '/^---$/{if(seen){exit}else{seen=1;next}} seen' "$input_dir/$file" >> "$output_dir/${file%.md}.yaml"
|
2024-04-13 21:07:22 +00:00
|
|
|
echo "Wrote frontmatter file: $output_dir/${file%.md}.yaml"
|
2024-04-13 20:11:41 +00:00
|
|
|
|
2024-04-13 21:07:22 +00:00
|
|
|
# Check for HTML tags
|
|
|
|
if grep -q '<[^>]*>' "$input_dir/$file"; then
|
|
|
|
# Remove HTML tags, including those which may span multiple lines
|
|
|
|
if awk -v RS='^$' -v ORS='' '{gsub(/<[^>]*>/, ""); print}' "$input_dir/$file" > "$input_dir/tmp.md"; then
|
|
|
|
mv "$input_dir/tmp.md" "$input_dir/$file"
|
|
|
|
fi
|
2024-04-13 20:11:41 +00:00
|
|
|
fi
|
|
|
|
|
2024-04-13 22:06:19 +00:00
|
|
|
# Remove empty/trailing spaces
|
|
|
|
sed -i 's/[ \t]*$//' "$input_dir/$file"
|
|
|
|
|
2024-04-13 20:11:41 +00:00
|
|
|
# Convert Markdown to GMI
|
|
|
|
/usr/local/bin/md2gmi -i "$input_dir/$file" -o "$output_dir/${file%.md}.gmi"
|
2024-04-13 21:07:22 +00:00
|
|
|
# Remove first line (and blank lines which follow) from output file (gempost will render the title)
|
|
|
|
if awk 'NR == 1 {next} NF == 0 && !p {next} {p=1; print}' "$output_dir/${file%.md}.gmi" > "$output_dir/tmp.gmi"; then
|
|
|
|
mv "$output_dir/tmp.gmi" "$output_dir/${file%.md}.gmi"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Wrote gemtext file: $output_dir/${file%.md}.gmi"
|
|
|
|
|
|
|
|
# Move original Markdown file to processed directory
|
|
|
|
mv "$input_dir/$file" "$processed_dir/$file"
|
2024-04-13 20:11:41 +00:00
|
|
|
done
|