[Black] apply black formatter to YAML Exporter

This commit is contained in:
MinchinWeb 2019-12-14 15:23:18 -07:00
parent 1ac96ae698
commit f568df86e4

View file

@ -10,6 +10,7 @@ from ..util import WARNING_COLOR, ERROR_COLOR, RESET_COLOR
class YAMLExporter(TextExporter): class YAMLExporter(TextExporter):
"""This Exporter can convert entries and journals into Markdown formatted text with YAML front matter.""" """This Exporter can convert entries and journals into Markdown formatted text with YAML front matter."""
names = ["yaml"] names = ["yaml"]
extension = "md" extension = "md"
@ -17,22 +18,26 @@ class YAMLExporter(TextExporter):
def export_entry(cls, entry, to_multifile=True): def export_entry(cls, entry, to_multifile=True):
"""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("{}ERROR{}: YAML export must be to individual files. Please \ print(
specify a directory to export to.".format(ERROR_COLOR, RESET_COLOR, file=sys.stderr)) "{}ERROR{}: YAML export must be to individual files. Please \
specify a directory to export to.".format(
ERROR_COLOR, RESET_COLOR, file=sys.stderr
)
)
return return
date_str = entry.date.strftime(entry.journal.config['timeformat']) date_str = entry.date.strftime(entry.journal.config["timeformat"])
body_wrapper = "\n" if entry.body else "" body_wrapper = "\n" if entry.body else ""
body = body_wrapper + entry.body body = body_wrapper + entry.body
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(fr'(?u)^\s*([{tagsymbols}][-+*#/\w]+\s*)+$') multi_tag_regex = re.compile(fr"(?u)^\s*([{tagsymbols}][-+*#/\w]+\s*)+$")
"""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 body.splitlines(True): for line in body.splitlines(True):
if re.match(r"#+ ", line): if re.match(r"#+ ", line):
@ -40,65 +45,81 @@ class YAMLExporter(TextExporter):
newbody = newbody + previous_line + heading + line newbody = newbody + previous_line + heading + line
if re.match(r"^#######+ ", heading + line): if re.match(r"^#######+ ", heading + line):
warn_on_heading_level = True warn_on_heading_level = True
line = '' line = ""
elif re.match(r"^=+$", line.rstrip()) and not re.match(r"^$", previous_line.strip()): elif re.match(r"^=+$", line.rstrip()) and not re.match(
r"^$", previous_line.strip()
):
"""Setext style H1""" """Setext style H1"""
newbody = newbody + heading + "# " + previous_line newbody = newbody + heading + "# " + previous_line
line = '' line = ""
elif re.match(r"^-+$", line.rstrip()) and not re.match(r"^$", previous_line.strip()): elif re.match(r"^-+$", line.rstrip()) and not re.match(
r"^$", previous_line.strip()
):
"""Setext style H2""" """Setext style H2"""
newbody = newbody + heading + "## " + previous_line newbody = newbody + heading + "## " + previous_line
line = '' line = ""
elif multi_tag_regex.match(line): elif multi_tag_regex.match(line):
"""Tag only lines""" """Tag only lines"""
line = '' line = ""
else: else:
newbody = newbody + previous_line newbody = newbody + previous_line
previous_line = line previous_line = line
newbody = newbody + previous_line # add very last line newbody = newbody + previous_line # add very last line
if warn_on_heading_level is True: if warn_on_heading_level is True:
print("{}WARNING{}: Headings increased past H6 on export - {} {}" print(
.format(WARNING_COLOR, RESET_COLOR, date_str, entry.title), file=sys.stderr) "{}WARNING{}: Headings increased past H6 on export - {} {}".format(
WARNING_COLOR, RESET_COLOR, date_str, entry.title
),
file=sys.stderr,
)
dayone_attributes = '' dayone_attributes = ""
if hasattr(entry, "uuid"): if hasattr(entry, "uuid"):
dayone_attributes += 'uuid: ' + entry.uuid + '\n' dayone_attributes += "uuid: " + entry.uuid + "\n"
if hasattr(entry, 'creator_device_agent') or \ if (
hasattr(entry, 'creator_generation_date') or \ hasattr(entry, "creator_device_agent")
hasattr(entry, 'creator_host_name') or \ or hasattr(entry, "creator_generation_date")
hasattr(entry, 'creator_os_agent') or \ or hasattr(entry, "creator_host_name")
hasattr(entry, 'creator_software_agent'): or hasattr(entry, "creator_os_agent")
dayone_attributes += 'creator:\n' or hasattr(entry, "creator_software_agent")
if hasattr(entry, 'creator_device_agent'): ):
dayone_attributes += f' device agent: {entry.creator_device_agent}\n' dayone_attributes += "creator:\n"
if hasattr(entry, 'creator_generation_date'): if hasattr(entry, "creator_device_agent"):
dayone_attributes += ' generation date: {}\n'.format(str(entry.creator_generation_date)) dayone_attributes += f" device agent: {entry.creator_device_agent}\n"
if hasattr(entry, 'creator_host_name'): if hasattr(entry, "creator_generation_date"):
dayone_attributes += f' host name: {entry.creator_host_name}\n' dayone_attributes += " generation date: {}\n".format(
if hasattr(entry, 'creator_os_agent'): str(entry.creator_generation_date)
dayone_attributes += f' os agent: {entry.creator_os_agent}\n' )
if hasattr(entry, 'creator_software_agent'): if hasattr(entry, "creator_host_name"):
dayone_attributes += f' software agent: {entry.creator_software_agent}\n' 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 # TODO: copy over pictures, if present
# source directory is entry.journal.config['journal'] # source directory is entry.journal.config['journal']
# output directory is...? # output directory is...?
return "title: {title}\ndate: {date}\nstared: {stared}\ntags: {tags}\n{dayone} {body} {space}" \ return "title: {title}\ndate: {date}\nstared: {stared}\ntags: {tags}\n{dayone} {body} {space}".format(
.format( date=date_str,
date=date_str, title=entry.title,
title=entry.title, stared=entry.starred,
stared=entry.starred, tags=", ".join([tag[1:] for tag in entry.tags]),
tags=', '.join([tag[1:] for tag in entry.tags]), dayone=dayone_attributes,
dayone=dayone_attributes, body=newbody,
body=newbody, space="",
space="" )
)
@classmethod @classmethod
def export_journal(cls, journal): def export_journal(cls, journal):
"""Returns an error, as YAML export requires a directory as a target.""" """Returns an error, as YAML export requires a directory as a target."""
print("{}ERROR{}: YAML export must be to individual files. \ print(
Please specify a directory to export to.".format(ERROR_COLOR, RESET_COLOR), file=sys.stderr) "{}ERROR{}: YAML export must be to individual files. \
Please specify a directory to export to.".format(
ERROR_COLOR, RESET_COLOR
),
file=sys.stderr,
)
return return