1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# encoding: utf-8 |
3
|
|
|
|
4
|
|
|
import os |
5
|
|
|
import re |
6
|
|
|
import sys |
7
|
|
|
|
8
|
|
|
from jrnl.color import RESET_COLOR |
9
|
|
|
from jrnl.color import WARNING_COLOR |
10
|
|
|
|
11
|
|
|
from .text_exporter import TextExporter |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class MarkdownExporter(TextExporter): |
15
|
|
|
"""This Exporter can convert entries and journals into Markdown.""" |
16
|
|
|
|
17
|
|
|
names = ["md", "markdown"] |
18
|
|
|
extension = "md" |
19
|
|
|
|
20
|
|
|
@classmethod |
21
|
|
|
def export_entry(cls, entry, to_multifile=True): |
22
|
|
|
"""Returns a markdown representation of a single entry.""" |
23
|
|
|
date_str = entry.date.strftime(entry.journal.config["timeformat"]) |
24
|
|
|
body_wrapper = "\n" if entry.body else "" |
25
|
|
|
body = body_wrapper + entry.body |
26
|
|
|
|
27
|
|
|
if to_multifile is True: |
28
|
|
|
heading = "#" |
29
|
|
|
else: |
30
|
|
|
heading = "###" |
31
|
|
|
|
32
|
|
|
"""Increase heading levels in body text""" |
33
|
|
|
newbody = "" |
34
|
|
|
previous_line = "" |
35
|
|
|
warn_on_heading_level = False |
36
|
|
|
for line in body.splitlines(True): |
37
|
|
View Code Duplication |
if re.match(r"^#+ ", line): |
|
|
|
|
38
|
|
|
"""ATX style headings""" |
39
|
|
|
newbody = newbody + previous_line + heading + line |
40
|
|
|
if re.match(r"^#######+ ", heading + line): |
41
|
|
|
warn_on_heading_level = True |
42
|
|
|
line = "" |
43
|
|
|
elif re.match(r"^=+$", line.rstrip()) and not re.match( |
44
|
|
|
r"^$", previous_line.strip() |
45
|
|
|
): |
46
|
|
|
"""Setext style H1""" |
47
|
|
|
newbody = newbody + heading + "# " + previous_line |
48
|
|
|
line = "" |
49
|
|
|
elif re.match(r"^-+$", line.rstrip()) and not re.match( |
50
|
|
|
r"^$", previous_line.strip() |
51
|
|
|
): |
52
|
|
|
"""Setext style H2""" |
53
|
|
|
newbody = newbody + heading + "## " + previous_line |
54
|
|
|
line = "" |
55
|
|
|
else: |
56
|
|
|
newbody = newbody + previous_line |
57
|
|
|
previous_line = line |
58
|
|
|
newbody = newbody + previous_line # add very last line |
59
|
|
|
|
60
|
|
|
# make sure the export ends with a blank line |
61
|
|
|
if previous_line not in ["\r", "\n", "\r\n", "\n\r"]: |
62
|
|
|
newbody = newbody + os.linesep |
63
|
|
|
|
64
|
|
|
if warn_on_heading_level is True: |
65
|
|
|
print( |
66
|
|
|
f"{WARNING_COLOR}WARNING{RESET_COLOR}: " |
67
|
|
|
f"Headings increased past H6 on export - {date_str} {entry.title}", |
68
|
|
|
file=sys.stderr, |
69
|
|
|
) |
70
|
|
|
|
71
|
|
|
return f"{heading} {date_str} {entry.title}\n{newbody} " |
72
|
|
|
|
73
|
|
|
@classmethod |
74
|
|
|
def export_journal(cls, journal): |
75
|
|
|
"""Returns a Markdown representation of an entire journal.""" |
76
|
|
|
out = [] |
77
|
|
|
year, month = -1, -1 |
78
|
|
|
for e in journal.entries: |
79
|
|
|
if not e.date.year == year: |
80
|
|
|
year = e.date.year |
81
|
|
|
out.append("# " + str(year)) |
82
|
|
|
out.append("") |
83
|
|
|
if not e.date.month == month: |
84
|
|
|
month = e.date.month |
85
|
|
|
out.append("## " + e.date.strftime("%B")) |
86
|
|
|
out.append("") |
87
|
|
|
out.append(cls.export_entry(e, False)) |
88
|
|
|
result = "\n".join(out) |
89
|
|
|
return result |
90
|
|
|
|