Skip to content

Instantly share code, notes, and snippets.

@amelandri
Last active August 31, 2021 16:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amelandri/555fdac374a24896f3be2f6ad32e0521 to your computer and use it in GitHub Desktop.
Save amelandri/555fdac374a24896f3be2f6ad32e0521 to your computer and use it in GitHub Desktop.
This code is outdated, please see https://github.com/amelandri/NotableUtilities
"""
*************************************************************************************
This code is outdated, please see https://github.com/amelandri/NotableUtilities
*************************************************************************************
A custom script developed by Alessandro Melandri (@AleMelandri) to reorder Notable (https://notable.md) notes in folders matching Notebooks tags.
Place it in your Notable home folder, run it and enjoy.
I'm not connected to Notable developers. I wrote this script for my personal use. It can containt bugs and cause data loss
***** TAKE A FULL BACKUP OF YOUR NOTES BEFORE USING THIS SCRIPT *****
***** USE IT AT YOUR OWN RISK *****
Feel free to adapt or modify it.
"""
import os
def main():
baseDirName='./notes'
archiveDirName = os.path.join(baseDirName,'Archive')
templateDirName = os.path.join(baseDirName,'Templates')
if (os.path.exists(archiveDirName) == False) :
os.makedirs(archiveDirName)
if (os.path.exists(templateDirName) == False) :
os.makedirs(templateDirName)
listOfFiles = list()
for (dirpath, dirnames, filenames) in os.walk(baseDirName) :
for file in filenames :
if (file.endswith('.md')) :
fo = open(os.path.join(dirpath, file), encoding="utf8")
line = fo.readline()
line_no = 1
while line != '' :
index = line.find('tags:')
indexArchive = line.find('archived: true')
if ( indexArchive != -1) :
currentDirName = os.path.join(dirpath, file)
if (currentDirName != os.path.join(archiveDirName + os.sep + file)) :
fo.close()
os.rename(os.path.join(dirpath, file), os.path.join(archiveDirName + os.sep + file))
break
if ( index != -1 & line.count('Templates/') == 1) :
tags = (line.replace('tags: [','').replace(']','').strip().split(','))
for tag in tags:
tag = tag.strip()
if (tag.find('Templates/') != -1) :
tagName = tag[tag.find('/')+1:]
newDirName = os.path.join(templateDirName,tagName)
oldDirName = os.path.join(dirpath, file)
if (os.path.exists(newDirName) == False) :
os.makedirs(newDirName)
if (oldDirName != os.path.join(newDirName + os.sep + file)) :
fo.close()
os.rename(os.path.join(dirpath, file), os.path.join(newDirName + os.sep + file))
break
fo.close()
break
if ( index != -1 & line.count('Notebooks/') == 1) :
tags = (line.replace('tags: [','').replace(']','').strip().split(','))
for tag in tags:
tag = tag.strip()
if (tag.find('Notebooks/') != -1) :
tagName = tag[tag.find('/')+1:]
newDirName = os.path.join(baseDirName,tagName)
oldDirName = os.path.join(dirpath, file)
if (os.path.exists(newDirName) == False) :
os.makedirs(newDirName)
if (oldDirName != os.path.join(newDirName + os.sep + file)) :
fo.close()
os.rename(os.path.join(dirpath, file), os.path.join(newDirName + os.sep + file))
break
fo.close()
break
line = fo.readline()
line_no += 1
fo.close()
if __name__ == '__main__':
main()
@amelandri
Copy link
Author

I've update the script to support Archive and Templates folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment