diff --git a/README.rst b/README.rst index 31f4cc8..15d07ea 100644 --- a/README.rst +++ b/README.rst @@ -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 @@ -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) diff --git a/ever2simple/converter.py b/ever2simple/converter.py index 4a3f849..39ad147 100644 --- a/ever2simple/converter.py +++ b/ever2simple/converter.py @@ -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 @@ -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: @@ -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: @@ -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') @@ -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')) diff --git a/ever2simple/core.py b/ever2simple/core.py index cfdcf33..50a1dca 100644 --- a/ever2simple/core.py +++ b/ever2simple/core.py @@ -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()