parent
8ddda7f2b4
commit
ac031344d3
5 changed files with 222 additions and 207 deletions
@ -0,0 +1,83 @@ |
||||
#!/usr/bin/env bash |
||||
|
||||
# This file contains functions relating to file operations such as new, edit and deleting files |
||||
|
||||
newFile(){ |
||||
vecho "newFile $*" |
||||
cd "$dataDir" || return |
||||
# While there is a - at the begining |
||||
local title="$*" |
||||
if [ -z "$title" ]; then |
||||
echo -n "Enter a title: " |
||||
read -r title |
||||
fi |
||||
local filename |
||||
filename="$(escapeFilename "$title.md")" |
||||
[ -e "$filename" ] && die "$filename already exists" |
||||
echo -e "--- |
||||
Title: $title |
||||
Tags: |
||||
- |
||||
--- |
||||
" > "$filename" |
||||
echo "INSERT INTO items (filename, title, type) |
||||
VALUES ( '$(safeSQL "$filename")', '$(safeSQL "$title")', 'normal' );" | |
||||
sqlite3 "${sqliteFile}" |
||||
editFile "$filename" |
||||
} |
||||
|
||||
# Takes the filename as a parameter |
||||
editFile(){ |
||||
vecho "editFile $*" |
||||
cd "$dataDir" || return |
||||
local filename |
||||
local oldTitle |
||||
local newTitle |
||||
filename="$(findFile "$*")" |
||||
[ ! -e "$filename" ] && exit 1 |
||||
oldTitle="$(getYamlTitle "$filename")" |
||||
"$editor" "$filename" |
||||
newTitle="$(getYamlTitle "$filename")" |
||||
getYamlTags "$filename" | assignTags "$filename" |
||||
if [ "$newTitle" != "$oldTitle" ]; then |
||||
vecho "Changed title" |
||||
local newfilename |
||||
newfilename="$(escapeFilename "$newTitle.md")" |
||||
if [ -e "$newfilename" ]; then |
||||
echo -e "${YELLOW}File name $newfilename already exists${NC}" |
||||
echo -e "Please fix manually" |
||||
exit 1 |
||||
else |
||||
mv "$filename" "$newfilename" |
||||
echo "UPDATE items |
||||
SET (filename,title) = ('$(safeSQL "$newfilename")','$(safeSQL "$newTitle")') |
||||
WHERE filename = '$(safeSQL "$filename")';" | |
||||
sqlite3 "${sqliteFile}" |
||||
gitChange "$newfilename" |
||||
fi |
||||
else |
||||
gitChange "$filename" |
||||
fi |
||||
} |
||||
|
||||
deleteFile(){ |
||||
cd "$dataDir" || return |
||||
local filename |
||||
local fileID |
||||
local rsp |
||||
filename="$(findFile "$1")" |
||||
fileID="$(findFileId "$filename")" |
||||
[ ! -e "$filename" ] && exit 1 |
||||
echo -n "Are you sure? [yN] " |
||||
read -r rsp |
||||
if [[ "$(echo "$rsp" | tr '[:upper:]' '[:lower:]')" = "y"* ]]; then |
||||
rm "$filename" |
||||
# This deletes the file from the sql database and any tag links |
||||
echo "DELETE FROM items |
||||
WHERE id = '$(safeSQL "$fileID")'; |
||||
DELETE FROM links |
||||
WHERE itemID = '$(safeSQL "$fileID")';" | |
||||
sqlite3 --column --header "${sqliteFile}" |
||||
fi |
||||
} |
||||
|
@ -0,0 +1,33 @@ |
||||
#!/usr/bin/env bash |
||||
|
||||
initKnowledgeBase(){ |
||||
local output |
||||
necho -e "${YELLOW}Initialising Knowledge base${NC}" |
||||
vecho "Directory: $dataDir" |
||||
if [ "$verbose" -gt 0 ]; then |
||||
output="/dev/stdout" |
||||
else |
||||
output="/dev/null" |
||||
fi |
||||
[ -e "$dataDir" ] && die "$dataDir already exists" |
||||
mkdir -p "$dataDir" |
||||
if [ "$dogit" -gt 0 ]; then |
||||
git init "$dataDir" > "$output" |
||||
|
||||
# TODO: make gitignore use new sqlite file |
||||
echo "/knowledgebase.sqlite3" >> "${dataDir}/.gitignore" |
||||
|
||||
git -C "$dataDir" add .gitignore > "$output" |
||||
git -C "$dataDir" commit -m "Knowledge base initialised" > output |
||||
fi |
||||
vecho "Creating Database" |
||||
echo 'CREATE TABLE items |
||||
(id integer primary key, filename text, title text, type text); |
||||
CREATE TABLE tags |
||||
(id integer primary key, name text); |
||||
CREATE TABLE links |
||||
(id integer primary key, itemID integer, tagID integer); ' | |
||||
sqlite3 "${sqliteFile}" |
||||
necho -e "${GREEN}Initialised Knowledge base${NC}" |
||||
} |
||||
|
@ -0,0 +1,66 @@ |
||||
#!/usr/bin/env bash |
||||
|
||||
assignTags(){ |
||||
local filename |
||||
local tags |
||||
local tagIDs |
||||
local tagIDsOr |
||||
local fileID |
||||
filename="$(findFile "$1")" |
||||
[ ! -e "$filename" ] && exit 1 |
||||
tags="$(cat - | sed '/^$/d')" |
||||
fileID="$(findFileId "$filename")" |
||||
# If there are tags |
||||
if [ -n "$tags" ]; then |
||||
local values |
||||
local orlist |
||||
while read -r line; do |
||||
values+=",('$(safeSQL "$line")')" |
||||
orlist+=" OR name = '$(safeSQL "$line")'" |
||||
done <<<"$(echo "$tags")" |
||||
values="$(echo "$values" | sed 's/^,//')" |
||||
orlist="$(echo "$orlist" | sed 's/^ OR //')" |
||||
|
||||
# Ensure that all the tags exist |
||||
echo "INSERT INTO tags (name) VALUES $values |
||||
EXCEPT SELECT name FROM tags;" | |
||||
sqlite3 "${sqliteFile}" |
||||
|
||||
# Get the tag ids we need to assosiate with the current file |
||||
tagIDs="$(echo "SELECT id FROM tags WHERE $orlist" | |
||||
sqlite3 "${sqliteFile}")" |
||||
|
||||
#Loop through them all |
||||
while read -r tagID; do |
||||
tagIDsOr+=" OR tagID = $(safeSQL "$tagID")" |
||||
|
||||
# Check the tag is already linkded with the file |
||||
local existing |
||||
existing="$(echo "SELECT id FROM links |
||||
WHERE itemID = $(safeSQL "$fileID") |
||||
AND tagID = $(safeSQL "$tagID")" | |
||||
sqlite3 "${sqliteFile}" |
||||
)" |
||||
|
||||
# If not, add a link |
||||
if [ -z "$existing" ]; then |
||||
echo "INSERT INTO links (itemID,tagID) |
||||
VALUES ($(safeSQL "$fileID"),$(safeSQL "$tagID"))" | |
||||
sqlite3 "${sqliteFile}" |
||||
fi |
||||
done <<<"$(echo "$tagIDs")" |
||||
tagIDsOr="$(echo "$tagIDsOr" | sed 's/^ OR //')" |
||||
|
||||
# Delete any links that are not in the list |
||||
echo "DELETE FROM links |
||||
WHERE itemID = $(safeSQL "$fileID") |
||||
AND NOT ( $tagIDsOr )" | |
||||
sqlite3 "${sqliteFile}" |
||||
|
||||
else # If there are no tags, simply delete any that are referenced |
||||
echo "DELETE FROM links WHERE itemID = '$(safeSQL "$fileID")'" | |
||||
sqlite3 "${sqliteFile}" |
||||
fi |
||||
|
||||
|
||||
} |
@ -0,0 +1,24 @@ |
||||
#!/usr/bin/env bash |
||||
|
||||
getYamlBlock(){ |
||||
vecho "getYamlBlock $*" |
||||
cd "$dataDir" || return |
||||
local filename |
||||
filename="$(findFile "$1")" |
||||
|
||||
sed -n '1 { /^---/ { :a N; /\n---/! ba; p} }' "$filename" | |
||||
sed '1d;$d;s/\t/ /g' |
||||
} |
||||
|
||||
getYamlTitle(){ |
||||
vecho "getYamlTitle $*" |
||||
cd "$dataDir" || return |
||||
getYamlBlock "$1" | yq -r '.Title' |
||||
} |
||||
|
||||
getYamlTags(){ |
||||
vecho "getYamlTitle $*" |
||||
cd "$dataDir" || return |
||||
getYamlBlock "$1" | yq -r '.Tags | join("\n")' |
||||
} |
||||
|
Loading…
Reference in new issue