1
0
mirror of https://github.com/RyanGreenup/cadmus.git synced 2025-01-17 04:38:34 +01:00
This commit is contained in:
Ryan Greenup 2021-03-05 18:44:40 +11:00
commit 1de6fd252c
4 changed files with 135 additions and 19 deletions

View File

@ -18,6 +18,8 @@ set -o nounset
# don't hide errors within pipes
set -o pipefail
# readonly EDITOR="ema"
readonly EDITOR="xdg-open"
# * Main Functions
@ -198,7 +200,7 @@ function NoteFind() {
## 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
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
@ -219,7 +221,7 @@ function NoteSearch() {
## Only try and open something that is non-empty
if [ "$FILES" != "" ]; then
echo "${FILES}" | sed -e s+^+\"+ -e s+$+\"+ | xargs -n 1 xdg-open
echo "${FILES}" | sed -e s+^+\"+ -e s+$+\"+ | xargs -n 1 "${EDITOR}"
fi
}
@ -273,7 +275,7 @@ function NoteSearch() {
## Only try and open something that is non-empty
if [ "$FILES" != "" ]; then
echo "${FILES}" | sed -e s+^+\"+ -e s+$+\"+ | xargs -n 1 xdg-open
echo "${FILES}" | sed -e s+^+\"+ -e s+$+\"+ | xargs -n 1 "${EDITOR}"
fi
}
@ -361,9 +363,12 @@ CadmusTools () {
;;
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 xdg-open && exit 0
random) shift; find "${NOTES_DIR}" -name '*.md' | shuf -n 1 | xargs "${EDITOR}" && exit 0
;;
rename) shift; CadmusRename "${NOTES_DIR}" && exit 0
;;
@ -406,17 +411,18 @@ function ToolsHelp() {
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 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 -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
}
@ -597,7 +603,7 @@ pandocExport () { # $1 is extension; $2+ are options for pandoc
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 *
xdg-open "${OUTDIR}"/"${FILEOUT}"
"${EDITOR}" "${OUTDIR}"/"${FILEOUT}"
cd -
}

View File

@ -1,19 +1,21 @@
#!/usr/bin/env bash
command -v rg >/dev/null 2>&1 || { echo >&2 "I require ripgrep but it's not installed. Aborting."; exit 1; }
command -v sd >/dev/null 2>&1 || { echo >&2 "I require sd (sed replacement) but it's not installed. Aborting."; exit 1; }
command -v xclip >/dev/null 2>&1 || { echo >&2 "I require xclip but it's not installed. Aborting."; exit 1; }
term=$(basename "${1}" | cut -f 1 -d '.')
DIR="${2}"
rg -e "\[.*\]\(.*$term\.md\)" -e "\[\[$term\]\]" -e "\[\[$term.*\]\]" \
"${NOTES_DIR}" \
"${DIR}" \
-t markdown -ol
# ~/Notes/MD/notes \
## If you want to preview the Backlinks
## rg -e "\[.*\]\(.*$term\.md\)" -e "\[\[$term\]\]" -e "\[\[$term.*\]\]" \
## ~/Notes/ -t markdown -ol \

View File

@ -0,0 +1,108 @@
// test this with node print_unused_attachments.js | xargs rg
// It doesn't seem to work.
// //////////////////////////////////////////////////////////
// ////////// Load Libraries ////////////////////////////////
// //////////////////////////////////////////////////////////
const path = require('path');
const fs = require('fs');
// const yamlFront = require('yaml-front-matter');
let debugFlag = false;
let glob = require('glob');
function main() {
change_directory();
attachments = get_file_names()[0];
notes = get_file_names()[1];
unused_attachments = find_unused_attachments(attachments, notes);
print(unused_attachments)
}
function change_directory() {
////////////////////////////////////////////////////////////
/////////// Change Directory ///////////////////////////////
////////////////////////////////////////////////////////////
if (process.argv[2] == undefined) {
const path = "./";
if (debugFlag) {
console.log(`No Path Detected, using this directory ${process.argv[1]}`)
console.log("Remember to use $HOME not ~")
}
} else if (process.argv[2] == "-h" | process.argv[2] == "--help") {
console.log("\nProvide the Directory of MD Notes as the First Argument")
console.log("Otherwise the current directory, ./, will be used.\n")
console.log("No notes will not lead to any warning")
console.log("This is necessary so as to not be dangerous when | bash\n")
} else {
const path = process.argv[2];
process.chdir(path);
if (debugFlag) {
console.log(`Using Specified Directory ${process.argv[2]}`)
}
}
}
function get_file_names() {
// //////////////////////////////////////////////////////////
// ////////////// Get File Names/////////////////////////////
// //////////////////////////////////////////////////////////
let att_extensions = [
"./**/*.png",
"./**/*.jpeg",
"./**/*.jpg",
"./**/*.svg"
]
let note_extensions = [
"./**/*.md",
"./**/*.org",
"./**/*.txt",
"./**/*.html",
"./**/*.tex"
]
var attFilePathList = [];
var noteFilePathList = [];
for (i=0; i < note_extensions.length; i++) {
noteFilePathList.push(glob.sync(note_extensions[i]));
}
for (i=0; i < att_extensions.length; i++) {
attFilePathList.push(glob.sync(att_extensions[i]));
}
noteFilePathList = noteFilePathList.flat();
attFilePathList = attFilePathList.flat();
return [attFilePathList, noteFilePathList];
}
function find_unused_attachments(attachments, notes) {
for (i=0; i < attachments.length;i++) {
att = attachments[i];
for (j = 0; j < notes.length; j++) {
note = fs.readFileSync(notes[j], "utf-8");
att_referencedQ = note.includes(basename(att))
// TODO this probably doesn't work because I need to loop over each line
if (!att_referencedQ) {
print(basename(att))
break
}
}
}
return 0;
}
function basename(string) {
return string.split('\\').pop().split('/').pop(); // https://stackoverflow.com/a/25221100
}
function print(val) {
console.log(val);
}
main()

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash
INSTALL_DIR="$HOME/.cadmus"
BIN_DIR="$HOME/.local/bin/"
mkdir "${BIN_DIR} > /dev/null
mkdir "${BIN_DIR}" > /dev/null
main () {
me=`basename "$0"`