-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
269 lines (214 loc) · 8.75 KB
/
utils.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
utils.py - Shared utility functions for Obsidian enhancement tools
This module provides common utility functions used across the various
Obsidian enhancement tools, including link extraction, tag management,
and content manipulation functions.
Author: Jonathan Care <jonc@lacunae.org>
"""
import os
import re
import hashlib
def extract_existing_links(content):
"""
Extract all existing wiki links from a note.
Args:
content (str or dict): The note content to extract links from.
Can be a string or a dictionary with a 'content' key.
Returns:
list: List of link targets (without brackets)
"""
links = []
# Handle both string content and dictionary with 'content' key
if isinstance(content, dict) and 'content' in content:
content = content['content']
# Ensure content is a string
if not isinstance(content, str):
return links
# Find all wiki links in the content
for match in re.finditer(r'\[\[(.*?)(?:\|.*?)?\]\]', content):
link = match.group(1).strip()
links.append(link)
return links
def extract_existing_tags(content):
"""
Extract all existing tags from a note (both from #tags section and inline).
Args:
content (str or dict): The note content to extract tags from.
Can be a string or a dictionary with a 'content' key.
Returns:
list: List of tags (with # prefix)
"""
existing_tags = []
# Handle both string content and dictionary with 'content' key
if isinstance(content, dict) and 'content' in content:
content = content['content']
# Ensure content is a string
if not isinstance(content, str):
return existing_tags
# Extract tags from #tags section if it exists
tags_section_match = re.search(r'#tags:\s*(.*?)(\n\n|\n$|$)', content, re.IGNORECASE | re.DOTALL)
if tags_section_match:
tags_text = tags_section_match.group(1).strip()
# Extract tags from the tags section
tags_from_section = [tag.strip() for tag in re.findall(r'#\w+', tags_text)]
existing_tags.extend(tags_from_section)
# Find other inline tags in the document
inline_tags = [f"#{tag}" for tag in re.findall(r'#([a-zA-Z0-9_]+)', content)]
# Combine all tags and remove duplicates while preserving order
all_tags = []
for tag in existing_tags + inline_tags:
tag_lower = tag.lower() # Case-insensitive comparison
if not any(t.lower() == tag_lower for t in all_tags):
all_tags.append(tag)
return all_tags
def extract_section(content, section_header):
"""
Extract a section from note content.
Args:
content (str or dict): The note content.
Can be a string or a dictionary with a 'content' key.
section_header (str): The section header to extract (e.g., "## Related Notes")
Returns:
tuple: (section_text, full_match) or (None, None) if section not found
"""
# Handle both string content and dictionary with 'content' key
if isinstance(content, dict) and 'content' in content:
content = content['content']
# Ensure content is a string
if not isinstance(content, str):
return None, None
# Escape any regex special characters in the section header
escaped_header = re.escape(section_header)
# Match the section and its content until the next section or end of file
pattern = f"{escaped_header}\\n(.*?)(?=\\n## |\\n#|\\Z)"
match = re.search(pattern, content, re.DOTALL)
if match:
return match.group(1), match.group(0)
return None, None
def replace_section(content, section_header, new_section_content):
"""
Replace a section in note content or add it if it doesn't exist.
Args:
content (str or dict): The note content.
Can be a string or a dictionary with a 'content' key.
section_header (str): The section header (e.g., "## Related Notes")
new_section_content (str): The new content for the section
Returns:
str: Updated content
"""
# Handle both string content and dictionary with 'content' key
if isinstance(content, dict) and 'content' in content:
content = content['content']
# Ensure content is a string
if not isinstance(content, str):
return content
# Check if section exists
escaped_header = re.escape(section_header)
if re.search(f"{escaped_header}\\n", content):
# Replace existing section
pattern = f"{escaped_header}.*?(?=\\n## |\\n#|\\Z)"
new_section = f"{section_header}\n{new_section_content}\n\n"
updated_content = re.sub(pattern, new_section, content, flags=re.DOTALL)
return updated_content
else:
# Add new section at the end
if content.strip() and not content.endswith("\n\n"):
if content.endswith("\n"):
section_text = f"\n{section_header}\n{new_section_content}\n"
else:
section_text = f"\n\n{section_header}\n{new_section_content}\n"
else:
section_text = f"{section_header}\n{new_section_content}\n"
return content + section_text
def merge_links(existing_links, new_links, format_func=None):
"""
Merge existing and new links, avoiding duplicates.
Args:
existing_links (list): List of existing link entries (full lines with formatting)
new_links (list): List of new links to add
format_func (callable, optional): Function to format new link entries
Returns:
list: Merged list of link entries with duplicates removed
"""
# Extract note names from existing entries
note_names_added = set()
merged_links = []
# Process existing entries first
for entry in existing_links:
if not entry.strip():
continue
link_match = re.search(r'\[\[(.*?)\]\]', entry)
if link_match:
note_name = link_match.group(1)
if note_name.lower() not in (name.lower() for name in note_names_added):
merged_links.append(entry)
note_names_added.add(note_name)
# Process new links
for link in new_links:
if isinstance(link, str) and "[[" in link:
# Link is already formatted
link_match = re.search(r'\[\[(.*?)\]\]', link)
if link_match:
note_name = link_match.group(1)
else:
# Link is just the note name
note_name = link
# Skip if already added
if note_name.lower() in (name.lower() for name in note_names_added):
continue
# Format the link if needed
if format_func and not (isinstance(link, str) and link.startswith("-")):
entry = format_func(link)
elif isinstance(link, str) and link.startswith("- [["):
entry = link
else:
entry = f"- [[{link}]]"
merged_links.append(entry)
note_names_added.add(note_name)
return merged_links
def deduplicate_tags(tags):
"""
Deduplicate a list of tags case-insensitively.
Args:
tags (list): List of tags (with # prefix)
Returns:
list: Deduplicated list of tags
"""
unique_tags = []
added_tags_lower = set()
for tag in tags:
# Ensure it has # prefix
if not tag.startswith('#'):
tag = f"#{tag}"
tag_lower = tag.lower()
if tag_lower not in added_tags_lower:
unique_tags.append(tag)
added_tags_lower.add(tag_lower)
return unique_tags
def generate_note_hash(note_content):
"""
Generate a hash for note content to track changes.
Args:
note_content (str or dict): The note content to hash.
Can be a string or a dictionary with a 'content' key.
Returns:
str: MD5 hash of the content
"""
# Handle both string content and dictionary with 'content' key
if isinstance(note_content, dict) and 'content' in note_content:
note_content = note_content['content']
# Ensure content is a string
if not isinstance(note_content, str):
note_content = str(note_content)
return hashlib.md5(note_content.encode('utf-8')).hexdigest()
def get_note_filename(path):
"""
Extract the note name without extension from a file path.
Args:
path (str): The path to the note file
Returns:
str: The note name without extension
"""
return os.path.splitext(os.path.basename(path))[0]