-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbear_note.py
64 lines (52 loc) · 1.72 KB
/
bear_note.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
import re
import bear_api
class BearNote:
def __init__(self, sql_row):
self.content = sql_row
def append_text(self, text):
return bear_api.append_text_to_note(self, text)
@property
def outgoing_links(self):
yield from bear_api.note_link(self.text)
@property
def sections_outgoing_links(self):
for title, content in self.sections:
for link in bear_api.note_link(content):
yield title, link
@property
def sections(self):
section_content = ""
previous_section_title = ""
for text_line in self.text.splitlines():
title = match_section_title(text_line)
if title:
if previous_section_title:
yield previous_section_title, section_content
previous_section_title = title
section_content = ""
else:
section_content += f"\n{text_line}"
yield previous_section_title, section_content
@property
def uid(self):
return self.content['ZUNIQUEIDENTIFIER']
@property
def title(self):
return self.content['ZTITLE']
@property
def text(self):
return self.content['ZTEXT'].rstrip()
# Unused for now
@property
def tags(self):
pattern1 = r'(?<!\S)\#([.\w\/\-]+)[ \n]?(?!([\/ \w]+\w[#]))'
pattern2 = r'(?<![\S])\#([^ \d][.\w\/ ]+?)\#([ \n]|$)'
for pattern in (pattern1, pattern2):
for matches in re.findall(pattern, self.text):
yield matches[0]
def match_section_title(text_line):
match = re.match(r'#+\s+([^\n]*)', text_line)
try:
return match[1]
except (TypeError, IndexError):
return None