Skip to content

Instantly share code, notes, and snippets.

@ReneHamburger1993
Forked from weaming/boostnote2md.py
Last active September 7, 2020 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReneHamburger1993/50a8abaece3e6ce2a4f34ac916ab73eb to your computer and use it in GitHub Desktop.
Save ReneHamburger1993/50a8abaece3e6ce2a4f34ac916ab73eb to your computer and use it in GitHub Desktop.
Convert boostnote cson format data to markdown
#!/usr/bin/env python3
# coding: utf-8
"""
Author : weaming
Created Time : 2018-05-26 21:32:59
Prerequisite:
python3 -m pip install cson arrow
"""
import json
import os
import sys
import datetime
import cson
try:
import arrow
time_aware = True
except ImportError:
print(
'warning: datetime information will be discarded unless you install arrow'
)
time_aware = False
def read_file(fp):
with open(fp) as f:
return f.read()
def text_to_dict(text):
"""convert json or cson to dict"""
try:
return cson.loads(text)
except:
pass
try:
return json.loads(text)
except:
pass
raise Exception("text should be in cson or json format")
def read_folder_names(fp):
print("filename:", fp)
data = text_to_dict(read_file(fp))
# structure of json file slightly changed
res = {key:x['_id'] for key, x in data['folderMap'].items()}
print(res)
return res
def write_boostnote_markdown(data, output, folder_map):
"""write boostnote dict to markdown"""
print(r"output", output)
# maybe more complex than necessary path handling for windows
# somewhat problematic but worked with the lines below
#target_dir = os.path.join(output, folder_map[data['folder']])
target_dir = folder_map[data['folderPathname']].split(":")[-1]
print("os.path.normpath(target_dir):", os.path.normpath(target_dir))
print(r"target_dir", target_dir)
print("os.getcwd():", os.getcwd())
print("os.sep:", os.sep)
target_dir = os.path.join(output, target_dir)
print(r"target_dir", target_dir)
target_folder = os.path.join(os.getcwd(), output)
target_dir = os.path.join(target_folder, "."+target_dir)
print(r"target_dir", target_dir)
target_dir = os.path.normpath(target_dir)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
print(r"output", output)
print(r"target_dir", target_dir)
target_file = os.path.join(target_dir, '{}.md'.format(data['title'].replace('/', '-').replace(" ","_")))
with open(target_file, 'w') as f:
f.write(data.get('content', ''))
print(target_file)
#if time_aware:
# did not work for me with arrow version 0.16
# did not further test why
if 0:
update_at = arrow.get(data['updatedAt'])
update_at_epoch = int(update_at.strftime('%s'))
os.utime(target_file, (update_at_epoch, update_at_epoch))
stat = os.stat(target_file)
def process_file(source, output, folder_map):
data = text_to_dict(read_file(source))
#print("data:", data, flush=True)
#print("folder_map:", folder_map, flush=True)
#print("folder_map keys:", folder_map.keys(), flush=True)
write_boostnote_markdown(data, output, folder_map)
def main(boostnote_dir, output):
"""
:input: input folder path
:output: output folder path
"""
folder_map = read_folder_names(os.path.join(boostnote_dir, 'boostnote.json'))
notes_dir = os.path.join(boostnote_dir, 'notes')
for name in os.listdir(notes_dir):
#if not name.endswith('.cson'):
# files have been renamed to '.json' instead of '.cson'
if not name.endswith('.json'):
continue
source = os.path.join(notes_dir, name)
process_file(source, output, folder_map)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description="convert boostnote cson format data to markdown")
parser.add_argument(
'-s',
'--source',
type=str,
help="directory store the cson files",
default=".")
parser.add_argument(
'-o', '--output', type=str, help="output directory", default="output")
args = parser.parse_args()
main(args.source, args.output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment