88
99VAULTS_FILE = 'obsidian.json'
1010VAULTS_PATH = Path (os .getenv ('APPDATA' ), 'obsidian' , VAULTS_FILE )
11+ CHECK_BOX = '- [ ]'
12+ MARKED_CHECK_BOX = '- [x]'
1113
1214def get_vaults ():
1315 vaults = []
@@ -22,6 +24,25 @@ def get_vaults():
2224 vaults .append (Vault (vault , data ['vaults' ][vault ]))
2325 return vaults
2426
27+ def get_vault (id ):
28+ try :
29+ with open (VAULTS_PATH , 'r' ) as f :
30+ data = json .load (f )
31+ except FileNotFoundError :
32+ logger .error (f'{ VAULTS_PATH } not found!\n Is obsidian installed?' )
33+ raise
34+ else :
35+ try :
36+ return Vault (id , data ['vaults' ][id ])
37+ except KeyError :
38+ logger .error (f'{ id } not found!' )
39+ raise
40+
41+ def get_note (vault_id , note_path ):
42+ vault = get_vault (vault_id )
43+ return Note (vault , note_path )
44+
45+
2546def open_note (vault_name , note_path ):
2647 URI = f'open?vault={ vault_name } &file={ note_path } ' .replace (' ' , '%20' ).replace ('/' , '%2F' ).replace ('\\ ' , '%2F' )
2748 URI = f'obsidian://{ URI } '
@@ -42,6 +63,11 @@ def notes(self):
4263 notes .append (Note (self , note ))
4364 return notes
4465
66+ def note (self , note_path ):
67+ for note in self .notes ():
68+ if str (note .relative_path ) == note_path :
69+ return note
70+
4571
4672class Note (object ):
4773
@@ -55,6 +81,52 @@ def __init__(self, vault: Vault, full_path: str):
5581 def open_note (self ):
5682 open_note (self .vault .name , self .relative_path )
5783
84+ def content (self ):
85+ with open (self .path , 'r' ) as f :
86+ return f .read ()
87+
88+
89+ def toggle_checkbox (self , raw ):
90+ content = self .content ()
91+ for line in content .splitlines ():
92+ if raw == line :
93+ if MARKED_CHECK_BOX in line :
94+ toggled_line = line .replace (MARKED_CHECK_BOX , CHECK_BOX )
95+ else :
96+ toggled_line = line .replace (CHECK_BOX , MARKED_CHECK_BOX )
97+ break
98+ content = content .replace (line , toggled_line )
99+ with open (self .path , 'w' ) as f :
100+ f .write (content )
101+
102+ def checklists (self ):
103+ checklists = []
104+ title = ''
105+ for line in self .content ().splitlines ():
106+ if CHECK_BOX in line or MARKED_CHECK_BOX in line :
107+ description = line .replace (CHECK_BOX , '' ).replace (MARKED_CHECK_BOX , '' ).strip ()
108+ if MARKED_CHECK_BOX in line :
109+ checked = True
110+ else :
111+ checked = False
112+ if (CHECK_BOX not in prev_line and MARKED_CHECK_BOX not in prev_line ) and prev_line .endswith (':' ):
113+ title = prev_line .replace (':' , '' ).strip ()
114+ checklists .append (
115+ {
116+ 'title' : title ,
117+ 'description' : description ,
118+ 'checked' : checked ,
119+ 'raw' : line
120+ }
121+ )
122+ else :
123+ title = ''
124+ prev_line = line
125+ return checklists
126+
127+
128+
129+
58130if __name__ == "__main__" :
59131 vaults = get_vaults ()
60132 for vault in vaults :
0 commit comments