Allow export to Pelican formatted Markdown

This commit is contained in:
William Minchin 2014-07-29 00:43:26 -06:00
parent 09e0e44363
commit 115a5c705d
3 changed files with 38 additions and 18 deletions

View file

@ -42,8 +42,8 @@ def parse_args(args=None):
exporting = parser.add_argument_group('Export / Import', 'Options for transmogrifying your journal')
exporting.add_argument('--short', dest='short', action="store_true", help='Show only titles or line containing the search tags')
exporting.add_argument('--tags', dest='tags', action="store_true", help='Returns a list of all tags and number of occurences')
exporting.add_argument('--export', metavar='TYPE', dest='export', choices=['text', 'txt', 'markdown', 'md', 'json'], help='Export your journal. TYPE can be json, markdown, or text.', default=False, const=None)
exporting.add_argument('-o', metavar='OUTPUT', dest='output', help='Optionally specifies output file when using --export. If OUTPUT is a directory, exports each entry into an individual file instead.', default=False, const=None)
exporting.add_argument('--export', metavar='TYPE', dest='export', choices=['text', 'txt', 'markdown', 'md', 'json', 'pelican', 'pelican-md'], help='Export your journal. TYPE can be json, markdown, text, or pelican.', default=False, const=None)
exporting.add_argument('-o', metavar='OUTPUT', dest='output', help='Optionally specifies output file when using --export. If OUTPUT is a directory, exports each entry into an individual file instead (required for pelican exports).', default=False, const=None)
exporting.add_argument('--encrypt', metavar='FILENAME', dest='encrypt', help='Encrypts your existing journal with a new password', nargs='?', default=False, const=None)
exporting.add_argument('--decrypt', metavar='FILENAME', dest='decrypt', help='Decrypts your journal and stores it in plain text', nargs='?', default=False, const=None)
exporting.add_argument('--edit', dest='edit', help='Opens your editor to edit the selected entries.', action="store_true")

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python
# encoding: utf-8
from __future__ import absolute_import
@ -65,10 +65,23 @@ def to_txt(journal):
"""Returns the complete text of the Journal."""
return journal.pprint()
def to_pelican(entry):
"""Returns a markdown representation of the Journal with Pelican formatted
front matter"""
out = ''
out = out + 'Title: ' + entry.title + '\n'
out = out + 'Date: ' + str(entry.date) + '\n'
if entry.tags:
# drop tag symbol
out = out + 'Tags: ' + ', '.join([tag[1:] for tag in entry.tags]) + '\n'
out += '\n'
out += entry.body
return out
def export(journal, format, output=None):
"""Exports the journal to various formats.
format should be one of json, txt, text, md, markdown.
format should be one of json, txt, text, md, markdown, pelican, pelican-md.
If output is None, returns a unicode representation of the output.
If output is a directory, exports entries into individual files.
Otherwise, exports to the given output file.
@ -78,12 +91,17 @@ def export(journal, format, output=None):
"txt": to_txt,
"text": to_txt,
"md": to_md,
"markdown": to_md
"markdown": to_md,
"pelican": to_pelican,
"pelican-md": to_pelican
}
if format not in maps:
return u"[ERROR: can't export to '{0}'. Valid options are 'md', 'txt', and 'json']".format(format)
return u"[ERROR: can't export to '{0}'. Valid options are 'md', 'txt', 'json', and 'pelican']".format(format)
if output and os.path.isdir(output): # multiple files
return write_files(journal, output, format)
else:
if format is ("pelican" or "pelican-md"):
return u"Pelican exports must be to a directory."
else:
content = maps[format](journal)
if output:
@ -99,16 +117,19 @@ def export(journal, format, output=None):
def write_files(journal, path, format):
"""Turns your journal into separate files for each entry.
Format should be either json, md or txt."""
Format should be either json, md, txt or pelican."""
make_filename = lambda entry: e.date.strftime("%Y-%m-%d_{0}.{1}".format(slugify(u(e.title)), format))
for e in journal.entries:
full_path = os.path.join(path, make_filename(e))
if format == 'json':
content = json.dumps(e.to_dict(), indent=2) + "\n"
elif format in ('md', 'markdown'):
content = e.to_md()
elif format in ('txt', 'text'):
content = e.__unicode__()
elif format in ('pelican', 'pelican-md'):
make_filename = lambda entry: e.date.strftime("%Y-%m-%d_{0}.{1}".format(slugify(u(e.title)), 'md'))
content = to_pelican(e)
full_path = os.path.join(path, make_filename(e))
with codecs.open(full_path, "w", "utf-8") as f:
f.write(content)
return u"[Journal exported individual files in {0}]".format(path)

View file

@ -145,7 +145,6 @@ def int2byte(i):
This is equivalent to chr() in Python 2 and bytes((i,)) in Python 3."""
return chr(i) if PY2 else bytes((i,))
def byte2int(b):
"""Converts a byte to an integer.
This is equivalent to ord(bs[0]) on Python 2 and bs[0] on Python 3."""