Last active
August 29, 2015 13:57
-
-
Save arthur-e/9470338 to your computer and use it in GitHub Desktop.
Tool(s) to recover the Markdown contents from an older Springseed *.note file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Recover the Markdown content of an older Springseed note (*.note) file. Output | |
*.md file names will be the name of the Note file prepended by the Notebook | |
name, if available. | |
''' | |
import sys | |
import os | |
import re | |
import json | |
def notefile_to_markdown(path, output_path=None, extension='md'): | |
list_name = '' | |
list_hash = os.path.basename(path).split('.')[0] | |
regex = re.compile(r'(P?.*)\.[^.]*$') | |
with open(path, 'rb') as stream: | |
obj = json.loads(stream.read()) | |
# Try to determine the group (Notebook) name | |
if os.path.exists(os.path.join(os.curdir, list_hash + '.list')): | |
with open(os.path.join(os.curdir, list_hash + '.list'), 'rb') as stream: | |
list_name = json.loads(stream.read())['name'] | |
if output_path is None: | |
output_filename = obj['name'].replace(' ', '_') + '.' + extension | |
output_path = os.path.join(os.curdir, | |
'_'.join((list_name, output_filename))) | |
else: | |
assert regex.match(output_path) is not None, 'Invalid file path or filename' | |
# Change the file extension to the one specified (or default) | |
output_path = regex.match(output_path).groups()[0] | |
output_path += '.' + extension | |
# Extract Markdown contents; replace escaped newlines with actual newlines | |
content = obj['content'].replace('\\n', '\n') | |
try: | |
with open(output_path, 'wb') as stream: | |
stream.write(content.encode('utf-8')) | |
except UnicodeEncodeError: | |
sys.stderr.write('UnicodeEncodeError on: ' + path + '\n') | |
if __name__ == '__main__': | |
try: | |
path = sys.argv[1] | |
except IndexError: | |
raise Exception('You need to provide a path to the directory of *.note files as the first argument') | |
regex = re.compile(r'.*\.note$') | |
for filename in os.listdir(path): | |
# Skip anything but *.note files | |
if regex.match(filename) is None: | |
continue | |
notefile_to_markdown(os.path.join(path, filename)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment