defgenerate_index(path): ''' :param path: parent dir of the new public (dist) and old archive ''' with open(path / 'public' / 'index.html', encoding='utf-8') as fp: new = fp.read() with open(path / 'old_archive' / 'index.html', encoding='utf-8') as fp: old = fp.read() stp = [] for m in re.finditer('article>\s+?</div', new, re.DOTALL): stp.append(m.start() + len('article>') + 10) output = new[:stp[0]] + '\n'.join(re.findall(r'<article.+?/article>', old, re.DOTALL)) + new[stp[0]:] with open(path / 'public' / 'index.html', 'w', encoding='utf-8') as fp: fp.write(output)
II. 将old_archive中的必要文件拷贝到新的dist文件夹(视具体情况修改),并将旧页面的css改回原来的css。
# 将旧页面的css替换回原来的css,解决页面显示错误的问题 for x in glob.glob(path / 'public' / '201*/*/*/*/index.html'): with open(x, encoding='utf-8') as fp: con = fp.read().replace('/css/main.css?v=5.0.1', '/css_old/main.css?v=5.0.1') with open(x, 'w', encoding='utf-8') as fp: fp.write(con)
for x in list(glob.glob(path / 'public/archives' / '201*/index.html')) + list(glob.glob(path / 'public/archives' / '201*/*/index.html')): print(x) with open(x, encoding='utf-8') as fp: con = fp.read().replace('/css/main.css?v=5.0.1', '/css_old/main.css?v=5.0.1') with open(x, 'w', encoding='utf-8') as fp: fp.write(con)
from path import Path from bs4 import BeautifulSoup path = Path('/PATH/TO/DIR')
with open(path / 'old_archive' / 'archives' / 'index.html', encoding='utf-8') as fp: old = BeautifulSoup(fp) # 修改旧文章的css with open(path / 'public/archives/index.html', encoding='utf-8') as fp: new = BeautifulSoup(fp)
art = new.find_all('div', {'class': 'posts-collapse'})[0] hd = old.find_all('div', {'class': 'collection-title'})[1] hd['class'] = 'collection-year' hd.h2['class'] = 'collection-header' hd.h2.name = 'span' hd.span.contents[0].replaceWith('更早之前...')
base = len(list(art.children)) art.insert(base, hd) base += 1
for i, x in enumerate(old.find_all('article', {'class': 'post post-type-normal'})): x.h1.name = 'div' x.header.contents = [x.header.contents[0], x.header.contents[3], x.header.contents[2], x.header.contents[1]] art.insert(base + i, x)
with open(path / 'public/archives/index.html', 'w', encoding='utf-8') as fp: fp.write(str(new))