diff --git a/CHANGELOG.md b/CHANGELOG.md index a0a5e984..0f7cd1b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,12 @@ Changelog ========= +#### 1.1.0 + +* [New] JSON export exports tags as well. #### 1.0.5 -* Backwards compatibility with `parsedatetime` 0.8.7 +* [Improved] Backwards compatibility with `parsedatetime` 0.8.7 #### 1.0.4 diff --git a/jrnl/__init__.py b/jrnl/__init__.py index 8e629323..4c3a2b79 100644 --- a/jrnl/__init__.py +++ b/jrnl/__init__.py @@ -7,7 +7,7 @@ jrnl is a simple journal application for your command line. """ __title__ = 'jrnl' -__version__ = '1.0.5' +__version__ = '1.1.0' __author__ = 'Manuel Ebert' __license__ = 'MIT License' __copyright__ = 'Copyright 2013 Manuel Ebert' diff --git a/jrnl/exporters.py b/jrnl/exporters.py index dfca5d78..d7a730d6 100644 --- a/jrnl/exporters.py +++ b/jrnl/exporters.py @@ -3,10 +3,16 @@ try: import simplejson as json except ImportError: import json +from jrnl import get_tags_count def to_json(journal): """Returns a JSON representation of the Journal.""" - return json.dumps([e.to_dict() for e in journal.entries], indent=2) + tags = get_tags_count(journal) + result = { + "tags": dict((tag, count) for count, tag in tags), + "entries": [e.to_dict() for e in journal.entries] + } + return json.dumps(result, indent=2) def to_md(journal): """Returns a markdown representation of the Journal""" diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index 20fa7e0f..5b705d17 100755 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -104,23 +104,29 @@ def decrypt(journal, filename=None): journal.write(filename) print("Journal decrypted to {0}.".format(filename or journal.config['journal'])) + +def get_tags_count(journal): + """Returns a set of tuples (count, tag) for all tags present in the journal.""" + # Astute reader: should the following line leave you as puzzled as me the first time + # I came across this construction, worry not and embrace the ensuing moment of enlightment. + tags = [tag + for entry in journal.entries + for tag in set(entry.tags) + ] + # To be read: [for entry in journal.entries: for tag in set(entry.tags): tag] + tag_counts = set([(tags.count(tag), tag) for tag in tags]) + return tag_counts + def print_tags(journal): - """Prints a list of all tags and the number of occurances.""" - # Astute reader: should the following line leave you as puzzled as me the first time - # I came across this construction, worry not and embrace the ensuing moment of enlightment. - tags = [tag - for entry in journal.entries - for tag in set(entry.tags) - ] - # To be read: [for entry in journal.entries: for tag in set(entry.tags): tag] - tag_counts = set([(tags.count(tag), tag) for tag in tags]) - if not tag_counts: - print('[No tags found in journal.]') - elif min(tag_counts)[0] == 0: - tag_counts = filter(lambda x: x[0] > 1, tag_counts) - print('[Removed tags that appear only once.]') - for n, tag in sorted(tag_counts, reverse=False): - print("{0:20} : {1}".format(tag, n)) + """Prints a list of all tags and the number of occurances.""" + tag_counts = get_tags_count(journal) + if not tag_counts: + print('[No tags found in journal.]') + elif min(tag_counts)[0] == 0: + tag_counts = filter(lambda x: x[0] > 1, tag_counts) + print('[Removed tags that appear only once.]') + for n, tag in sorted(tag_counts, reverse=False): + print("{0:20} : {1}".format(tag, n)) def touch_journal(filename):