@ -20,9 +20,6 @@ Tags:
-
---
" > "$filename"
echo "INSERT INTO items (filename, title, type)
VALUES ( '$(safeSQL "$filename")', '$(safeSQL "$title")', 'normal' );" |
sqlite3 "${sqliteFile}"
editFile "$filename"
}
@ -31,32 +28,62 @@ 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")"
updateFileChange "$filename"
}
# This is used to update the DB when a file is changed
# param1 is the file
updateFileChange(){
local filename
local title
local newFilename
filename="$(findFile "$1")"
[ ! -e "$filename" ] && die "No such file $1"
title="$(getYamlTitle "$filename")"
newFilename="$(escapeFilename "$title.md")"
echo "Filename: '$filename'
new filename: '$newFilename'"
if [ "$filename" = "$newFilename" ]; then
# The title hasn't changed
# check if the file is in the DB
if ! fileInDB "$filename"; then
# If not, add it
echo "INSERT INTO items (filename, title, type)
VALUES ( '$(safeSQL "$filename")',
'$(safeSQL "$title")', 'normal' );" |
sqlite3 "${sqliteFile}"
fi
# Make sure all the tags are up to date
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
gitChange "$filename"
else
mv "$filename" "$newfilename"
# The title has changed so we need to move the file
if [ -e "$newFilename" ]; then
# If the place we need to move it to already exists, die
die -e "$newFilename already exists
$filename title changed
Please resolve manually"
else
mv "$filename" "$newFilename"
if fileInDB "$filename"; then
echo "UPDATE items
SET (filename,title) = ('$(safeSQL "$newfilename")','$(safeSQL "$newTitle")')
SET (filename,title) =
('$(safeSQL "$newFilename")','$(safeSQL "$title")')
WHERE filename = '$(safeSQL "$filename")';" |
sqlite3 "${sqliteFile}"
gitChange "$newfilename"
fi
else
gitChange "$filename"
# We get here if the title was changed in the create process
echo "INSERT INTO items (filename, title, type)
VALUES ( '$(safeSQL "$newFilename")',
'$(safeSQL "$title")', 'normal' );" |
sqlite3 "${sqliteFile}"
fi
gitChange "$filename" "$newFilename"
fi
fi
}
@ -78,6 +105,7 @@ deleteFile(){
DELETE FROM links
WHERE itemID = '$(safeSQL "$fileID")';" |
sqlite3 --column --header "${sqliteFile}"
gitChange "$filename"
fi
}