import os
import re

CSS = '''
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: #fafafa; color: #37352f; line-height: 1.6; max-width: 900px; margin: 0 auto; padding: 40px 20px; }
.header { background: white; border: 1px solid #e8e8e8; border-radius: 8px; padding: 30px; margin-bottom: 30px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
.header h1 { font-size: 28px; font-weight: 600; margin-bottom: 8px; }
.header p { color: #787774; font-size: 14px; }
.article-list { display: flex; flex-direction: column; gap: 1px; background: #e8e8e8; border: 1px solid #e8e8e8; border-radius: 8px; overflow: hidden; }
.article-item { background: white; padding: 16px 20px; transition: background 0.1s; }
.article-item:hover { background: #f7f6f3; }
.article-item h2 { font-size: 15px; font-weight: 500; margin-bottom: 4px; }
.article-item h2 a { color: #37352f; text-decoration: none; }
.article-item h2 a:hover { color: #1a1a1a; }
.article-item .meta { font-size: 12px; color: #787774; margin-bottom: 4px; }
.article-item .excerpt { font-size: 13px; color: #787774; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.back { display: inline-block; margin-bottom: 20px; color: #2eaadc; text-decoration: none; font-size: 14px; }
.back:hover { text-decoration: underline; }
.article { background: white; border: 1px solid #e8e8e8; border-radius: 8px; padding: 40px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
.article h1 { font-size: 26px; font-weight: 600; margin-bottom: 10px; }
.article .meta { font-size: 13px; color: #787774; margin-bottom: 20px; padding-bottom: 20px; border-bottom: 1px solid #e8e8e8; }
.article .meta a { color: #2eaadc; text-decoration: none; }
.article .content { font-size: 15px; }
.article .content h1,.article .content h2,.article .content h3 { margin: 20px 0 10px 0; font-weight: 600; }
.article .content h1 { font-size: 22px; }
.article .content h2 { font-size: 18px; }
.article .content h3 { font-size: 16px; }
.article .content p { margin-bottom: 12px; }
.article .content ul,.article .content ol { margin: 10px 0 10px 20px; }
.article .content li { margin-bottom: 4px; }
.article .content pre { background: #f7f6f3; border: 1px solid #e8e8e8; border-radius: 4px; padding: 15px; overflow-x: auto; margin: 15px 0; font-size: 13px; }
.article .content code { background: #f7f6f3; padding: 2px 5px; border-radius: 3px; font-size: 13px; }
.article .content pre code { background: none; padding: 0; }
.article .content img { max-width: 100%; border-radius: 4px; margin: 15px 0; }
.article .content a { color: #2eaadc; text-decoration: none; }
.article .content a:hover { text-decoration: underline; }
.article .content blockquote { border-left: 3px solid #e8e8e8; padding-left: 15px; color: #787774; margin: 15px 0; }
.article .content hr { border: none; border-top: 1px solid #e8e8e8; margin: 25px 0; }
.article .content table { width: 100%%; border-collapse: collapse; margin: 15px 0; }
.article .content th,.article .content td { border: 1px solid #e8e8e8; padding: 8px 12px; text-align: left; font-size: 14px; }
.article .content th { background: #f7f6f3; font-weight: 600; }
'''

def get_index_html():
    with open('/var/www/blog/index.html', 'r') as f:
        return f.read()

def update_index(html):
    # Replace head with new CSS
    html = re.sub(r'<style>.*?</style>', f'<style>{CSS}</style>', html, flags=re.DOTALL)
    # Remove old header class styling, keep structure
    return html

def update_article(html):
    html = re.sub(r'<style>.*?</style>', f'<style>{CSS}</style>', html, flags=re.DOTALL)
    return html

def main():
    # Update index
    idx = get_index_html()
    idx = update_index(idx)
    with open('/var/www/blog/index.html', 'w') as f:
        f.write(idx)
    print('Updated index.html')
    
    # Update all articles
    count = 0
    for fname in os.listdir('/var/www/blog'):
        if fname.endswith('.html') and fname != 'index.html':
            with open(f'/var/www/blog/{fname}', 'r') as f:
                html = f.read()
            html = update_article(html)
            with open(f'/var/www/blog/{fname}', 'w') as f:
                f.write(html)
            count += 1
    print(f'Updated {count} articles')

main()
