File size: 1,876 Bytes
6f57a6d |
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 |
import os
import re
def fix_blurhash_block(content):
# Pattern to match the blurhash block
pattern = r'{{<\s*blurhash\s*\n((?:[^>}]|\n)*?)>}}'
def replace_block(match):
lines = match.group(1).strip().split('\n')
# Process each line to add proper indentation and clean up
processed_lines = []
for line in lines:
# Strip existing whitespace
clean_line = line.strip()
# Add 2 spaces indentation
processed_lines.append(f" {clean_line}")
# If this is the alt line, add grid="true" after it
if 'alt=' in clean_line:
grid_line = ' grid="true"'
processed_lines.append(grid_line)
# Reconstruct the block
return '{{< blurhash\n' + '\n'.join(processed_lines) + '\n>}}'
# Replace all occurrences in the content
return re.sub(pattern, replace_block, content, flags=re.MULTILINE)
def process_file(file_path):
try:
# Read the file content
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Fix the content
modified_content = fix_blurhash_block(content)
# Write back only if changes were made
if content != modified_content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(modified_content)
print(f"Processed: {file_path}")
except Exception as e:
print(f"Error processing {file_path}: {str(e)}")
def main():
# Walk through all directories recursively
for root, dirs, files in os.walk('.'):
for file in files:
if file.endswith('.cringe'):
file_path = os.path.join(root, file)
process_file(file_path)
if __name__ == '__main__':
main()
|