Skip to content

Instantly share code, notes, and snippets.

@vanzaj
Created November 29, 2016 09:05
Show Gist options
  • Save vanzaj/89393597d1c5fbd9d2ab1a0b8a0d76e5 to your computer and use it in GitHub Desktop.
Save vanzaj/89393597d1c5fbd9d2ab1a0b8a0d76e5 to your computer and use it in GitHub Desktop.
Convert evernote (enex) files to markdown
#!/usr/bin/env python
# coding=utf-8
#
# convert evernote enex files to markdown
# depends on https://github.com/claytron/ever2simple
# assumes enex files are named as CATEG-foo-bar.enex
import datetime
import glob
import json
import os
import re
import subprocess
opath = os.path
ENEX_DIR = opath.expanduser('~/tmp/evernotes_enex')
DST_DIR = opath.expanduser('~/tmp/evernotes_md')
def tmp_filename(prefix='tmp_file'):
return '{}.{}'.format(prefix, os.getpid())
def convert2json(fname):
'''
ever2simple -o /tmp/xxx -f json fname
load json conten from /tmp/xxx
remove /tmp/xxx
'''
tmp_file = opath.join('/tmp', tmp_filename('enex'))
args = ['ever2simple', '-o', tmp_file, '-f json', fname]
subprocess.check_call(args, shell=True)
with open(tmp_file) as fp:
content = json.load(fp)
os.unlink(tmp_file)
return content[0]
def parse_date(text):
''' convert 'May 24 2013 00:44:45' to datetime object
'''
return datetime.datetime.strptime(text, '%b %d %Y %H:%M:%S')
def date2yymmdd(date):
return '{}{:02d}{:02d}'.format(date.year, date.month, date.day)
def tags2str(tags):
tt = ', '.join(map(lambda x: '"' + x + '"', tags))
return '[ {} ]'.format(tt)
def make_header(title, cdate, categ, tags):
txt = '+++\n'
txt += 'title = "{}"\n'.format(title)
txt += 'date = "{}"\n'.format(cdate.isoformat())
txt += 'categories = [ "{}" ]\n'.format(categ)
txt += 'tags = {}\n'.format(tags2str(tags))
txt += '\n'
txt += '+++\n\n'
return txt
def make_body(content):
body = re.sub(r'\ \ \n', '\n', content)
body = re.sub(r'\n\*\*\n', '\n\n', body)
body = re.sub(r'\n\n+', '\n\n', body)
body = re.sub(r'&', '\&', body)
body = re.sub(r'(-)*\>', '`-->`', body)
body = re.sub(r'\&lt;(-)*', '`<--`', body)
return body.encode('utf-8')
def save_md(json_data, name, dest='.'):
if not opath.isdir(dest):
os.mkdir(dest)
name_parts = name.split('-')
title = ' '.join(name_parts[1:]).capitalize()
categ = name_parts[0]
tags = json_data['tags']
if categ in tags and len(tags) > 1:
tags.remove(categ)
cdate = parse_date(json_data['createdate'])
file_name = date2yymmdd(cdate) + '-' + name + '.md'
full_name = opath.join(dest, file_name)
with open(full_name, 'w') as fp:
fp.write(make_header(title, cdate, categ, tags))
fp.write(make_body(json_data['content']))
list_of_enex = glob.glob(opath.join(ENEX_DIR, '*.enex'))
for each_file in list_of_enex:
json_data = convert2json(each_file)
base_name, _ext = opath.splitext(opath.basename(each_file))
save_md(json_data, base_name, dest=DST_DIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment