Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
142 lines (112 sloc)
3.8 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| shopt -s nullglob | |
| TMP_DIR="/tmp/obsidian-export" | |
| mkdir -p "$TMP_DIR" | |
| rm -rf "$TMP_DIR"/* | |
| echo-title() { | |
| echo ---------------- $@ ----------------- | |
| } | |
| join_arr_by() { | |
| local d=$1 f=$2 | |
| if shift 2; then | |
| printf %s "$f" "${@/#/$d}" | |
| fi | |
| } | |
| get_file_containing_tags() { | |
| # use `files` from argv or all the vault files | |
| (( ${#files[@]} )) && | |
| vault_md_files=("${files[@]}") || | |
| readarray -t vault_md_files < <(ls "$vault"/*.md) | |
| files=() | |
| # prepend "#" to all tags and join them by "|" | |
| grep_pattern="$(join_arr_by '\|' ${tags[@]/#/#})" | |
| for file in "${vault_md_files[@]}"; do | |
| if grep "$grep_pattern" "$file" &> /dev/null; then | |
| files+=("$file") | |
| fi | |
| done | |
| echo-title 'exporting these files?' | |
| printf "%s\n" "${files[@]}" | |
| read -p "continue ? ([Y]/n): " ans | |
| local ans="$(echo $ans | tr '[:upper:]' '[:lower:]')" | |
| [ "$ans" = n ] && exit 1 | |
| } | |
| collect-args() { | |
| vault="." # default to current directory | |
| out_dir="." # default to current directory | |
| while (($#)); do | |
| arg="$1"; shift | |
| [ "$arg" = "--vault" ] && vault="$1" && shift && continue | |
| [ "$arg" = "--out-dir" ] && out_dir="$1" && shift && continue | |
| [ "$arg" = "-o" ] && out_dir="$1" && shift && continue | |
| [ "$arg" = "--tags" ] && tags="$1" && shift && continue | |
| [ "$arg" = "-t" ] && tags="$1" && shift && continue | |
| [ "$arg" = "--md" ] && is_md_only=1 && continue | |
| [ "$arg" = "--md" ] && is_md_only=1 && continue | |
| [ "$arg" = "--cat" ] && is_concat=1 && continue | |
| [ "$arg" = "--fnah" ] && is_file_name_as_header=1 && continue | |
| [ "$arg" = "--file-name-as-header" ] && is_file_name_as_header=1 && continue | |
| files+=("$arg") # fallback, append to the files | |
| done | |
| vault="$(realpath "$vault")" | |
| out_dir="$(realpath "$out_dir")" | |
| # read the Attachments dir from obsidian vault settings | |
| attachments="$vault/$( | |
| sed -e '/attachmentFolderPath/ !d' "$vault"/.obsidian/app.json | | |
| grep -oP '\s*"attachmentFolderPath": \K"(.*?)"' | |
| )" | |
| # remove quotations "" | |
| attachments="$(eval "echo $attachments")" | |
| # convert comma delimited values into array | |
| readarray -d, -t tags <<< "$tags" | |
| [ "$tags" ] && get_file_containing_tags | |
| [ ${#files[@]} = 0 ] && echo ERROR: no file to export && exit 1 | |
| [ -f "$out_dir" ] && echo ERROR: output dir is a file, choose another place && exit 1 | |
| [ -d "$out_dir" ] || mkdir -p "$out_dir" | |
| } | |
| prepare-file() { | |
| # TODO: use the captian after "|" symbol | |
| # replace `![[image-name.png]]` to `` | |
| local output="$(sed -r "s:^!\[\[(.*)\]\]::")" | |
| # increase the level due to adding the file name as a `# header` | |
| [ $is_file_name_as_header ] && | |
| local output="$(echo "$output" | sed -r "s/^#+ (.*)/#\0/")" | |
| echo "$output" | |
| } | |
| convert-to-pure-markdown() { | |
| for file in "${files[@]}"; do | |
| file_base="$(basename "$file")" | |
| [ $is_concat ] && | |
| prepared_md_file="$TMP_DIR/concated_file.pure.md" || # FIXME: this may overwrite an existing file | |
| prepared_md_file="$TMP_DIR/${file_base%.md}.pure.md" | |
| [ $is_file_name_as_header ] && | |
| echo "# ${file_base%.md}" >> "$prepared_md_file" | |
| echo >> "$prepared_md_file" | |
| cat "$file" | prepare-file >> "$prepared_md_file" | |
| echo >> "$prepared_md_file" | |
| echo >> "$prepared_md_file" | |
| done | |
| } | |
| convert-to-pdf() { | |
| echo-title exporting to pdf | |
| for file in "$TMP_DIR"/*.md; do | |
| out_file="${file%.pure.md}.pdf" | |
| echo exporting 📤: $(basename "$out_file") | |
| pandoc \ | |
| --citeproc --toc --number-sections --pdf-engine=xelatex \ | |
| -f markdown "$file" -o "$out_file" | |
| done | |
| } | |
| copy-to-out-dir() { | |
| if [ "$is_md_only" ]; then | |
| mv "$TMP_DIR"/*.md "$out_dir" | |
| else | |
| mv "$TMP_DIR"/*.pdf "$out_dir" | |
| fi | |
| } | |
| collect-args "$@" | |
| convert-to-pure-markdown | |
| [ ! "$is_md_only" ] && | |
| convert-to-pdf | |
| copy-to-out-dir | |
| echo DONE 📑 |