mirror of
https://github.com/RyanGreenup/cadmus.git
synced 2025-08-08 23:26:34 +02:00
878 lines
34 KiB
Bash
Executable File
878 lines
34 KiB
Bash
Executable File
#! /usr/bin/env bash
|
|
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
|
|
readonly script_name=$(basename "${0}")
|
|
script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
|
readonly script_dir=$(realpath "${script_dir}""/""${script_name}" | xargs dirname)
|
|
readonly CONFIG="$HOME/.config/cadmus/config.json" # one directory above
|
|
## readonly SERVER_DIR="/srv/www/html/MD"
|
|
## readonly RECOLL_CONFIG_DIR="$HOME/.cadmus"
|
|
## readonly MKDOCS_YML="$HOME/Notes/mkdocs.yml" ## This may be above the notes directory, the
|
|
## notes directory may not contain all the
|
|
## images for want of seperation and mkdocs is
|
|
## fidally, so specify the path to mkdocs.yml.
|
|
# 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
|
|
|
|
# readonly EDITOR="ema"
|
|
readonly EDITOR="xdg-open"
|
|
|
|
# * Main Functions
|
|
|
|
main() {
|
|
checkConfig
|
|
setvars
|
|
[[ -z "${1:-}" ]] && mainHelp && exit 0
|
|
arguments "${@:-}"
|
|
}
|
|
|
|
function setvars() {
|
|
|
|
readonly TERMINAL="kitty"
|
|
readonly TERMINAL_EXEC='kitty -- '
|
|
|
|
setClipboard
|
|
|
|
readonly NOTES_DIR="$(cat "${CONFIG}" | jq -r '.notesDir')"
|
|
readonly SERVER_DIR="$(cat "${CONFIG}" | jq -r '.serverDir')"
|
|
readonly RECOLL_CONFIG_DIR="$(cat "${CONFIG}" | jq -r '.recollConfigDir')"
|
|
readonly MKDOCS_YML="$(cat "${CONFIG}" | jq -r '.mkdocsConfigDir')" ## This may be above the notes directory, the
|
|
}
|
|
|
|
setClipboard () {
|
|
|
|
case "$(uname -s)" in
|
|
|
|
Darwin)
|
|
CLIP_IN () { pbcopy ; }
|
|
CLIP_OUT () { pbpaste ; }
|
|
xdg-open () { open "%{@}" ; }
|
|
;;
|
|
|
|
Linux|GNU|*BSD|SunOS)
|
|
## "$XDG_SESSION_TYPE" not always defined
|
|
if [[ "$( loginctl show-session $(loginctl | grep $(whoami) | awk '{print $1}') -p Type | rg 'wayland' )" ]]; then
|
|
CLIP_IN () { wl-copy ; }
|
|
CLIP_OUT () { wl-paste ; }
|
|
else
|
|
CLIP_IN () { xclip -selection clipboard ; }
|
|
CLIP_OUT () { xclip -selection clipboard -o ; }
|
|
fi
|
|
;;
|
|
|
|
CYGWIN*|MINGW32*|MSYS*|MINGW*)
|
|
CLIP_IN () { xclip -selection clipboard ; }
|
|
CLIP_OUT () { xclip -selection clipboard -o ; }
|
|
;;
|
|
|
|
# Add here more strings to compare
|
|
# See correspondence table at the bottom of this answer
|
|
|
|
*)
|
|
echo "Could not Detect OS, if you're not on Mac/Linux, file a bug please"
|
|
echo "Applying Linux Defaults"
|
|
CLIP_IN () { xclip -selection clipboard ; }
|
|
CLIP_OUT () { xclip -selection clipboard -o ; }
|
|
;;
|
|
esac
|
|
|
|
|
|
}
|
|
|
|
|
|
# * Sub-functions
|
|
# ** MainMenu
|
|
|
|
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 \e[4mf\e[0m\e[1;93mind \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Find Notes based on FileName"
|
|
echo -e " 🔎 \e[1;32m \e[4ms\e[0m\e[1;32mearch \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Search through Notes using Recoll"
|
|
echo -e " 🏷 \e[1;33m \e[4mt\e[0m\e[1;33mags \e[0m \e[1;34m ┊┊┊ 📁\e[0m Use TMSU to work with tags"
|
|
echo -e " 📖 \e[1;33m \e[4mr\e[0m\e[1;33mofi \e[0m \e[1;34m ┊┊┊ 📁\e[0m Use ROFI instead of fzf (WIP) "
|
|
echo -e " 🔧 \e[1;34m \e[4mto\e[0m\e[1;34mols \e[0m \e[1;34m ┊┊┊ 📁\e[0m Tools for Editing"
|
|
echo -e " 📝 \e[1;35m \e[4me\e[0m\e[1;35mxport \e[0m \e[1;34m ┊┊┊ 📁\e[0m Export Notes to Different Formats "
|
|
echo -e " ⎋ \e[1;36m \e[4mc\e[0m\e[1;36monvert \e[0m \e[1;34m ┊┊┊ 📁\e[0m Convert Clipboard Contents to Different Formats "
|
|
echo -e " 🧰 \e[1;37m \e[4mm\e[0m\e[1;37misc \e[0m \e[1;34m ┊┊┊ 📁\e[0m Miscelanneous Tools nice to have on hand "
|
|
echo -e " 🌏 \e[1;92m \e[4mpub\e[0m\e[1;92mlish\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 \e[4mp\e[0m\e[1;93mreview \e[0m \e[1;34m ┊┊┊ 🎆\e[0m Preview with \e[1;34m \e[4m\e[3mMarkServ\e[0m\e[0m (use 'preview!' for global)"
|
|
echo -e " 🕮 \e[1;94m \e[4mh\e[0m\e[1;94melp \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
|
|
# *** Config
|
|
checkConfig () {
|
|
if [[ -f "${CONFIG}" ]]; then
|
|
echo "Config Detected"
|
|
else
|
|
echo -e "No config File detected at ${CONFIG}"
|
|
echo -e "Would you like to generate a config?"
|
|
echo
|
|
echo -e "Press y to continue or any other key to exit."
|
|
echo
|
|
read -d '' -s -n1 choice
|
|
if [[ "${choice}" == "y" ]]; then
|
|
config_json=$("${script_dir}/makeConfig.bash")
|
|
if [[ "${config_json}" != "" ]]; then
|
|
[[ -d "$(dirname "${CONFIG}")" ]] || mkdir -p "$(dirname "${CONFIG}")"
|
|
echo "${config_json}" > "${CONFIG}"
|
|
echo -e "This is the Config:\n"
|
|
highlight "${CONFIG}"
|
|
else
|
|
echo "Config not Generated"
|
|
exit 1
|
|
fi
|
|
else
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
}
|
|
# *** Switch
|
|
arguments () {
|
|
|
|
while test $# -gt 0
|
|
do
|
|
case "$1" in
|
|
--help|-h) mainHelp && exit 0
|
|
;;
|
|
find|f) shift; NoteFind "${NOTES_DIR}" "${@:-}" ## Don't steal function name
|
|
;;
|
|
search|s) shift; NoteSearch "${NOTES_DIR}" ${@:-}
|
|
;;
|
|
rofi|r) shift; RofiMenu ${@:-}
|
|
;;
|
|
tags|t) shift; CadmusTags ${@:-}
|
|
;;
|
|
tools|to) shift; CadmusTools "${@:-}"
|
|
;;
|
|
export|e) shift; CadmusExport "${@:-}"
|
|
;;
|
|
convert|c) shift; CadmusConvert "${@:-}"
|
|
;;
|
|
misc|m) shift; CadmusMisc "${@:-}"
|
|
;;
|
|
publish|pub) shift; CadmusPublish "${@:-}"
|
|
;;
|
|
preview|p) shift; cd "${NOTES_DIR}" && markserv || echo -e " \n \e[1;34m maybe fix this with:\n\tsudo fuser 35729/tcp -k \n \tor\n\tkillall markserv \e[0m\n"
|
|
;;
|
|
preview!) shift; previewGlobal
|
|
;;
|
|
help|h) 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="${script_dir}/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 "${EDITOR}"
|
|
|
|
# # 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="${script_dir}/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 "${EDITOR}"
|
|
fi
|
|
}
|
|
|
|
|
|
# *** RofiMenu
|
|
function RofiMenu() {
|
|
|
|
[[ -z "${1:-}" ]] && RofiHelp && exit 0
|
|
|
|
while test $# -gt 0
|
|
do
|
|
case "$1" in
|
|
search) shift; "${script_dir}"/rofi_search.sh "${NOTES_DIR}" && exit 0
|
|
;;
|
|
find) shift; "${script_dir}"/rofi_find.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
|
|
|
|
}
|
|
|
|
# **** Rofi Help
|
|
function RofiHelp() {
|
|
|
|
echo
|
|
echo -e " \e[3m\e[1m Cadmus Rofi\e[0m; Use Rofi and Browser to Search/Find and Preview"
|
|
echo -e " \e[1;31m -------------------------\e[0m "
|
|
echo
|
|
echo -e " \e[3m\e[1m• Notes\e[0m "
|
|
echo
|
|
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 Create Tags with TMSU"
|
|
echo
|
|
}
|
|
|
|
# *** Note Search
|
|
|
|
function NoteSearch() {
|
|
COMMAND="${script_dir}/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 "${EDITOR}"
|
|
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
|
|
;;
|
|
mount) shift; MountTags
|
|
;;
|
|
--*) >&2 echo "bad option $1"
|
|
;;
|
|
*) >&2 echo -e "argument \e[1;35m${1}\e[0m has no definition."
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
}
|
|
|
|
# **** Mount Tags
|
|
MountTags () {
|
|
cd "${NOTES_DIR}"
|
|
|
|
DIR="$(mktemp -d)"
|
|
|
|
tmsu mount "${DIR}"
|
|
nautilus "${DIR}" && exit 0
|
|
}
|
|
|
|
|
|
# **** 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[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}")"
|
|
echo
|
|
echo -e " \e[3m\e[1m• Troubleshooting\e[0m \n"
|
|
echo -e " If you are having Issues use the following to find any .tmsu databases:"
|
|
echo -e ' find $HOME -type d -name '.tmsu'' | highlight --syntax bash -O ansi
|
|
echo -e " And delete all the databases to start fresh\n"
|
|
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 -e " \e[1;32m mount \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Mount Tags as a Virtual FS"
|
|
echo
|
|
}
|
|
|
|
|
|
# *** Tools
|
|
CadmusTools () {
|
|
[[ -z "${1:-}" ]] && ToolsHelp && exit 0
|
|
|
|
|
|
while test $# -gt 0
|
|
do
|
|
case "$1" in
|
|
new) shift; makeNewNote "${@}" && exit 0
|
|
;;
|
|
webtitle) shift; "${script_dir}/tools/PrintWebTitle.sh" "$(CLIP_OUT)" | CLIP_IN
|
|
;;
|
|
backlinks) shift; "${script_dir}/tools/List-Backlinks.sh" "$(CLIP_OUT)" "${NOTES_DIR}" ${@:-} && exit 0
|
|
;;
|
|
link) shift; "${script_dir}/tools/LinkMarkdownNotes.sh" "${NOTES_DIR}" "$(CLIP_OUT)" | CLIP_IN && exit 0
|
|
;;
|
|
fix) shift; "${script_dir}/tools/fixLink.sh" "${NOTES_DIR}" && exit 0
|
|
;;
|
|
|
|
unused-attachments) shift; node "${script_dir}/tools/print_unused_attachments.js" "${NOTES_DIR}" && exit 0
|
|
;;
|
|
page-import) shift; CLIP_OUT | xargs curl | pandoc -f html -t markdown_github+tex_math_dollars --atx-headers | CLIP_IN && exit 0
|
|
;;
|
|
random) shift; find "${NOTES_DIR}" -name '*.md' | shuf -n 1 | xargs "${EDITOR}" && exit 0
|
|
;;
|
|
rename) shift; CadmusRename "${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 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[3m\e[1m• Legend\e[0m "
|
|
echo -e " ✀ Works with the clipboard for input/output."
|
|
echo -e " \e[3m\e[1m• Notes\e[0m "
|
|
echo -e " Renaming relies on the assumption that all notes have unique names."
|
|
echo -e " If two notes have the same name consider giving one a more descriptive name"
|
|
echo -e " or merging them."
|
|
echo
|
|
echo -e " To fix the links this just uses a dumb call to sed, this shouldn't be an issue"
|
|
echo -e " if file names are unique-long-and-descriptive-without-whitespace.md, unless"
|
|
echo -e " ∵ I doubt you would use a slugified word like that for any other reason in a note"
|
|
echo -e " \tNone the less commit changes before renaming and then use the diff after"
|
|
echo -e " \tto see which changes you want to commit in the event there are false positives"
|
|
echo
|
|
echo -e " When Using Rename, make sure to not type in the extension, this is automatically"
|
|
echo -e " Done so that the same logic could be used on org-files"
|
|
echo
|
|
echo -e " For the sake of Sanity (my sanity) Renaming will not work with the following characters:"
|
|
echo -e " . ...................................(period)"
|
|
echo -e " ␣ ...................................(i.e. whitespace)"
|
|
echo -e " / ...................................(forward slash)"
|
|
echo -e " \\ ..................................(back slash)"
|
|
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 new \e[0m \e[1;34m ┊┊┊ \e[0m Starts an interactive prompt to make a new note"
|
|
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 unused-attachments \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Print unused attachments, not working yet "
|
|
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 rename \e[0m \e[1;34m ┊┊┊ 🎆 \e[0m Choose a note to rename and adjust all links"
|
|
echo
|
|
}
|
|
|
|
# **** New Note
|
|
|
|
makeNewNote () {
|
|
echo -e "Please Choose Relevant Tags (Use Tab for Multi Select)"
|
|
echo -e "\n\t(Press any key to continue)"
|
|
read -d '' -s -n1 choice
|
|
|
|
## Choose Tags
|
|
TAGS="$(node "${script_dir}"/tags/yaml-parse.js "${NOTES_DIR}" 2>/dev/null | sort -u| sk -m)"
|
|
|
|
if [[ "${TAGS}" != "" ]]; then
|
|
TAGS="$(echo "${TAGS}" | sed 's/^\|$//g'|paste -sd, - | sed 's/,/,\ /g' | sed 's/^/\[/' | sed 's/$/\]/')"
|
|
fi
|
|
|
|
echo "${TAGS}"
|
|
|
|
echo -e "\nPlease Choose an appropriate directory\n\n"
|
|
echo -e "\n\t(Press any key to continue)"
|
|
read -d '' -s -n1 choice
|
|
|
|
## Chose Directory
|
|
DIR="$(find "${NOTES_DIR}" -type d | sk)" || exit 0
|
|
|
|
## Enter a name
|
|
echo -e "Please enter a title for the note:\n"
|
|
read NAME
|
|
|
|
## Name must be non-empty
|
|
if [[ "${NAME}" != "" ]]; then
|
|
NAME_SLUG="$(echo "${NAME}" | sed s/\ /-/g )"
|
|
else
|
|
echo "No input for title, aborting" && exit 0
|
|
fi
|
|
|
|
## combine dir and name to get path
|
|
FILE="${DIR}/${NAME_SLUG}.md"
|
|
echo -e "Creating:\n "${FILE}""
|
|
|
|
## Test for the file
|
|
if [ -f "${DIR}/${NAME_SLUG}.md" ]; then
|
|
echo "${DIR}/${NAME_SLUG}.md" | CLIP_IN
|
|
echo "The file exists, aborting"
|
|
echo "The desired path is in the clipboard" && exit 0
|
|
else
|
|
touch "${DIR}/${NAME_SLUG}.md"
|
|
fi
|
|
|
|
## Get time and date
|
|
printf -v date '%(%Y-%m-%dT%H:%M:%S)T\n' -1
|
|
date="$(echo ${date} | sed s/^/\'/ | sed s/$/\'/)"
|
|
|
|
## Create the new file
|
|
echo -e "---" >> "${FILE}"
|
|
echo -e "title: "${NAME}"" >> "${FILE}"
|
|
echo -e "tags: "${TAGS}"" >> "${FILE}"
|
|
echo -e "created: "${date}"" >> "${FILE}"
|
|
echo -e "---\n" >> "${FILE}"
|
|
echo -e "# "${NAME}"" >> "${FILE}"
|
|
|
|
mdcat "${FILE}"
|
|
echo -e "Succesfully Created:\n "${FILE}""
|
|
nvim + "${FILE}"
|
|
|
|
|
|
}
|
|
|
|
# **** Rename
|
|
CadmusRename () {
|
|
COMMAND="${script_dir}/NoteFind.sh"
|
|
echo "Running "${COMMAND}" "${1}""
|
|
|
|
FILES="$("${COMMAND}" "${@:-}")"
|
|
|
|
for i in $FILES; do
|
|
DirofNote="$(dirname "${i}")"
|
|
getnames "${1}"
|
|
echo -e "\n Renaming \e[1;32m"${OLDNAME}"\e[0m to \e[1;32m"${NEWNAME}"\e[0m\n"
|
|
renameThenSed "${i}" "${OLDNAME}" "${NEWNAME}"
|
|
done
|
|
|
|
}
|
|
|
|
# ***** Prompt for Names
|
|
#
|
|
getnames () {
|
|
OLDNAME="$(basename $i | cut -f 1 -d '.' )"
|
|
EXTENSION="$(basename $i | rev | cut -f 1 -d '.' | rev )"
|
|
echo -e "\nplease enter a new name \e[1;31mWITHOUT THE .md \e[0m\n for:\n \e[1;34m"${OLDNAME}"\e[0m\n"
|
|
read NEWNAME && [[ "${NEWNAME}" == "" ]] && echo -e "\e[1;31mRequire a New Name!\e[0m" && getnames "${i}"
|
|
|
|
## Remove whitespace from new name
|
|
NEWNAME="$(echo "${NEWNAME}" | sed s/\ /-/g)"
|
|
}
|
|
# ***** Rename then Sed
|
|
renameThenSed () {
|
|
## Rename
|
|
echo "${DirofNote}"
|
|
echo "mv "\'${DirofNote}/${OLDNAME}.md\'" "\'${DirofNote}/${NEWNAME}\'" "
|
|
echo "mv "\'${DirofNote}/${OLDNAME}.md\'" "\'${DirofNote}/${NEWNAME}.md\'" " | bash
|
|
|
|
## Sed
|
|
echo "sd -s "\'${OLDNAME}\'" "\'${NEWNAME}\'""
|
|
sd -s "${OLDNAME}" "${NEWNAME}" $(fd --type file '.md' "$NOTES_DIR")
|
|
}
|
|
# *** 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 --mathjax -c "${script_dir}"'/resources/pandoc.css' && exit 0
|
|
;;
|
|
page-import) shift; CLIP_OUT | xargs curl | pandoc -f html -t markdown_github+tex_math_dollars --atx-headers | CLIP_IN && 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="$(CLIP_OUT)"
|
|
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}" --mathjax ${@:-} --extract-media="${OUTDIR}/Media" -o "${OUTDIR}"/"${FILEOUT} | highlight --syntax bash -O ansi
|
|
echo
|
|
|
|
## Media must have first letter capitalised for mediawiki
|
|
## HTML2Wiki wants zip file
|
|
|
|
pandoc -s "${FILE}" ${@:-} --extract-media="${OUTDIR}/Media" -o "${OUTDIR}"/"${FILEOUT}" || exit 7
|
|
cd "${OUTDIR}"
|
|
## Run it a second time so extracted media is relative not absolute
|
|
## It seems mathjax only works here, not katex :shrug:
|
|
pandoc -f -s --mathjax "${FILEOUT}" ${@:-} --extract-media="Media" -o "${FILEOUT}" || echo "" # This might fail for PDF's, don't exit
|
|
## Zip everything up as well
|
|
zip -r "$(echo ${FILE} | cut -f 1 -d '.')".zip *
|
|
"${EDITOR}" "${OUTDIR}"/"${FILEOUT}"
|
|
cd -
|
|
}
|
|
|
|
# **** 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[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 made relative, by using a second call to pandoc"
|
|
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 (firefox/safari only)"
|
|
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 -e " \e[1;32m org \e[0m \e[1;34m ┊┊┊\e[0m Export as org-mode with images to directory"
|
|
echo
|
|
}
|
|
|
|
|
|
# *** Convert
|
|
CadmusConvert () {
|
|
[[ -z "${1:-}" ]] && ConvertHelp && exit 0
|
|
|
|
while test $# -gt 0
|
|
do
|
|
case "$1" in
|
|
org2md) shift; CLIP_OUT | pandoc -f org -t markdown_github+tex_math_dollars --atx-headers | CLIP_IN && exit
|
|
;;
|
|
md2org) shift; CLIP_OUT | pandoc -f markdown_github+tex_math_dollars --atx-headers -t org | CLIP_IN && exit
|
|
;;
|
|
tex2md) shift; CLIP_OUT | pandoc -f latex -t markdown_github+tex_math_dollars --atx-headers | CLIP_IN && exit
|
|
;;
|
|
md2tex) shift; CLIP_OUT | pandoc -f markdown_github+tex_math_dollars --atx-headers -t latex | CLIP_IN && exit
|
|
;;
|
|
html2md) shift; CLIP_OUT | pandoc -f html -t markdown_github+tex_math_dollars --atx-headers | CLIP_IN && exit
|
|
;;
|
|
md2html) shift; CLIP_OUT | pandoc -f markdown_github+tex_math_dollars --atx-headers -t html | CLIP_IN && exit
|
|
;;
|
|
mediawiki2md) shift; CLIP_OUT | pandoc -f mediawiki -t markdown_github+tex_math_dollars --atx-headers | CLIP_IN && exit
|
|
;;
|
|
md2mediawiki) shift; CLIP_OUT | pandoc -f markdown_github+tex_math_dollars --atx-headers -t mediawiki | CLIP_IN && exit
|
|
;;
|
|
dokuwiki2md) shift; CLIP_OUT | pandoc -f dokuwiki -t markdown_github+tex_math_dollars --atx-headers | CLIP_IN && exit
|
|
;;
|
|
md2dokuwiki) shift; CLIP_OUT | pandoc -f markdown_github+tex_math_dollars --atx-headers -t dokuwiki | CLIP_IN && exit
|
|
;;
|
|
--*) >&2 echo "bad option $1"
|
|
;;
|
|
*) >&2 echo -e "argument \e[1;35m${1}\e[0m has no definition."
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
}
|
|
|
|
# *** Preview Global
|
|
previewGlobal() {
|
|
MARKSERVPORT="28754"
|
|
cd "${NOTES_DIR}"
|
|
printQR
|
|
markserv -p "${MARKSERVPORT}" -a 0.0.0.0 || echo -e " \n \e[1;34m maybe fix this with:\n\tsudo fuser 35729/tcp -k \n \tor\n\tkillall markserv \e[0m\n"
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
# **** Print QR
|
|
printQR () {
|
|
command -v qrencode >/dev/null 2>&1 || { echo -e >&2 "I require qrencode but it's not installed.\nNo QR Code can be printed."; return; }
|
|
command -v ip >/dev/null 2>&1 || { echo -e >&2 "I requre ip but it's not installed.\nNo QR Code can be printed."; return; }
|
|
|
|
CURRENTIP="$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')"
|
|
qrencode -t UTF8 "http://${CURRENTIP}:${MARKSERVPORT}"
|
|
|
|
}
|
|
|
|
# **** Help
|
|
function ConvertHelp () {
|
|
|
|
echo
|
|
echo -e " \e[3m\e[1m Cadmus Convert\e[0m; Convert the Clipboard in place"
|
|
echo -e " \e[1;31m -------------------------\e[0m "
|
|
echo
|
|
echo -e " \e[3m\e[1m• Notes\e[0m "
|
|
echo -e " These commands all transform the clipboard in place."
|
|
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
|
|
}
|
|
|
|
|
|
# *** Misc
|
|
CadmusMisc() {
|
|
[[ -z "${1:-}" ]] && MiscHelp && exit 0
|
|
|
|
while test $# -gt 0
|
|
do
|
|
case "$1" in
|
|
ranger) shift; ranger "${NOTES_DIR}" && exit 0
|
|
;;
|
|
code) shift; code "${NOTES_DIR}" && exit 0
|
|
;;
|
|
atom) shift; atom "${NOTES_DIR}" && exit 0
|
|
;;
|
|
wa) shift; wmctrl -a "$(wmctrl -l | cut -f 5- -d " " | fzf)" && exit 0
|
|
;;
|
|
wR) shift; wmctrl -R "$(wmctrl -l | cut -f 5- -d " " | fzf)" && exit 0
|
|
;;
|
|
print) shift; "${script_dir}/NoteFind.sh" "${NOTES_DIR}" | xargs mdcat
|
|
;;
|
|
sync) shift; cd "${NOTES_DIR}"; git pull && git add -A && git commit && git push
|
|
;;
|
|
--*) >&2 echo "bad option $1"
|
|
;;
|
|
*) >&2 echo -e "argument \e[1;35m${1}\e[0m has no definition."
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
}
|
|
|
|
# **** Help
|
|
function MiscHelp () {
|
|
|
|
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 ranger \e[0m \e[1;34m ┊┊┊ \e[0m Open Notes Directory in Ranger"
|
|
echo -e " \e[1;32m code \e[0m \e[1;34m ┊┊┊ \e[0m Open Notes Directory in Ranger"
|
|
echo -e " \e[1;32m atom \e[0m \e[1;34m ┊┊┊ \e[0m Open Notes Directory in Ranger"
|
|
echo -e " \e[1;32m wa \e[0m \e[1;34m ┊┊┊ \e[0m Activate Window FIX: use command not alias?"
|
|
echo -e " \e[1;32m wR \e[0m \e[1;34m ┊┊┊ \e[0m Relocate Window (Like rofi)"
|
|
echo -e " \e[1;32m print \e[0m \e[1;34m ┊┊┊ \e[0m Print Markdown to the terminal (with images)"
|
|
echo -e " \e[1;32m sync \e[0m \e[1;34m ┊┊┊ \e[0m pull, add -A, commit and push repository"
|
|
echo
|
|
echo -e " \e[3m\e[1m• Notes\e[0m "
|
|
echo -e " "
|
|
echo
|
|
}
|
|
|
|
|
|
# *** Publish
|
|
CadmusPublish () {
|
|
[[ -z "${1:-}" ]] && PublishHelp && exit 0
|
|
|
|
while test $# -gt 0
|
|
do
|
|
case "$1" in
|
|
gh-deploy) shift; [[ -f "${MKDOCS_YML}" ]] && cd "$(dirname $(realpath ${MKDOCS_YML}))" && mkdocs gh-deploy || echo "Cannot locate "${MKDOCS_YML}""
|
|
;;
|
|
server) shift; [[ -f "${MKDOCS_YML}" ]] && cd "$(dirname $(realpath ${MKDOCS_YML}))" && mkdocs --clean --site-dir "${SERVER_DIR}" || echo "Cannot locate "${MKDOCS_YML}""
|
|
;;
|
|
--*) >&2 echo "bad option $1"
|
|
;;
|
|
*) >&2 echo -e "argument \e[1;35m${1}\e[0m has no definition."
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
}
|
|
|
|
# **** Help
|
|
function PublishHelp () {
|
|
|
|
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[3m\e[1m• Notes\e[0m "
|
|
echo -e " Building in server directory will require root priviliges"
|
|
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 gh-deploy \e[0m \e[1;34m ┊┊┊ \e[0m call mkdocs gh-deploy from ${NOTES_DIR} "
|
|
echo -e " \e[1;32m server \e[0m \e[1;34m ┊┊┊ \e[0m Build Mkdocs in ${SERVER_DIR}"
|
|
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 "
|
|
"${script_dir}/NoteFind.sh"
|
|
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 "
|
|
"${script_dir}/NoteRecollSearch.sh"
|
|
|
|
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
|
|
|
|
|
|
echo
|
|
echo
|
|
echo -e "\e[1;35m -------------------------\e[0m "
|
|
echo -e " \e[1;94m\e[3m\e[1m Cadmus Export \e[0m"
|
|
echo -e "\e[1;35m -------------------------\e[0m "
|
|
ExportHelp
|
|
|
|
echo
|
|
echo
|
|
echo -e "\e[1;35m -------------------------\e[0m "
|
|
echo -e " \e[1;94m\e[3m\e[1m Cadmus Convert \e[0m"
|
|
echo -e "\e[1;35m -------------------------\e[0m "
|
|
ConvertHelp
|
|
|
|
echo
|
|
echo
|
|
echo -e "\e[1;35m -------------------------\e[0m "
|
|
echo -e " \e[1;94m\e[3m\e[1m Cadmus Misc \e[0m"
|
|
echo -e "\e[1;35m -------------------------\e[0m "
|
|
MiscHelp
|
|
|
|
|
|
echo
|
|
echo
|
|
echo -e "\e[1;35m -------------------------\e[0m "
|
|
echo -e " \e[1;94m\e[3m\e[1m Preview \e[0m"
|
|
echo -e "\e[1;35m -------------------------\e[0m "
|
|
echo -e " this should just work"
|
|
}
|
|
|
|
# * Call the Main Function
|
|
main "${@}"
|