|
#!/usr/bin/env python3 |
|
|
|
import os |
|
import sys |
|
import re |
|
|
|
def extract_frontmatter(file_path): |
|
"""Extract frontmatter from markdown files manually without using yaml.safe_load.""" |
|
try: |
|
with open(file_path, 'r', encoding='utf-8') as file: |
|
content = file.read() |
|
|
|
# Look for frontmatter between --- delimiters |
|
frontmatter_match = re.search(r'^---\s*\n(.*?)\n---', content, re.DOTALL) |
|
|
|
if frontmatter_match: |
|
frontmatter_text = frontmatter_match.group(1) |
|
|
|
# Manually extract tags using regex instead of yaml parsing |
|
tags_match = re.search(r'tags:\s*\n((?:- .*\n)*)', frontmatter_text) |
|
if tags_match: |
|
# Extract individual tags from list format |
|
tags_list = re.findall(r'- (.*?)\s*$', tags_match.group(1), re.MULTILINE) |
|
return {'tags': tags_list} |
|
|
|
# Check for single tag format (tags: value) |
|
single_tag_match = re.search(r'tags:\s*(.*?)\s*$', frontmatter_text, re.MULTILINE) |
|
if single_tag_match: |
|
return {'tags': [single_tag_match.group(1)]} |
|
|
|
return None |
|
return None |
|
except (IOError, UnicodeDecodeError): |
|
# If file can't be read or isn't text, return None |
|
return None |
|
|
|
def search_files_with_tag(start_path, target_tag): |
|
"""Recursively search for files containing the specified tag.""" |
|
matching_files = [] |
|
|
|
# Validate the start path exists |
|
if not os.path.exists(start_path): |
|
print(f"Error: Path '{start_path}' does not exist.") |
|
return matching_files |
|
|
|
# Walk through all directories and files starting from start_path |
|
for root, _, files in os.walk(start_path): |
|
for filename in files: |
|
file_path = os.path.join(root, filename) |
|
|
|
# Skip non-regular files (symlinks, etc.) |
|
if not os.path.isfile(file_path): |
|
continue |
|
|
|
# Extract frontmatter from the file |
|
metadata = extract_frontmatter(file_path) |
|
|
|
# Check if metadata contains tags |
|
if metadata and 'tags' in metadata: |
|
tags = metadata['tags'] |
|
|
|
# Check if the target tag is in the list of tags |
|
if target_tag in tags: |
|
matching_files.append(file_path) |
|
|
|
return matching_files |
|
|
|
def main(): |
|
# Check if the correct number of arguments is provided |
|
if len(sys.argv) != 3: |
|
print("Usage: python script.py <directory_path> <tag>") |
|
sys.exit(1) |
|
|
|
# Get the directory path and tag from command line arguments |
|
directory_path = sys.argv[1] |
|
tag = sys.argv[2] |
|
|
|
# Search for files with the specified tag |
|
matching_files = search_files_with_tag(directory_path, tag) |
|
|
|
# Print the results |
|
if matching_files: |
|
for file_path in matching_files: |
|
if not file_path.endswith('~'): |
|
print(f"{file_path}") |
|
else: |
|
print(f"No files found with the tag '{tag}'.") |
|
|
|
if __name__ == "__main__": |
|
main() |