import os
import re

# 1. Create publications.html based on template of about.html / news.html
template_page = 'news.html' if os.path.exists('news.html') else 'about.html'
with open(template_page, 'r', encoding='utf-8') as f:
    template = f.read()

# Generate publications.html content
pub_page = template

# Replace Title and SEO
pub_page = re.sub(r'<title>.*?</title>', '<title>Publications | Franciscan Friars of the Third Order Regular</title>', pub_page)
pub_page = re.sub(r'<link rel="canonical" href=".*?">', '<link rel="canonical" href="https://franciscansociety.org/publications.html">', pub_page)

# Update Hero section
hero_pattern = re.compile(r'<section style="padding: 12rem 2rem 8rem 2rem;.*?</section>', re.DOTALL)
new_hero = """    <!-- Page Hero -->
    <section style="padding: 11rem 2rem 7rem 2rem; background-image: url('assets/images/new_uploads/ChatGPT_Image_Aug_18_2026_05_51_30_PM.png'); background-size: cover; background-position: center; position: relative; overflow: hidden;">
        <div style="position: absolute; inset: 0; background-color: rgba(12, 11, 10, 0.72);"></div>
        <div style="max-width: 800px; margin: 0 auto; position: relative; z-index: 2; text-align: center;">
            <div style="display: inline-flex; align-items: center; gap: 0.6rem; background: rgba(255,255,255,0.1); backdrop-filter: blur(10px); padding: 0.5rem 1.2rem; border-radius: 50px; margin-bottom: 1.5rem; border: 1px solid rgba(255,255,255,0.2);">
                <span style="width: 8px; height: 8px; background-color: #e6c888; border-radius: 50%; display: inline-block;"></span>
                <span style="color: #ffffff; font-size: 0.85rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.08em; font-family: 'Instrument Sans', sans-serif;">SCHOLARSHIP &amp; ESSAYS</span>
            </div>
            <h1 style="font-family: 'Phudu', sans-serif; font-size: clamp(2.8rem, 5vw, 4.2rem); font-weight: 700; color: #ffffff; text-transform: uppercase; margin: 0;">PUBLICATIONS</h1>
        </div>
    </section>"""

if hero_pattern.search(pub_page):
    pub_page = hero_pattern.sub(new_hero, pub_page, count=1)

