1
0
mirror of https://github.com/RyanGreenup/cadmus.git synced 2025-04-09 04:11:51 +02:00
cadmus/bin/cadmus
2020-07-17 00:05:03 +10:00

434 lines
18 KiB
Bash
Executable File

#! /usr/bin/env bash
readonly NOTES_DIR="$HOME/Notes/MD/notes" ## TODO Global Variables are bad
readonly RECOLL_CONFIG_DIR="$HOME/.cadmus"
# Author: Ryan Greenup <ryan.greenup@protonmail.com>
# abort on nonzero exitstatus
set -o errexit
# abort on unbound variable
set -o nounset
# don't hide errors within pipes
set -o pipefail
# * Main Functions
main() {
setvars
[[ -z "${1:-}" ]] && mainHelp && exit 0
arguments "${@:-}"
}
function setvars() {
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
readonly script_name=$(basename "${0}")
readonly script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
readonly TERMINAL="kitty"
readonly TERMINAL_EXEC='kitty -- '
}
# * Sub-functions
# ** Help
function mainHelp() {
echo
echo -e " \e[3m\e[1m Cadmus\e[0m; Helpful Shell Scripts for Markdown Notes"
echo -e " \e[1;31m -------------------------\e[0m "
echo
echo -e " \e[1;91m \e[1m Command \e[0m\e[0m \e[1;34m┊┊┊ \e[0m Description "
echo -e " ..............\e[1;34m┊┊┊\e[0m........................................... "
echo -e " 🔍 \e[1;93m find \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Find Notes based on FileName"
echo -e " 🔎 \e[1;32m search \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Search through Notes using Recoll"
echo -e " 🏷 \e[1;33m tags \e[0m \e[1;34m ┊┊┊ 📁\e[0m Use TMSU to work with tags"
echo -e " 🔧 \e[1;34m tools \e[0m \e[1;34m ┊┊┊ \e[0m Tools for Editing"
echo -e " 📝 \e[1;35m export \e[0m \e[1;34m ┊┊┊ \e[0m Export Notes to Different Formats "
echo -e " ⎋ \e[1;36m convert \e[0m \e[1;34m ┊┊┊ \e[0m Convert Clipboard Contents to Different Formats "
echo -e " 🧰 \e[1;37m misc \e[0m \e[1;34m ┊┊┊ \e[0m Miscelanneous Tools nice to have on hand "
echo -e " 🌏\e[1;92m publish\e[0m \e[1;34m ┊┊┊ \e[0m Publish with \e[1;34m \e[4m\e[3mMkDocs\e[0m\e[0m🐍"
echo -e " 🕮 \e[1;93m preview \e[0m \e[1;34m ┊┊┊ \e[0m Preview with \e[1;34m \e[4m\e[3mMarkServ\e[0m\e[0m "
echo -e " 🕮 \e[1;94m help \e[0m \e[1;34m ┊┊┊ \e[0m Open help for correspoding functions "
echo
echo -e " \e[3m\e[1m• Legend\e[0m "
echo
echo -e " 🎆: Executes a command, a Suffixed -h argument will print help"
echo -e " 📁: Prints another Menu"
echo
}
# ** Main Arguments - Level 0
# *** Switch
arguments () {
while test $# -gt 0
do
case "$1" in
--help) mainHelp && exit 0
;;
-h) mainHelp && exit 0
;;
find) shift; NoteFind "${NOTES_DIR}" "${@:-}" ## Don't steal function name
;;
search) shift; NoteSearch "${NOTES_DIR}" ${@:-}
;;
tags) shift; CadmusTags ${@:-}
;;
tools) shift; CadmusTools "${@:-}"
;;
export) shift; CadmusExport "${@:-}"
;;
convert) shift; CadmusConvert "${@:-}"
;;
misc) echo "begin misc"
;;
publish) echo "begin publish"
;;
preview) echo "begin preview"
;;
help) subHelp && exit 0
;;
--*) >&2 echo "bad option $1"
;;
*) >&2 echo -e "argument \e[1;35m${1}\e[0m has no definition."
;;
esac
shift
done
}
# *** Find
function NoteFind() {
COMMAND="NoteFind.sh"
echo "Running "${COMMAND}" "${1}""
FILES="$("${COMMAND}" "${@:-}")"
## Only try and open something that is non-empty
if [ "$FILES" != "" ]; then
## xdg-open doesn't accept multiple arguments, so create commands and
## pipe them back into bash This opens all files in the same tab with
## VSCode and Atom The individual selections can also be passed off with
## the keybindings to emacsclient.
# Using XDG-OPEN
# echo "${FILES}" | sed s/^/xdg-open\ \"/ | sed s/$/\"/ | bash
echo "${FILES}" | sed -e s+^+\"+ -e s+$+\"+ | xargs -n 1 xdg-open
# # Getting the .desktop file and cutting off the Extension
# dex "$(xdg-mime query default text/markdown)" $FILES
# dex "$(xdg-mime query default text/markdown)" $FILES
## This might break if the .desktop file doesn't have the same
## name as the binary.
## This appears to be a standard though
## https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#file-naming
fi
}
# *** Search
function NoteSearch() {
COMMAND="NoteRecollSearch.sh"
echo "Running "${COMMAND}" "${1}""
FILES="$("${COMMAND}" "${@:-}")"
## Only try and open something that is non-empty
if [ "$FILES" != "" ]; then
echo "${FILES}" | sed -e s+^+\"+ -e s+$+\"+ | xargs -n 1 xdg-open
fi
}
# *** Tags
CadmusTags () {
[[ -z "${1:-}" ]] && TagsHelp && exit 0
while test $# -gt 0
do
case "$1" in
filter) shift; "${script_dir}/tags/FilterNotesByTMSUTag.sh" "${NOTES_DIR}" "${@:-}"
;;
create) shift; "${script_dir}/tags/tags-to-TMSU.sh" "${NOTES_DIR}" ${@:-} && exit 0
./
;;
--*) >&2 echo "bad option $1"
;;
*) >&2 echo -e "argument \e[1;35m${1}\e[0m has no definition."
;;
esac
shift
done
}
# **** Help
function TagsHelp() {
echo
echo -e " \e[3m\e[1m Cadmus Tags\e[0m; Filter and Create TMSU Tags"
echo -e " \e[1;31m -------------------------\e[0m "
echo
echo -e " \e[1;91m \e[1m Command \e[0m\e[0m \e[1;34m┊┊┊ \e[0m Description "
echo -e " ..............\e[1;34m┊┊┊\e[0m........................................... "
echo -e " \e[1;93m filter \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Find Notes based on FileName"
echo -e " \e[1;32m create \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Create Tags with TMSU"
echo
echo -e " \e[3m\e[1m• Notes\e[0m "
echo
echo -e " Tags are very much so a work in progress, the relevant scripts are:"
echo -e " • FilterNotesByTMSUTag.sh "
echo -e " • tags-to-TMSU.sh "
echo
echo -e " There very much an assumption that there is a .tmsu folder "
echo -e " somewhere above the NOTES_DIR "
echo -e " (in this case that is "${NOTES_DIR}")"
}
# *** Tools
CadmusTools () {
[[ -z "${1:-}" ]] && ToolsHelp && exit 0
while test $# -gt 0
do
case "$1" in
webtitle) shift; "${script_dir}/tools/PrintWebTitle.sh"
;;
backlinks) shift; "${script_dir}/tools/List-Backlinks.sh" "${NOTES_DIR}" ${@:-} && exit 0
;;
link) shift; "${script_dir}/tools/LinkMarkdownNotes.sh" "${NOTES_DIR}" | xclip -selection clipboard && exit 0
;;
fix) shift; "${script_dir}/tools/fixLink.sh" "${NOTES_DIR}" && exit 0
;;
page-import) shift; xclip -o -selection clipboard | xargs curl | pandoc -f html -t markdown_github+tex_math_dollars --atx-headers | xclip -selection clipboard && exit 0
;;
random) shift; find "${NOTES_DIR}" -name '*.md' | shuf -n 1 | xargs xdg-open && exit 0
;;
--*) >&2 echo "bad option $1"
;;
*) >&2 echo -e "argument \e[1;35m${1}\e[0m has no definition."
;;
esac
shift
done
}
# **** Help
function ToolsHelp() {
echo
echo -e " \e[3m\e[1m Cadmus Tools\e[0m; Tools for Editing Notes "
echo -e " \e[1;31m -------------------------\e[0m "
echo
echo -e " \e[1;91m \e[1m Command \e[0m\e[0m \e[1;34m┊┊┊ \e[0m Description "
echo -e " ..................\e[1;34m┊┊┊\e[0m........................................... "
echo -e " \e[1;93m webtitle \e[0m \e[1;34m ┊┊┊ \e[0m✀ Transforms the Clipboard 📋 to a Link"
echo -e " \e[1;93m backlinks \e[0m \e[1;34m ┊┊┊ \e[0m✀ Takes the Abs Path of a Note from the Clipboard 📋"
echo -e " \e[1;93m \e[0m \e[1;34m ┊┊┊ \e[0m and prints out backlinks (Abs Path)"
echo -e " \e[1;93m fix \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Fix the relative path in the clipboard 📋 "
echo -e " \e[1;93m link \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Generate a link to another note from the current in the clipboard"
echo -e " \e[1;32m page-import \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Transform Clipboard from URL to corresponding Markdown"
echo -e " \e[1;32m random \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Open a Random Note in the Default program"
echo -e " \e[1;32m odt \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Export the Clipboard into an ODT File."
echo
echo -e " \e[3m\e[1m• Legend\e[0m "
echo -e " ✀ Works with the clipboard for input/output."
echo
}
# *** Export
CadmusExport () {
[[ -z "${1:-}" ]] && ExportHelp && exit 0
while test $# -gt 0
do
case "$1" in
odt) shift; pandocExport odt && exit 0
;;
docx) shift; pandocExport docx && exit 0
;;
org) shift; pandocExport org && exit 0
;;
md) shift; pandocExport md && exit 0
;;
pdf) shift; pandocExport pdf --pdf-engine=xelatex && exit 0
;;
tex) shift; pandocExport tex && exit 0
;;
tectonic) shift; pandocExport pdf --pdf-engine=tectonic && exit 0
;;
weasyprint) shift; pandocExport pdf --pdf-engine=weasyprint && exit 0
;;
html) shift; pandocExport html --mathml --self-contained -c "${script_dir}"'/resources/pandoc.css' && exit 0
;;
html-dir) shift; pandocExport html --katex -c "${script_dir}"'/resources/pandoc.css' && exit 0
;;
page-import) shift; xclip -o -selection clipboard | xargs curl | pandoc -f html -t markdown_github+tex_math_dollars --atx-headers | xclip -selection clipboard && exit 0
;;
--*) >&2 echo "bad option $1"
;;
*) >&2 echo -e "argument \e[1;35m${1}\e[0m has no definition."
;;
esac
shift
done
}
# **** Pandoc Export
pandocExport () { # $1 is extension; $2+ are options for pandoc
echo "If the file path is in the clipboard press y to continue"
echo " Press any key to choose a note to export"
read -d '' -s -n1 pathInClipQ
if [ "$pathInClipQ" == "y" ]; then
FILEPATH="$(xclip -selection clipboard -o)"
else
FILEPATH=$("${script_dir}/NoteFind.sh" "${NOTES_DIR}")
fi
[[ -f "${FILEPATH}" ]] && FILE="$(basename ${FILEPATH})" || exit 0
FILEOUT="$(echo ${FILE} | cut -f 1 -d '.').${1}"
shift
cd "$(echo "${FILEPATH}" | xargs realpath | xargs dirname)"
OUTDIR="$(mktemp -d /tmp/cadmusExportXXX)"
echo
echo -e " \e[1;94m Calling pandoc from: \e[1;32m $(pwd) \e[0m"
echo -e " \e[1;94m Output to: \e[1;32m "${OUTDIR}"/"${FILEOUT}"\e[0m"
echo
echo -e " \e[1;94m Performing: \e[0m"
echo " pandoc -s "${FILE}" ${@:-} --extract-media="${OUTDIR}/media" -o "${OUTDIR}"/"${FILEOUT} | highlight --syntax bash -O ansi
echo
pandoc -s "${FILE}" ${@:-} --extract-media="${OUTDIR}/media" -o "${OUTDIR}"/"${FILEOUT}" || exit 7
xdg-open "${OUTDIR}"/"${FILEOUT}"
}
# **** Help
function ExportHelp () {
echo
echo -e " \e[3m\e[1m Cadmus Export\e[0m; Tools for Editing Notes "
echo -e " \e[1;31m -------------------------\e[0m "
echo
echo -e " \e[1;91m \e[1m Command \e[0m\e[0m \e[1;34m┊┊┊ \e[0m Description "
echo -e " ...............\e[1;34m┊┊┊\e[0m........................................... "
echo -e " \e[1;32m odt \e[0m \e[1;34m ┊┊┊ \e[0mExport a Note to ODT (no syntax highlighting)"
echo -e " \e[1;32m docx \e[0m \e[1;34m ┊┊┊ \e[0mExport a Note to .docx"
echo -e " \e[1;32m pdf \e[0m \e[1;34m ┊┊┊\e[0m Export a Note to PDF with LaTeX"
echo -e " \e[1;32m tex \e[0m \e[1;34m ┊┊┊\e[0m Export a Note to LaTeX"
echo -e " \e[1;32m tectonic \e[0m \e[1;34m ┊┊┊\e[0m Export with Tectonic (Slow)"
echo -e " \e[1;32m weasyprint \e[0m \e[1;34m ┊┊┊\e[0m Make a PDF that looks like HTML"
echo -e " \e[1;32m HTML \e[0m \e[1;34m ┊┊┊\e[0m Make a self-contained MathML HTML"
echo -e " \e[1;32m HTML-dir \e[0m \e[1;34m ┊┊┊\e[0m Make a directory of HTML+images (for importing to Mediawiki)"
echo -e " \e[1;32m md \e[0m \e[1;34m ┊┊┊\e[0m Export markdown and images to directory"
echo
echo -e " \e[3m\e[1m• Notes\e[0m "
echo -e " This does not work for multiple selections (yet) so avoid pressing"
echo -e " TAB to try and export multiple files, it won't work"
echo
echo -e " The links in exports are not relative, I don't know why,"
echo -e " I need to fix this because it's really really inconvenient."
}
# *** Convert
CadmusConvert () {
[[ -z "${1:-}" ]] && ConvertHelp && exit 0
while test $# -gt 0
do
case "$1" in
org2md) shift; xclip -selection clipboard -o | pandoc -f org -t markdown | xclip -selection clipboard && exit
;;
md2org) shift; xclip -selection clipboard -o | pandoc -f markdown_github+tex_math_dollars --atx-headers -t org | xclip -selection clipboard && exit
;;
tex2md) shift; xclip -selection clipboard -o | pandoc -f latex -t markdown_github+tex_math_dollars --atx-headers | xclip -selection clipboard && exit
;;
md2tex) shift; xclip -selection clipboard -o | pandoc -f markdown_github+tex_math_dollars --atx-headers -t latex | xclip -selection clipboard && exit
;;
html2md) shift; xclip -selection clipboard -o | pandoc -f html -t markdown_github+tex_math_dollars --atx-headers | xclip -selection clipboard && exit
;;
md2html) shift; xclip -selection clipboard -o | pandoc -f markdown_github+tex_math_dollars --atx-headers -t html | xclip -selection clipboard && exit
;;
mediawiki2md) shift; xclip -selection clipboard -o | pandoc -f mediawiki -t markdown_github+tex_math_dollars --atx-headers | xclip -selection clipboard && exit
;;
md2mediawiki) shift; xclip -selection clipboard -o | pandoc -f markdown_github+tex_math_dollars --atx-headers -t mediawiki | xclip -selection clipboard && exit
;;
dokuwiki2md) shift; xclip -selection clipboard -o | pandoc -f dokuwiki -t markdown_github+tex_math_dollars --atx-headers | xclip -selection clipboard && exit
;;
md2dokuwiki) shift; xclip -selection clipboard -o | pandoc -f markdown_github+tex_math_dollars --atx-headers -t dokuwiki | xclip -selection clipboard && exit
;;
--*) >&2 echo "bad option $1"
;;
*) >&2 echo -e "argument \e[1;35m${1}\e[0m has no definition."
;;
esac
shift
done
}
# **** Help
function ConvertHelp () {
echo
echo -e " \e[3m\e[1m Cadmus Export\e[0m; Tools for Editing Notes "
echo -e " \e[1;31m -------------------------\e[0m "
echo
echo -e " \e[1;91m \e[1m Command \e[0m\e[0m \e[1;34m┊┊┊ \e[0m Description "
echo -e " ..............\e[1;34m┊┊┊\e[0m........................................... "
echo -e " \e[1;32m org2md \e[0m \e[1;34m ┊┊┊ \e[0m Transform org-mode to \e[3mMarkDown\e[0m"
echo -e " \e[1;32m md2org \e[0m \e[1;34m ┊┊┊ \e[0m Transform \e[3mMarkDown\e[0m to org-mode"
echo -e " ……… \e[0m\e[1;34m┊┊┊\e[0m"
echo -e " \e[1;32m tex2md \e[0m \e[1;34m ┊┊┊ \e[0m Transform \e[3mLaTeX\e[0m to \e[3mMarkDown\e[0m"
echo -e " \e[1;32m md2tex \e[0m \e[1;34m ┊┊┊ \e[0m Transform \e[3mMarkDown\e[0m to \e[3mLaTeX\e[0m"
echo -e " ……… \e[0m\e[1;34m┊┊┊\e[0m"
echo -e " \e[1;32m html2md\e[0m \e[1;34m ┊┊┊ \e[0m Transform \e[3mHTML\e[0m to \e[3mMarkDown\e[0m"
echo -e " \e[1;32m html2org \e[0m \e[1;34m ┊┊┊ \e[0m Transform \e[3mMarkDown\e[0m to \e[3mHTML\e[0m"
echo -e " ……… \e[0m\e[1;34m┊┊┊\e[0m"
echo -e " \e[1;32m mediawiki2md\e[0m \e[1;34m┊┊┊ \e[0m Transform \e[3mMediaWiki\e[0m to \e[3mMarkDown\e[0m"
echo -e " \e[1;32m md2mediawiki\e[0m \e[1;34m┊┊┊ \e[0m Transform \e[3mMarkDown\e[0m to \e[3mMediaWiki\e[0m"
echo -e " ……… \e[0m\e[1;34m┊┊┊\e[0m"
echo -e " \e[1;32m dokuwiki2md\e[0m \e[1;34m ┊┊┊ \e[0m Transform \e[3mDokuwiki\e[0m to \e[3mMarkDown\e[0m"
echo -e " \e[1;32m md2dokuwiki\e[0m \e[1;34m ┊┊┊ \e[0m Transform \e[3mMarkDown\e[0m to \e[3mDokuwiki\e[0m"
echo -e " ……… \e[0m\e[1;34m┊┊┊\e[0m"
echo
echo -e " \e[3m\e[1m• Notes\e[0m "
echo -e " These commands all transform the clipboard in place."
echo
}
# *** All the Help
## I think all the help files should just be md files, then I could simply do `mdcat *`
subHelp () {
echo
echo
echo -e "\e[1;35m -------------------------\e[0m "
echo -e " \e[1;94m\e[3m\e[1m Cadmus Find \e[0m"
echo -e "\e[1;35m -------------------------\e[0m "
NoteFind.sh -h
echo
echo
echo -e "\e[1;35m -------------------------\e[0m "
echo -e " \e[1;94m\e[3m\e[1m Cadmus Search\e[0m"
echo -e "\e[1;35m -------------------------\e[0m "
NoteRecollSearch.sh -h
echo
echo
echo -e "\e[1;35m -------------------------\e[0m "
echo -e " \e[1;94m\e[3m\e[1m Cadmus Tools\e[0m"
echo -e "\e[1;35m -------------------------\e[0m "
ToolsHelp # This is the exception because these things don't have a help cause they're so simple
}
# * Call the Main Function
main "${@}"