-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_blob_references.py
More file actions
177 lines (145 loc) · 5.98 KB
/
fix_blob_references.py
File metadata and controls
177 lines (145 loc) · 5.98 KB
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
#!/usr/bin/env python3
"""
Script to fix blob references in markdown files by:
1. Moving blob files to a 'files' folder
2. Replacing blob references with proper image links
"""
import os
import re
import shutil
from pathlib import Path
def resolve_cards_dir() -> Path:
"""Resolve the default cards directory (env override, then fallback locations)."""
raw = (
os.environ.get("FLASHCARDS_DIR")
or os.environ.get("NEXT_PUBLIC_FLASHCARDS_DIR")
or os.environ.get("CARDS_DIR")
or os.environ.get("MARKDOWN_CARDS_DIR")
)
if raw:
p = Path(raw)
return p if p.is_absolute() else (Path.cwd() / p)
in_site = Path.cwd() / "markdown_cards"
if in_site.exists():
return in_site
in_parent = Path.cwd().parent / "markdown_cards"
if in_parent.exists():
return in_parent
current = Path.cwd()
for parent in [current, *current.parents]:
obsidian_root = parent / "obsidian_notes"
if obsidian_root.exists():
return obsidian_root / "flashcards"
return Path.cwd() / "obsidian_notes" / "flashcards"
def resolve_files_dir(cards_dir: Path) -> Path:
"""Resolve the media files directory under the cards directory."""
return cards_dir / "files"
def move_blobs_to_files_folder():
"""Move all blob files from blobs/ to files/ folder."""
cards_dir = resolve_cards_dir()
blobs_dir = Path("blobs")
if not blobs_dir.exists():
blobs_dir = cards_dir / "blobs"
files_dir = resolve_files_dir(resolve_cards_dir())
if not blobs_dir.exists():
print("No blobs directory found!")
return
# Create files directory if it doesn't exist
files_dir.mkdir(exist_ok=True)
# Move all blob files
blob_files = list(blobs_dir.glob("*"))
moved_count = 0
for blob_file in blob_files:
if blob_file.is_file():
# Determine file extension based on content
file_extension = get_file_extension(blob_file)
new_filename = f"{blob_file.name}{file_extension}"
new_path = files_dir / new_filename
# Move the file
shutil.move(str(blob_file), str(new_path))
moved_count += 1
print(f"Moved: {blob_file.name} -> files/{new_filename}")
print(f"Moved {moved_count} blob files to files/ directory")
# Remove empty blobs directory
if blobs_dir.exists() and not any(blobs_dir.iterdir()):
blobs_dir.rmdir()
print("Removed empty blobs/ directory")
def get_file_extension(file_path):
"""Determine the correct file extension based on file content."""
try:
import subprocess
result = subprocess.run(['file', str(file_path)], capture_output=True, text=True)
file_type = result.stdout.lower()
if 'png' in file_type:
return '.png'
elif 'jpeg' in file_type or 'jpg' in file_type:
return '.jpg'
elif 'gif' in file_type:
return '.gif'
elif 'svg' in file_type:
return '.svg'
elif 'pdf' in file_type:
return '.pdf'
else:
# Default to .bin for unknown types
return '.bin'
except:
# Fallback to .bin if file command fails
return '.bin'
def replace_blob_references_in_markdown():
"""Replace blob references in markdown files with proper image links."""
markdown_dir = resolve_cards_dir()
files_dir = resolve_files_dir(markdown_dir)
if not markdown_dir.exists():
print("No cards directory found!")
return
# Pattern to match blob references: {{blob <hash>}}
blob_pattern = r'\{\{blob\s+([a-f0-9]+)\}\}'
markdown_files = list(markdown_dir.glob("*.md"))
updated_count = 0
for md_file in markdown_files:
if md_file.name == "README.md":
continue # Skip README file
with open(md_file, 'r', encoding='utf-8') as f:
content = f.read()
original_content = content
# Find all blob references
blob_matches = re.findall(blob_pattern, content)
for blob_hash in blob_matches:
# Check if the blob file exists in files directory
blob_files = list(files_dir.glob(f"{blob_hash}.*"))
if blob_files:
# Use the first matching file
blob_file = blob_files[0]
file_extension = blob_file.suffix
# Determine if it's an image based on extension
if file_extension.lower() in ['.png', '.jpg', '.jpeg', '.gif', '.svg']:
# Replace with markdown image link
replacement = f""
else:
# Replace with file link
replacement = f"[File](files/{blob_file.name})"
# Replace the blob reference
content = content.replace(f"{{{{blob {blob_hash}}}}}", replacement)
else:
# If blob file not found, replace with a note
content = content.replace(f"{{{{blob {blob_hash}}}}}", f"[Missing blob: {blob_hash}]")
# Write back if content changed
if content != original_content:
with open(md_file, 'w', encoding='utf-8') as f:
f.write(content)
updated_count += 1
print(f"Updated: {md_file.name}")
print(f"Updated {updated_count} markdown files")
def main():
"""Main function to fix blob references."""
print("Starting blob reference fix...")
# Step 1: Move blob files to files folder
print("\n1. Moving blob files to files/ directory...")
move_blobs_to_files_folder()
# Step 2: Replace blob references in markdown files
print("\n2. Replacing blob references in markdown files...")
replace_blob_references_in_markdown()
print("\nBlob reference fix complete!")
if __name__ == "__main__":
main()