# Replace Main Content with the refined Articles & Publications list
main_content = """        <!-- Publications & Articles List (Inspired by torfranciscan.com/articles) -->
        <section style="padding: clamp(3.5rem, 6vw, 5.5rem) 0; background-color: #FFFFFF; color: #1c1917; min-height: 500px;">
            <div style="max-width: 1200px; margin: 0 auto; padding: 0 clamp(1rem, 4vw, 2.5rem);">
                
                <!-- Section Header -->
                <div style="text-align: center; margin-bottom: clamp(3rem, 6vw, 4.5rem);">
                    <h2 style="font-family: 'Phudu', sans-serif; font-size: clamp(2rem, 4vw, 2.8rem); font-weight: 700; color: #1c1917; text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 0.8rem;">
                        ARTICLES &amp; RESEARCH
                    </h2>
                    <p style="font-family: 'Instrument Sans', sans-serif; font-size: 1.05rem; color: #78716c; max-width: 600px; margin: 0 auto;">
                        Scholarly papers, theological insights, and academic publications by the Franciscan Friars.
                    </p>
                </div>

                <!-- Articles Container -->
                <div style="display: flex; flex-direction: column; gap: 0;">

                    <!-- Publication Item 1: Jnanadeepa PDF -->
                    <div style="display: grid; grid-template-columns: minmax(80px, 120px) minmax(140px, 200px) 1fr auto; gap: clamp(1.2rem, 3vw, 2.5rem); align-items: center; padding: clamp(2rem, 4vw, 3rem) 0; border-top: 1px solid #f0ece1;" class="publication-item">
                        
                        <!-- Date Block -->
                        <div style="display: flex; flex-direction: column; align-items: flex-start; justify-content: center; font-family: 'Phudu', sans-serif;">
                            <span style="font-size: clamp(2.4rem, 4vw, 3.4rem); font-weight: 300; line-height: 1; color: #4A2A18;">05</span>
                            <span style="font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.1em; color: #78716c; margin-top: 0.35rem; font-family: 'Instrument Sans', sans-serif;">MAY - AUG<br>2025</span>
                        </div>

                        <!-- Thumbnail Image -->
                        <div style="width: 100%; aspect-ratio: 4/3; border-radius: 12px; overflow: hidden; box-shadow: 0 8px 20px rgba(74,42,24,0.12); border: 1px solid rgba(230,200,136,0.3); background: #FAF7F0;">
                            <img loading="lazy" decoding="async" src="assets/images/gallery/WhatsApp Image 2026-08-07 at 10.18.29 AM.jpeg" alt="Jnanadeepa Journal of Religious Studies" style="width: 100%; height: 100%; object-fit: cover; transition: transform 0.4s ease;" onmouseover="this.style.transform='scale(1.05)'" onmouseout="this.style.transform='scale(1)'">
                        </div>

                        <!-- Content Block -->
                        <div>
                            <h3 style="font-family: 'Phudu', sans-serif; font-size: clamp(1.15rem, 2.2vw, 1.4rem); font-weight: 700; color: #1c1917; text-transform: uppercase; letter-spacing: 0.02em; margin: 0 0 0.65rem 0; line-height: 1.35;">
                                Jnanadeepa: Pune Journal of Religious Studies
                            </h3>
                            <p style="font-family: 'Instrument Sans', sans-serif; font-size: clamp(0.92rem, 1.6vw, 1rem); color: #57534e; line-height: 1.65; margin: 0 0 0.85rem 0; font-weight: 400;">
                                Pope Francis’ Teachings on Marriage and Family
                            </p>
                            <div style="display: flex; align-items: center; gap: 0.5rem; font-family: 'Instrument Sans', sans-serif; font-size: 0.84rem; color: #78716c; font-weight: 500;">
                                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>
                                <span>Religious Studies &amp; Theology • PDF Document</span>
                            </div>
                        </div>

                        <!-- Action Button -->
                        <div style="white-space: nowrap;">
                            <a href="assets/pdf/jnanadeepa-may-aug-2025.pdf" target="_blank" class="pub-btn" style="display: inline-flex; align-items: center; gap: 0.6rem; padding: 0.8rem 1.8rem; border: 1.5px solid #c4a45a; border-radius: 4px; background: transparent; color: #4A2A18; font-family: 'Instrument Sans', sans-serif; font-size: 0.85rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; text-decoration: none; transition: all 0.3s ease;" onmouseover="this.style.background='#4A2A18'; this.style.color='#ffffff'; this.style.borderColor='#4A2A18';" onmouseout="this.style.background='transparent'; this.style.color='#4A2A18'; this.style.borderColor='#c4a45a';">
                                VIEW PDF
                                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg>
                            </a>
                        </div>
                    </div>

                    <!-- Publication Item 2: PubMed Link -->
                    <div style="display: grid; grid-template-columns: minmax(80px, 120px) minmax(140px, 200px) 1fr auto; gap: clamp(1.2rem, 3vw, 2.5rem); align-items: center; padding: clamp(2rem, 4vw, 3rem) 0; border-top: 1px solid #f0ece1; border-bottom: 1px solid #f0ece1;" class="publication-item">
                        
                        <!-- Date Block -->
                        <div style="display: flex; flex-direction: column; align-items: flex-start; justify-content: center; font-family: 'Phudu', sans-serif;">
                            <span style="font-size: clamp(2.4rem, 4vw, 3.4rem); font-weight: 300; line-height: 1; color: #4A2A18;">11</span>
                            <span style="font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.1em; color: #78716c; margin-top: 0.35rem; font-family: 'Instrument Sans', sans-serif;">MAY<br>2017</span>
                        </div>

                        <!-- Thumbnail Image -->
                        <div style="width: 100%; aspect-ratio: 4/3; border-radius: 12px; overflow: hidden; box-shadow: 0 8px 20px rgba(74,42,24,0.12); border: 1px solid rgba(230,200,136,0.3); background: #FAF7F0;">
                            <img loading="lazy" decoding="async" src="assets/images/new_uploads/ChatGPT_Image_Aug_18_2026_05_24_08_PM.png" alt="Farmer suicide in India biotechnology research" style="width: 100%; height: 100%; object-fit: cover; transition: transform 0.4s ease;" onmouseover="this.style.transform='scale(1.05)'" onmouseout="this.style.transform='scale(1)'">
                        </div>

                        <!-- Content Block -->
                        <div>
                            <h3 style="font-family: 'Phudu', sans-serif; font-size: clamp(1.15rem, 2.2vw, 1.4rem); font-weight: 700; color: #1c1917; text-transform: uppercase; letter-spacing: 0.02em; margin: 0 0 0.65rem 0; line-height: 1.35;">
                                Farmer-suicide in India: debating the role of biotechnology
                            </h3>
                            <p style="font-family: 'Instrument Sans', sans-serif; font-size: clamp(0.92rem, 1.6vw, 1rem); color: #57534e; line-height: 1.65; margin: 0 0 0.85rem 0; font-weight: 400;">
                                Indian Biotech opponents have attributed the increase of suicides to the monopolization of GM seeds, centering on patent control, application of terminator technology, marketing strategy, and increased production costs.
                            </p>
                            <div style="display: flex; align-items: center; gap: 0.5rem; font-family: 'Instrument Sans', sans-serif; font-size: 0.84rem; color: #78716c; font-weight: 500;">
                                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></svg>
                                <span>National Library of Medicine (PubMed: 28497354) • Peer-Reviewed Paper</span>
                            </div>
                        </div>

                        <!-- Action Button -->
                        <div style="white-space: nowrap;">
                            <a href="https://pubmed.ncbi.nlm.nih.gov/28497354/" target="_blank" rel="noopener noreferrer" class="pub-btn" style="display: inline-flex; align-items: center; gap: 0.6rem; padding: 0.8rem 1.8rem; border: 1.5px solid #c4a45a; border-radius: 4px; background: transparent; color: #4A2A18; font-family: 'Instrument Sans', sans-serif; font-size: 0.85rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; text-decoration: none; transition: all 0.3s ease;" onmouseover="this.style.background='#4A2A18'; this.style.color='#ffffff'; this.style.borderColor='#4A2A18';" onmouseout="this.style.background='transparent'; this.style.color='#4A2A18'; this.style.borderColor='#c4a45a';">
                                VIEW ARTICLE
                                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg>
                            </a>
                        </div>
                    </div>

                </div>

            </div>
        </section>

        <!-- Responsive Media Query for Publication Items on Mobile -->
        <style>
            @media (max-width: 820px) {
                .publication-item {
                    grid-template-columns: 1fr !important;
                    gap: 1.2rem !important;
                    text-align: left !important;
                }
                .publication-item > div:first-child {
                    flex-direction: row !important;
                    align-items: baseline !important;
                    gap: 0.75rem !important;
                }
                .publication-item > div:first-child span:first-child {
                    font-size: 2.2rem !important;
                }
                .publication-item > div:nth-child(2) {
                    max-width: 260px !important;
                }
                .publication-item .pub-btn {
                    width: 100% !important;
                    justify-content: center !important;
                }
            }
        </style>"""

