[DayOne] YAML export improvements (#773)

* [YAML Export] code style improvements

* [Dayone] Brings back extended Dayone attributes to YAML export
c.f. 7d3afd811b
reverse b8dbf84753

* [YAML Exporter] switch to f-strings

* [Black] apply black formatter to YAML Exporter

* Code fixes as per review
This commit is contained in:
MinchinWeb 2020-01-25 18:01:55 -07:00
parent fdcbf8abc4
commit 2b605f8177

View file

@ -19,11 +19,10 @@ class YAMLExporter(TextExporter):
"""Returns a markdown representation of a single entry, with YAML front matter.""" """Returns a markdown representation of a single entry, with YAML front matter."""
if to_multifile is False: if to_multifile is False:
print( print(
"{}ERROR{}: YAML export must be to individual files. " "{}ERROR{}: YAML export must be to individual files. Please \
"Please specify a directory to export to.".format( specify a directory to export to.".format(
"\033[31m", "\033[0m" ERROR_COLOR, RESET_COLOR, file=sys.stderr
), )
file=sys.stderr,
) )
return return
@ -33,16 +32,14 @@ class YAMLExporter(TextExporter):
tagsymbols = entry.journal.config["tagsymbols"] tagsymbols = entry.journal.config["tagsymbols"]
# see also Entry.Entry.rag_regex # see also Entry.Entry.rag_regex
multi_tag_regex = re.compile( multi_tag_regex = re.compile(fr"(?u)^\s*([{tagsymbols}][-+*#/\w]+\s*)+$")
r"(?u)^\s*([{tags}][-+*#/\w]+\s*)+$".format(tags=tagsymbols)
)
"""Increase heading levels in body text""" """Increase heading levels in body text"""
newbody = "" newbody = ""
heading = "#" heading = "#"
previous_line = "" previous_line = ""
warn_on_heading_level = False warn_on_heading_level = False
for line in entry.body.splitlines(True): for line in body.splitlines(True):
if re.match(r"^#+ ", line): if re.match(r"^#+ ", line):
"""ATX style headings""" """ATX style headings"""
newbody = newbody + previous_line + heading + line newbody = newbody + previous_line + heading + line
@ -80,9 +77,32 @@ class YAMLExporter(TextExporter):
dayone_attributes = "" dayone_attributes = ""
if hasattr(entry, "uuid"): if hasattr(entry, "uuid"):
dayone_attributes += "uuid: " + entry.uuid + "\n" dayone_attributes += "uuid: " + entry.uuid + "\n"
# TODO: copy over pictures, if present if (
# source directory is entry.journal.config['journal'] hasattr(entry, "creator_device_agent")
# output directory is...? or hasattr(entry, "creator_generation_date")
or hasattr(entry, "creator_host_name")
or hasattr(entry, "creator_os_agent")
or hasattr(entry, "creator_software_agent")
):
dayone_attributes += "creator:\n"
if hasattr(entry, "creator_device_agent"):
dayone_attributes += f" device agent: {entry.creator_device_agent}\n"
if hasattr(entry, "creator_generation_date"):
dayone_attributes += " generation date: {}\n".format(
str(entry.creator_generation_date)
)
if hasattr(entry, "creator_host_name"):
dayone_attributes += f" host name: {entry.creator_host_name}\n"
if hasattr(entry, "creator_os_agent"):
dayone_attributes += f" os agent: {entry.creator_os_agent}\n"
if hasattr(entry, "creator_software_agent"):
dayone_attributes += (
f" software agent: {entry.creator_software_agent}\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{dayone} {body} {space}".format( return "title: {title}\ndate: {date}\nstared: {stared}\ntags: {tags}\n{dayone} {body} {space}".format(
date=date_str, date=date_str,