-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject_layout.py
More file actions
92 lines (79 loc) · 3.85 KB
/
inject_layout.py
File metadata and controls
92 lines (79 loc) · 3.85 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
import glob
from bs4 import BeautifulSoup
def process_file(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
html = f.read()
soup = BeautifulSoup(html, 'html.parser')
modified = False
# 1. Inject CSS
head = soup.find('head')
if head:
existing_link = head.find('link', href=lambda x: x and '/assets/css/layout-upgrade.css' in x)
if not existing_link:
new_link = soup.new_tag('link', rel='stylesheet', href='/assets/css/layout-upgrade.css')
head.append(new_link)
modified = True
# 2. Inject Sidebar for posts
if 'posts' in filepath.replace('\\', '/'):
article = soup.find('article')
if article and not soup.find('div', class_='article-layout'):
# Find Best Pick from TLDR
best_pick_name = "Top Recommendation"
best_pick_link = "#"
tldr = soup.find(class_=lambda x: x and ('tldr-verdict' in x or 'tldr-box' in x or 'verdict-box' in x))
if tldr:
cta = tldr.find('a', class_='btn-primary')
if cta:
best_pick_link = cta.get('href', '#')
# Try to parse name from "Check [Name] on Amazon"
text = cta.get_text(strip=True)
if "Check" in text and "on Amazon" in text:
best_pick_name = text.replace("Check", "").replace("on Amazon", "").strip()
if best_pick_name == "Top Recommendation" and article:
# fallback
first_cta = article.find('a', class_='btn-primary')
if first_cta:
best_pick_link = first_cta.get('href', '#')
text = first_cta.get_text(strip=True)
if "Check" in text and "on Amazon" in text:
best_pick_name = text.replace("Check", "").replace("on Amazon", "").strip()
# Create layout wrapper
layout_div = soup.new_tag('div', attrs={'class': 'article-layout'})
# Wrap article
article.wrap(layout_div)
# Create sidebar
sidebar_html = f'''
<aside class="sidebar">
<div class="sidebar-inner">
<div class="quick-verdict">
<p class="best-pick-label">🏆 Best Pick</p>
<p class="product-name">{best_pick_name}</p>
<a style="padding: 12px 16px; font-size: 14px; width: 100%; box-sizing: border-box;" href="{best_pick_link}" class="btn-primary" target="_blank" rel="nofollow noopener sponsored">Check Price</a>
</div>
<h3>Quick Navigation</h3>
<nav class="quick-nav">
<a href="#overview">Overview</a>
<a href="#comparison">Comparison Table</a>
<a href="#buying-guide">Buying Guide</a>
<a href="#verdict">Final Recommendation</a>
</nav>
</div>
</aside>
'''
sidebar_soup = BeautifulSoup(sidebar_html, 'html.parser')
# Append sidebar after article inside layout_div
layout_div.append(sidebar_soup)
modified = True
if modified:
with open(filepath, 'w', encoding='utf-8') as f:
# BeautifulSoup sometimes messes up DOCTYPE and formatting slightly,
# but usually it's fine for our simple DOM manipulation here.
# Using formatter="html" keeps self-closing tags intact.
f.write(soup.encode(formatter="html").decode('utf-8'))
print(f"Updated {filepath}")
# Process all html files
files = glob.glob('*.html') + glob.glob('posts/*.html')
for f in files:
process_file(f)
print("Done.")