Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Command Line Help
The help given by running ``ever2simple -h``::


usage: ever2simple [-h] [-o OUTPUT] [-f {json,csv,dir}] enex-file
usage: ever2simple [-h] [-o OUTPUT] [-t] [-f {json,csv,dir}] enex-file

Convert Evernote.enex files to Markdown

Expand All @@ -100,6 +100,8 @@ The help given by running ``ever2simple -h``::
-o OUTPUT, --output OUTPUT
the path to the output file or directory, leave black
to output to the terminal (stdout) (default: None)
-t, --title use the title of the note for the filename of the
generated file (default: False)
-f {json,csv,dir}, --format {json,csv,dir}
the output format, json, csv or a directory (default:
json)
Expand Down
11 changes: 9 additions & 2 deletions ever2simple/converter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import sys
import re
from csv import DictWriter
from cStringIO import StringIO
from dateutil.parser import parse
Expand All @@ -14,8 +15,9 @@ class EverConverter(object):

fieldnames = ['createdate', 'modifydate', 'content', 'tags']
date_fmt = '%h %d %Y %H:%M:%S'
title_space_char = '_'

def __init__(self, enex_filename, simple_filename=None, fmt='json'):
def __init__(self, enex_filename, simple_filename=None, fmt='json', titles_as_filenames=None):
self.enex_filename = os.path.expanduser(enex_filename)
self.stdout = False
if simple_filename is None:
Expand All @@ -24,6 +26,7 @@ def __init__(self, enex_filename, simple_filename=None, fmt='json'):
else:
self.simple_filename = os.path.expanduser(simple_filename)
self.fmt = fmt
self.titles_as_filenames = titles_as_filenames

def _load_xml(self, enex_file):
try:
Expand All @@ -41,6 +44,7 @@ def prepare_notes(self, xml_tree):
for note in raw_notes:
note_dict = {}
title = note.xpath('title')[0].text
note_dict['title'] = title
# Use dateutil to figure out these dates
# 20110610T182917Z
created_string = parse('19700101T000017Z')
Expand Down Expand Up @@ -121,6 +125,9 @@ def _convert_dir(self, notes):
elif not os.path.exists(self.simple_filename):
os.makedirs(self.simple_filename)
for i, note in enumerate(notes):
output_file_path = os.path.join(self.simple_filename, str(i) + '.txt')
filename = str(i)
if self.titles_as_filenames is not None:
filename = re.sub(r'\W+', self.title_space_char, note['title'])
output_file_path = os.path.join(self.simple_filename, filename + '.txt')
with open(output_file_path, 'w') as output_file:
output_file.write(note['content'].encode(encoding='utf-8'))
4 changes: 3 additions & 1 deletion ever2simple/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ def main():
parser = argparse.ArgumentParser(prog=None, description="Convert Evernote.enex files to Markdown", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('enex-file', help="the path to the Evernote.enex file")
parser.add_argument('-o', '--output', help="the path to the output file or directory, leave black to output to the terminal (stdout)")
parser.add_argument('-t', '--title', help="use the title of the note for the filename of the generated file", action='store_true')
parser.add_argument('-f', '--format', help="the output format, json, csv or a directory", choices=['json', 'csv', 'dir'], default='json')
args = parser.parse_args()
enex_file = vars(args)['enex-file']
output = args.output
fmt = args.format
filepath = os.path.expanduser(enex_file)
titles_as_filenames = args.title
if not os.path.exists(filepath):
print 'File does not exist: %s' % filepath
sys.exit(1)
converter = EverConverter(filepath, simple_filename=output, fmt=fmt)
converter = EverConverter(filepath, simple_filename=output, fmt=fmt, titles_as_filenames=titles_as_filenames)
converter.convert()
sys.exit()

Expand Down