defcreate_links(): links = {} postdir = os.path.join(curdir, 'source/_posts') for f in os.listdir(postdir): f = os.path.join(postdir, f) if os.path.isdir(f): for p in os.listdir(f): if (p.endswith('.md') or p.endswith('markdown') ) andnot os.path.isdir(os.path.join(f, p)) and re.match(r'^[\d]{4}-[\d]{2}-[\d]{2}.*', p): long_link = f'/{p[0:4]}/{p[5:7]}/{p[8:10]}/{p[11:-3] if p.endswith(".md") else p[11:-9]}/' short_link = shorten(p) if short_link in links: raise Exception(f'found short link {short_link} -> {long_link} conflict with existing {links[short_link]}') links[short_link] = long_link items = sorted(list(links.items()), key=lambda item: item[1]) for item in items: print(f'{item[1]}: https://brightliao.com/#/{item[0]}') print(f'found {len(links)} links.') return links
defupdate_bootjs(links): bootjs = os.path.join(curdir, 'themes/next/source/js/next-boot.js') ifnot os.path.exists(bootjs): raise Exception('bootjs file not found at: ' + bootjs) withopen(bootjs, 'r') as f: content = f.read() rex = r'\nvar links = {[^\n]*}\s*\n' ifnot re.search(rex, content): raise Exception('rex not found in bootjs') content = re.sub(rex, f'\nvar links = {json.dumps(links)}\n', content, count=1) withopen(bootjs, 'w') as f: f.write(content) print('updated file: ' + bootjs)
defupdate_indexjs(): indexjs = os.path.join(curdir, 'themes/next/layout/_scripts/index.swig') ifnot os.path.exists(indexjs): raise Exception('indexjs file not found at: ' + indexjs) withopen(indexjs, 'r') as f: content = f.read() rex = r"'next-boot\.js\?([\d]+)'" ifnot re.search(rex, content): raise Exception('rex not found in indexjs') nextver = int(re.match(r".*'next-boot\.js\?([\d]+)'.*", content, re.DOTALL).groups()[0]) + 1 withopen(indexjs, 'w') as f: f.write(re.sub(rex, f"'next-boot.js?{nextver}'", content, count=1)) print('updated file: ' + indexjs)
if __name__ == '__main__': links = create_links() update_bootjs(links) update_indexjs()