Skip to content

Instantly share code, notes, and snippets.

@erinc
Created December 12, 2021 05:37
Show Gist options
  • Save erinc/86bc287455a680f02f09b65c04db2af5 to your computer and use it in GitHub Desktop.
Save erinc/86bc287455a680f02f09b65c04db2af5 to your computer and use it in GitHub Desktop.
import datetime
import os
import re
CRAFT_FOLDER = "craft"
JOURNALS_PATH = CRAFT_FOLDER + '/journals'
PAGES_PATH = CRAFT_FOLDER + '/pages'
def fix_content():
def date_suffix(d):
return 'th' if 11<=d<=13 else {1:'st',2:'nd',3:'rd'}.get(d%10, 'th')
def custom_strftime(format, t):
return t.strftime(format).replace('{S}', str(t.day) + date_suffix(t.day))
for count, filename in enumerate(os.listdir(CRAFT_FOLDER)):
md_file = f"{CRAFT_FOLDER}/{filename}"
if not os.path.isdir(md_file) and '.DS_Store' not in md_file:
# print(md_file)
a_file = open(md_file, "r")
lines = a_file.readlines()
if not lines:
continue
# Remove page title in content
if lines[0][0] == '#':
del(lines[0])
# Remote empty line at top after page title
if not lines[0].strip():
del(lines[0])
new_file = open(md_file, "w+")
for line in lines:
# Fix todos
line = line.replace('[x]', 'DONE')
line = line.replace('{{{[[DONE]]}}}}', 'DONE') # Roam legacy
line = line.replace('{{{[[DONE]]}}}', 'DONE') # Roam legacy
line = line.replace('{[[DONE]]}}', 'DONE') # Roam legacy
line = line.replace('[ ]', 'TODO')
# Fix craft specific date references
# TODO: If the line has multiple date references, we only fix the first occurance
if 'day://' in line:
x = re.search(r"\[[^\]]*\]\(([^()]*)\)", line)
if x:
date_ref = line[x.span()[0]:x.span()[1]]
if 'day://' in date_ref:
datetime_object = datetime.datetime.strptime(date_ref[-11:-1], "%Y.%m.%d")
new_date_str = custom_strftime('%b {S}, %Y', datetime_object) # ex. Dec 7th, 2021
new_date_str = f"[[{new_date_str}]]"
newline = line[:x.span()[0]] + new_date_str + line[x.span()[1]:]
line = newline
new_file.write(line)
new_file.close()
def create_logseq_folders():
if not os.path.exists(JOURNALS_PATH):
print('Creating journals folder')
os.makedirs(JOURNALS_PATH)
if not os.path.exists(PAGES_PATH):
print('Creating pages folder')
os.makedirs(PAGES_PATH)
def move_files():
for count, filename in enumerate(os.listdir(CRAFT_FOLDER)):
md_file = f"{CRAFT_FOLDER}/{filename}"
if os.path.isdir(md_file):
continue
if filename[:-3].replace('.', '').isdigit() and '.' in filename[:-3]:
print(filename[:-3].replace('.', '_'))
dst = filename[:-3].replace('.', '_') + '.md'
src =f"{CRAFT_FOLDER}/{filename}"
dst =f"{JOURNALS_PATH}/{dst}"
os.rename(src, dst)
else:
src =f"{CRAFT_FOLDER}/{filename}"
dst =f"{PAGES_PATH}/{filename}"
os.rename(src, dst)
if __name__ == '__main__':
fix_content()
create_logseq_folders()
move_files()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment