diff --git a/jrnl/DayOneJournal.py b/jrnl/DayOneJournal.py index 1941849d..a3e53826 100644 --- a/jrnl/DayOneJournal.py +++ b/jrnl/DayOneJournal.py @@ -4,6 +4,7 @@ from __future__ import absolute_import, unicode_literals from . import Entry from . import Journal +from . import time as jrnl_time import os import re from datetime import datetime @@ -53,6 +54,7 @@ class DayOne(Journal.Journal): entry = Entry.Entry(self, date, title, body, starred=dict_entry["Starred"]) entry.uuid = dict_entry["UUID"] entry.tags = [self.config['tagsymbols'][0] + tag for tag in dict_entry.get("Tags", [])] + self.entries.append(entry) self.sort() return self @@ -61,16 +63,19 @@ class DayOne(Journal.Journal): """Writes only the entries that have been modified into plist files.""" for entry in self.entries: if entry.modified: + utc_time = datetime.utcfromtimestamp(time.mktime(entry.date.timetuple())) + if not hasattr(entry, "uuid"): entry.uuid = uuid.uuid1().hex - utc_time = datetime.utcfromtimestamp(time.mktime(entry.date.timetuple())) - filename = os.path.join(self.config['journal'], "entries", entry.uuid + ".doentry") + + filename = os.path.join(self.config['journal'], "entries", entry.uuid.upper() + ".doentry") + entry_plist = { 'Creation Date': utc_time, 'Starred': entry.starred if hasattr(entry, 'starred') else False, 'Entry Text': entry.title + "\n" + entry.body, 'Time Zone': str(tzlocal.get_localzone()), - 'UUID': entry.uuid, + 'UUID': entry.uuid.upper(), 'Tags': [tag.strip(self.config['tagsymbols']).replace("_", " ") for tag in entry.tags] } plistlib.writePlist(entry_plist, filename) @@ -84,12 +89,11 @@ class DayOne(Journal.Journal): return "\n".join(["# {0}\n{1}".format(e.uuid, e.__unicode__()) for e in self.entries]) def parse_editable_str(self, edited): - """Parses the output of self.editable_str and updates it's entries.""" + """Parses the output of self.editable_str and updates its entries.""" # Method: create a new list of entries from the edited text, then match # UUIDs of the new entries against self.entries, updating the entries # if the edited entries differ, and deleting entries from self.entries # if they don't show up in the edited entries anymore. - date_length = len(datetime.today().strftime(self.config['timeformat'])) # Initialise our current entry entries = [] @@ -106,16 +110,18 @@ class DayOne(Journal.Journal): current_entry.modified = False current_entry.uuid = m.group(1).lower() else: - try: - new_date = datetime.strptime(line[:date_length], self.config['timeformat']) + date_blob_re = re.compile("^\[[^\\]]+\] ") + date_blob = date_blob_re.findall(line) + if date_blob: + date_blob = date_blob[0] + new_date = jrnl_time.parse(date_blob.strip(" []")) if line.endswith("*"): current_entry.starred = True line = line[:-1] - current_entry.title = line[date_length + 1:] + current_entry.title = line[len(date_blob) - 1:] current_entry.date = new_date - except ValueError: - if current_entry: - current_entry.body += line + "\n" + elif current_entry: + current_entry.body += line + "\n" # Append last entry if current_entry: diff --git a/jrnl/__init__.py b/jrnl/__init__.py index 794da406..796c58c4 100644 --- a/jrnl/__init__.py +++ b/jrnl/__init__.py @@ -11,4 +11,4 @@ __title__ = 'jrnl' __version__ = '2.0.0-rc1' __author__ = 'Manuel Ebert' __license__ = 'MIT License' -__copyright__ = 'Copyright 2013 - 2014 Manuel Ebert' +__copyright__ = 'Copyright 2013 - 2015 Manuel Ebert' diff --git a/jrnl/cli.py b/jrnl/cli.py index b1f653e1..5c4585ce 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -13,6 +13,7 @@ from . import Journal from . import util from . import install from . import plugins +from .util import WARNING_COLOR, ERROR_COLOR, RESET_COLOR import jrnl import argparse import sys @@ -252,7 +253,7 @@ def run(manual_args=None): elif args.edit: if not config['editor']: - util.prompt("[You need to specify an editor in {0} to use the --edit function.]".format(install.CONFIG_FILE_PATH)) + util.prompt("[{1}ERROR{2}: You need to specify an editor in {0} to use the --edit function.]".format(install.CONFIG_FILE_PATH, ERROR_COLOR, RESET_COLOR)) sys.exit(1) other_entries = [e for e in old_entries if e not in journal.entries] # Edit diff --git a/jrnl/plugins/markdown_exporter.py b/jrnl/plugins/markdown_exporter.py index 157e01b5..346a5a37 100644 --- a/jrnl/plugins/markdown_exporter.py +++ b/jrnl/plugins/markdown_exporter.py @@ -5,6 +5,7 @@ from __future__ import absolute_import, unicode_literals, print_function from .text_exporter import TextExporter import re import sys +from ..util import WARNING_COLOR, ERROR_COLOR, RESET_COLOR class MarkdownExporter(TextExporter): @@ -49,7 +50,7 @@ class MarkdownExporter(TextExporter): newbody = newbody + previous_line # add very last line if warn_on_heading_level is True: - print("{}WARNING{}: Headings increased past H6 on export - {} {}".format("\033[33m", "\033[0m", date_str, entry.title), file=sys.stderr) + print("{}WARNING{}: Headings increased past H6 on export - {} {}".format(WARNING_COLOR, RESET_COLOR, date_str, entry.title), file=sys.stderr) return "{md} {date} {title} {body} {space}".format( md=heading, diff --git a/jrnl/plugins/text_exporter.py b/jrnl/plugins/text_exporter.py index 042c66fd..6e550956 100644 --- a/jrnl/plugins/text_exporter.py +++ b/jrnl/plugins/text_exporter.py @@ -6,6 +6,7 @@ import codecs from . import BaseExporter from ..util import u, slugify import os +from ..util import WARNING_COLOR, ERROR_COLOR, RESET_COLOR class TextExporter(BaseExporter): @@ -31,7 +32,7 @@ class TextExporter(BaseExporter): f.write(cls.export_journal(journal)) return "[Journal exported to {0}]".format(path) except IOError as e: - return "[ERROR: {0} {1}]".format(e.filename, e.strerror) + return "[{2}ERROR{3}: {0} {1}]".format(e.filename, e.strerror, ERROR_COLOR, RESET_COLOR) @classmethod def make_filename(cls, entry): @@ -46,7 +47,7 @@ class TextExporter(BaseExporter): with codecs.open(full_path, "w", "utf-8") as f: f.write(cls.export_entry(entry)) except IOError as e: - return "[ERROR: {0} {1}]".format(e.filename, e.strerror) + return "[{2}ERROR{3}: {0} {1}]".format(e.filename, e.strerror, ERROR_COLOR, RESET_COLOR) return "[Journal exported to {0}]".format(path) @classmethod diff --git a/jrnl/plugins/yaml_exporter.py b/jrnl/plugins/yaml_exporter.py index 1e00cf2f..1442adcc 100644 --- a/jrnl/plugins/yaml_exporter.py +++ b/jrnl/plugins/yaml_exporter.py @@ -5,11 +5,11 @@ from __future__ import absolute_import, unicode_literals, print_function from .text_exporter import TextExporter import re import sys -import yaml +from ..util import WARNING_COLOR, ERROR_COLOR, RESET_COLOR -class MarkdownExporter(TextExporter): - """This Exporter can convert entries and journals into Markdown with YAML front matter.""" +class YAMLExporter(TextExporter): + """This Exporter can convert entries and journals into Markdown formatted text with YAML front matter.""" names = ["yaml"] extension = "md" @@ -24,9 +24,13 @@ class MarkdownExporter(TextExporter): body_wrapper = "\n" if entry.body else "" body = body_wrapper + entry.body + tagsymbols = entry.journal.config['tagsymbols'] + # see also Entry.Entry.rag_regex + multi_tag_regex = re.compile(r'(?u)^\s*([{tags}][-+*#/\w]+\s*)+$'.format(tags=tagsymbols), re.UNICODE) + '''Increase heading levels in body text''' newbody = '' - heading = '###' + heading = '#' previous_line = '' warn_on_heading_level = False for line in entry.body.splitlines(True): @@ -44,27 +48,36 @@ class MarkdownExporter(TextExporter): """Setext style H2""" newbody = newbody + heading + "## " + previous_line line = '' + elif multi_tag_regex.match(line): + """Tag only lines""" + line = '' else: newbody = newbody + previous_line previous_line = line newbody = newbody + previous_line # add very last line if warn_on_heading_level is True: - print("{}WARNING{}: Headings increased past H6 on export - {} {}".format("\033[33m", "\033[0m", date_str, entry.title), file=sys.stderr) + print("{}WARNING{}: Headings increased past H6 on export - {} {}".format(WARNING_COLOR, RESET_COLOR, date_str, entry.title), file=sys.stderr) - # top = yaml.dump(entry) + dayone_attributes = '' + if hasattr(entry, "uuid"): + dayone_attributes += 'uuid: ' + entry.uuid + '\n' + # TODO: copy over pictures, if present + # source directory is entry.journal.config['journal'] + # output directory is...? - return "title: {title}\ndate: {date}\nstared: {stared}\ntags: {tags}\n{body} {space}".format( - date=date_str, - title=entry.title, - stared=entry.starred, - tags=', '.join([tag[1:] for tag in entry.tags]), - body=newbody, + return "title: {title}\ndate: {date}\nstared: {stared}\ntags: {tags}\n{dayone} {body} {space}".format( + date = date_str, + title = entry.title, + stared = entry.starred, + tags = ', '.join([tag[1:] for tag in entry.tags]), + dayone = dayone_attributes, + body = newbody, space="" ) @classmethod def export_journal(cls, journal): """Returns an error, as YAML export requires a directory as a target.""" - print("{}ERROR{}: YAML export must be to individual files. Please specify a directory to export to.".format("\033[31m", "\033[0m", file=sys.stderr)) + print("{}ERROR{}: YAML export must be to individual files. Please specify a directory to export to.".format(ERROR_COLOR, RESET_COLOR), file=sys.stderr) return diff --git a/jrnl/util.py b/jrnl/util.py index 1fd99c24..7f8caaa1 100644 --- a/jrnl/util.py +++ b/jrnl/util.py @@ -23,6 +23,10 @@ STDOUT = sys.stdout TEST = False __cached_tz = None +WARNING_COLOR = "\033[33m" +ERROR_COLOR = "\033[31m" +RESET_COLOR = "\033[0m" + def getpass(prompt="Password: "): if not TEST: @@ -101,6 +105,7 @@ def py23_input(msg=""): def py23_read(msg=""): + print(msg) return STDIN.read() @@ -118,12 +123,11 @@ def load_config(config_path): def get_text_from_editor(config, template=""): - tmpfile = os.path.join(tempfile.mktemp(prefix="jrnl", suffix=".txt")) filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=".txt") with codecs.open(tmpfile, 'w', "utf-8") as f: if template: f.write(template) - subprocess.call(shlex.split(config['editor']) + [tmpfile]) + subprocess.call(shlex.split(config['editor'], posix="win" not in sys.platform) + [tmpfile]) with codecs.open(tmpfile, "r", "utf-8") as f: raw = f.read() os.close(filehandle)