掲示板<33>2026/1/31
from=TOZ

Pythonを利用⑨「spot_fixer_v30.py」

TOZ山荘のページをリニューアルしました。 最終設定に利用したプログラムです。500枚近いHTMLファイルを一瞬に して変更してくれるものです。 ホームページのタグの関係で、プログラム中の「<」「>」は全角表示しています。
import os
import re
import html

def spot_fixer_v30():
    output_dir = 'fixed_final_v30'
    if not os.path.exists(output_dir): os.makedirs(output_dir)

    print(f"--- コピーライト復刻・3ボタン並列版(v30)を開始します ---")

    for i in range(1, 488):
        filename = f"{i}.html"
        if not os.path.exists(filename): continue
        
        content = ""
        for enc in ['utf-8', 'cp932', 'euc-jp']:
            try:
                with open(filename, 'r', encoding=enc) as f:
                    content = f.read()
                break
            except: continue
        if not content: continue

        # 1. リンク先の特定
        if i <= 92: target_phase = "phase1.html"
        elif i <= 214: target_phase = "phase2.html"
        elif i <= 305: target_phase = "phase3.html"
        elif i <= 384: target_phase = "phase4.html"
        else: target_phase = "phase5.html"

        # 2. デザイン(CSS):3ボタン + 薄いコピーライト
        classic_style_final = """
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: sans-serif; line-height: 1.4; max-width: 900px; margin: 0 auto; padding: 0; background-color: #faf9f6; }
        
        header { background: #556b2f; color: #fff; text-align: center; height: 25px; display: flex; align-items: center; justify-content: center; }
        header h1 { font-size: 0.9em; letter-spacing: 2px; }
        .phase-nav { background: #eee; border: 1px solid #ddd; padding: 1px 0; }
        .phase-nav ul { list-style: none; display: flex; justify-content: center; gap: 1px; }
        .phase-nav li a { text-decoration: none; color: #333; font-weight: bold; padding: 1px 5px; background-color: #fff; border: 1px solid #ccc; font-size: 0.7em; display: block; }

        img, video { max-width: 100% !important; height: auto !important; display: block !important; margin: 0 auto !important; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
        .photo-grid { display: block !important; text-align: center !important; padding: 20px 0; background: #f0f0f0; border-top: 2px solid #556b2f; }
        .photo-card { display: inline-block !important; vertical-align: top; width: 95%; max-width: 500px; margin: 10px; background: #fff; border-bottom: 3px solid #556b2f; text-align: left; box-sizing: border-box; }
        .photo-frame { padding: 20px; background: #eee; text-align: center; }
        .cap-area { padding: 15px; background: #fff; text-align: center; font-weight: bold; font-size: 0.95rem; border-top: 1px solid #eee; }

        .article-nav { display: flex; justify-content: center; align-items: center; gap: 8px; padding: 25px 10px 10px 10px; border-top: 1px solid #ddd; margin-top: 20px; }
        .nav-btn { display: inline-block; color: #8b4513 !important; text-decoration: none; font-size: 0.75em; border: 1px solid #8b4513; padding: 6px 10px; background: #fff; white-space: nowrap; min-width: 85px; text-align: center; transition: all 0.3s; }
        .nav-btn:hover { background: #8b4513; color: #fff !important; }
        .nav-placeholder { display: inline-block; min-width: 85px; }

        /* 復刻:薄く表示するコピーライト */
        footer { text-align: center; padding: 15px 0 25px 0; font-size: 0.65em; color: #ccc; letter-spacing: 1px; }

        @media screen and (min-width: 768px) {
            .photo-card { width: 45%; }
            .nav-btn { font-size: 0.85em; padding: 8px 20px; min-width: 120px; }
            .article-nav { gap: 20px; }
        }
        """

        # 3. お掃除:古いタグとスタイルを完全に削除
        content = re.sub(r'<style>.*?</style>', f'<style>{classic_style_final}</style>', content, flags=re.DOTALL)
        content = re.sub(r'<nav class="article-nav">.*?</nav>', '', content, flags=re.DOTALL)
        content = re.sub(r'<div class="center-link">.*?</div>', '', content, flags=re.DOTALL)
        content = re.sub(r'<div class="nav-footer">.*?</div>', '', content, flags=re.DOTALL)
        content = re.sub(r'<footer>.*?</footer>', '', content, flags=re.DOTALL)

        # 4. 新しい3並列ボタン + コピーライトの挿入
        p_btn = f'<a href="{i-1}.html" class="nav-btn">← 前の回</a>' if i > 1 else '<div class="nav-placeholder"></div>'
        m_btn = f'<a href="{target_phase}" class="nav-btn">目次へ戻る</a>'
        n_btn = f'<a href="{i+1}.html" class="nav-btn">次の回 →</a>' if i < 487 else '<div class="nav-placeholder"></div>'
        
        final_footer_html = f"""
    <nav class="article-nav">{p_btn}{m_btn}{n_btn}</nav>
    <footer>© TOZ山荘 25年の記録</footer>"""

        # </body>の直前に挿入
        content = content.replace('</body>', f'{final_footer_html}\n</body>')

        with open(os.path.join(output_dir, filename), 'w', encoding='utf-8') as f:
            f.write(content)

    print(f"完了! {output_dir} フォルダ内のコピーライト表示を確認してください。")

if __name__ == "__main__":
    spot_fixer_v30()


← 一覧へ戻る