-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.py
77 lines (57 loc) · 1.76 KB
/
reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import frontmatter
import datetime
def is_headline(line):
return line[:3] == "###"
def is_empty_line(line):
return line == "\n" or ""
class MetaReader:
def __init__(self, file):
self.yaml = frontmatter.loads(file.read())
def _title_author(self):
entry = self.yaml["title"]
title, author = entry.split(" - ")
return title.strip(), author.strip()
@property
def author(self):
_, author = self._title_author()
return author
@property
def title(self):
title, _ = self._title_author()
return title
@property
def date(self):
return str(self.yaml["date"])
@property
def date_earlier(self):
return shift_date(self.date, -1)
def shift_date(date_time_str, nbr_days):
dt = datetime.datetime.strptime(date_time_str, "%Y-%m-%d")
res = dt + datetime.timedelta(days=-1)
return res.strftime("%Y-%m-%d")
class NoteReader:
note_section = "## 📒 Summary + Notes\n"
def __init__(self, file):
self.file = file
self._intro_skipped = False
def _is_intro_skipped(self, line):
if not self._intro_skipped:
self._intro_skipped = line == self.note_section
return self._intro_skipped
def get_all_notes(self):
notes = []
for line in self.file:
if self._is_note(line):
notes.append(line.rstrip())
return notes
def get_note(self):
for line in self.file:
if self._is_note(line):
return line.rstrip()
def _is_note(self, line):
return (
self._is_intro_skipped(line)
and line != self.note_section
and not is_headline(line)
and not is_empty_line(line)
)