1
0
mirror of https://github.com/RyanGreenup/cadmus.git synced 2025-04-21 02:01:50 +02:00

Began writing tool to print unused attachments

This commit is contained in:
Ryan Greenup 2021-01-26 13:49:56 +11:00
parent 141d2060f5
commit d130c69c8b
2 changed files with 124 additions and 12 deletions

View File

@ -18,7 +18,7 @@ set -o nounset
# don't hide errors within pipes
set -o pipefail
readonly EDITOR="emacsclient"
readonly EDITOR="ema"
# * Main Functions
@ -362,6 +362,9 @@ 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 "${EDITOR}" && exit 0
@ -407,17 +410,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
}

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()