# Replace in main section
main_regex = re.compile(r'<main id="main-content"[^>]*>.*?</main>', re.DOTALL)
if main_regex.search(pub_page):
    pub_page = main_regex.sub(f'<main id="main-content" style="padding-top: 0; background-color: #FFFFFF;">\n{new_hero}\n{main_content}\n    </main>', pub_page)

with open('publications.html', 'w', encoding='utf-8') as f:
    f.write(pub_page)

print("Created publications.html successfully!")

# 2. Update navigation across ALL HTML files to include Publications
html_files = [f for f in os.listdir('.') if f.endswith('.html')]

for fname in html_files:
    with open(fname, 'r', encoding='utf-8') as f:
        c = f.read()

    # Desktop Nav: Insert <a href="publications.html">Publications</a> before <a href="news.html">News</a>
    if 'publications.html' not in c:
        # In desktop nav
        c = re.sub(
            r'(<a\s+href="news\.html">News</a>)',
            r'<a href="publications.html">Publications</a>\n      \1',
            c
        )
        # In mobile nav
        c = re.sub(
            r'(<a\s+href="news\.html">News</a>)',
            r'<a href="publications.html">Publications</a>\n  \1',
            c
        )
        # In footer quick links
        c = re.sub(
            r'(<li><a\s+href="gallery\.html"[^>]*>Gallery</a></li>)',
            r'\1\n                                <li><a href="publications.html" style="color: #a8a29e; text-decoration: none; transition: color 0.3s ease;" onmouseover="this.style.color=\'#e6c888\'" onmouseout="this.style.color=\'#a8a29e\'">Publications</a></li>',
            c
        )

    with open(fname, 'w', encoding='utf-8') as f:
        f.write(c)

print(f"Added Publications menu link to all {len(html_files)} HTML files!")
