toolkit / fixcringe.py
k4d3's picture
add silly code
6f57a6d
raw
history blame
1.88 kB
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()