Rename as 'yaml'

Without the first line being `---`, I don't know if it is completely
valid YAML
This commit is contained in:
William Minchin 2014-08-05 12:04:28 -06:00
parent dcfd15fb6a
commit 400d286254
2 changed files with 13 additions and 13 deletions

View file

@ -42,7 +42,7 @@ def parse_args(args=None):
exporting = parser.add_argument_group('Export / Import', 'Options for transmogrifying your journal') 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('--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('--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', 'pelican', 'pelican-md'], help='Export your journal. TYPE can be json, markdown, text, or pelican.', default=False, const=None) exporting.add_argument('--export', metavar='TYPE', dest='export', choices=['text', 'txt', 'markdown', 'md', 'json', 'yaml', 'yaml-md'], help='Export your journal. TYPE can be json, markdown, text, or yaml.', 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('-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('--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('--decrypt', metavar='FILENAME', dest='decrypt', help='Decrypts your journal and stores it in plain text', nargs='?', default=False, const=None)

View file

@ -65,8 +65,8 @@ def to_txt(journal):
"""Returns the complete text of the Journal.""" """Returns the complete text of the Journal."""
return journal.pprint() return journal.pprint()
def to_pelican(entry): def to_yaml(entry):
"""Returns a markdown representation of the Journal with Pelican formatted """Returns a markdown representation of the Journal with YAML formatted
front matter""" front matter"""
out = u'' out = u''
out += u'Title: {}\n'.format(entry.title) out += u'Title: {}\n'.format(entry.title)
@ -74,13 +74,13 @@ def to_pelican(entry):
if entry.tags: if entry.tags:
# drop tag symbol # drop tag symbol
out += u'Tags: {}\n'.format(', '.join([tag[1:] for tag in entry.tags])) out += u'Tags: {}\n'.format(', '.join([tag[1:] for tag in entry.tags]))
out += u'\n{}'.format(entry.body) out += u'---\n\n{}'.format(entry.body)
return out return out
def export(journal, format, output=None): def export(journal, format, output=None):
"""Exports the journal to various formats. """Exports the journal to various formats.
format should be one of json, txt, text, md, markdown, pelican, pelican-md. format should be one of json, txt, text, md, markdown, yaml, yaml-md.
If output is None, returns a unicode representation of the output. If output is None, returns a unicode representation of the output.
If output is a directory, exports entries into individual files. If output is a directory, exports entries into individual files.
Otherwise, exports to the given output file. Otherwise, exports to the given output file.
@ -91,16 +91,16 @@ def export(journal, format, output=None):
"text": to_txt, "text": to_txt,
"md": to_md, "md": to_md,
"markdown": to_md, "markdown": to_md,
"pelican": to_pelican, "yaml": to_yaml,
"pelican-md": to_pelican "yaml-md": to_yaml
} }
if format not in maps: if format not in maps:
return u"[ERROR: can't export to '{0}'. Valid options are 'md', 'txt', 'json', and 'pelican']".format(format) return u"[ERROR: can't export to '{0}'. Valid options are 'md', 'txt', 'json', and 'yaml']".format(format)
if output and os.path.isdir(output): # multiple files if output and os.path.isdir(output): # multiple files
return write_files(journal, output, format) return write_files(journal, output, format)
else: else:
if format is ("pelican" or "pelican-md"): if format is ("yaml" or "yaml-md"):
return u"Pelican exports must be to a directory." return u"YAML exports must be to a directory."
else: else:
content = maps[format](journal) content = maps[format](journal)
if output: if output:
@ -116,7 +116,7 @@ def export(journal, format, output=None):
def write_files(journal, path, format): def write_files(journal, path, format):
"""Turns your journal into separate files for each entry. """Turns your journal into separate files for each entry.
Format should be either json, md, txt or pelican.""" Format should be either json, md, txt or yaml."""
make_filename = lambda entry: e.date.strftime("%Y-%m-%d_{0}.{1}".format(slugify(u(e.title)), format)) make_filename = lambda entry: e.date.strftime("%Y-%m-%d_{0}.{1}".format(slugify(u(e.title)), format))
for e in journal.entries: for e in journal.entries:
if format == 'json': if format == 'json':
@ -125,9 +125,9 @@ def write_files(journal, path, format):
content = e.to_md() content = e.to_md()
elif format in ('txt', 'text'): elif format in ('txt', 'text'):
content = e.__unicode__() content = e.__unicode__()
elif format in ('pelican', 'pelican-md'): elif format in ('yaml', 'yaml-md'):
make_filename = lambda entry: e.date.strftime("%Y-%m-%d_{0}.{1}".format(slugify(u(e.title)), 'md')) make_filename = lambda entry: e.date.strftime("%Y-%m-%d_{0}.{1}".format(slugify(u(e.title)), 'md'))
content = to_pelican(e) content = to_yaml(e)
full_path = os.path.join(path, make_filename(e)) full_path = os.path.join(path, make_filename(e))
with codecs.open(full_path, "w", "utf-8") as f: with codecs.open(full_path, "w", "utf-8") as f:
f.write(content) f.write(content)