def render_categories( base_path: str, links_by_category: LinksByCategory, categories, template, ): logger.info("Rendering categories.") for category_str, links in links_by_category.items(): category = categories[category_str] file_path: str = join(base_path, cast(str, category["path"]), "index.html") root_path: str = get_category_root_path(category_str) with open(file_path, "w") as file: file.write( htmlmin( template.render( site_title=ENV["SITE_TITLE"], links=links, root_path=root_path, category=category, categories=categories, env=ENV, ), **HTMLMIN_KWARGS, )) for category_str, category in categories.items(): if category_str in links_by_category: continue file_path = join(base_path, category["path"], "index.html") root_path = get_category_root_path(category_str) with open(file_path, "w") as file: file.write( htmlmin( template.render( links=[], root_path=root_path, category=category, categories=categories, env=ENV, ), **HTMLMIN_KWARGS, ))
def minify_html_file(filename): """Create a minified HTML file, overwriting the original. :param str filename: The file to minify. """ with open(filename, encoding='utf-8') as f: raw = f.read() with open(filename, 'w', encoding='utf-8') as f: logger.debug('Minifying: %s' % filename) f.write(htmlmin(raw))
def render_links( base_path: str, links_by_category: LinksByCategory, template: Template, ): logger.info("Rendering links.") force = strtobool(cast(str, ENV.get("FORCE_SCREENSHOT", "False"))) cleaner_js: str = """ document.getElementsByTagName('header')[0].style.background='none'; document.getElementsByTagName('form')[0].remove(); document.getElementById('page').style.margin=0; document.getElementById('link_detail').style.margin=0; text = document.getElementsByTagName('h1')[0].textContent; document.getElementsByTagName('h1')[0].textContent=text.toUpperCase(); document.getElementsByTagName('header')[0].style.background='none'; document.getElementsByTagName('script')[0].remove(); document.getElementsByClassName('meta')[0].remove(); document.getElementsByClassName('socializer')[0].remove() document.getElementsByTagName('p')[1].classList.remove('mb'); """ browser = get_browser() if browser is None: logger.info("Not able to run Selenium. " "Screenshots will not be generated.") for category_str, links in links_by_category.items(): for link in links: file_path = join(base_path, cast(str, link.file_path)) image_url: str = f"{link.file_path}.png" with open(file_path, "w") as file: file.write( htmlmin( template.render( link=link, root_path=get_category_root_path(category_str), image_url=image_url, env=ENV, ), **HTMLMIN_KWARGS, )) logger.debug(f'{file_path} written.') image_path = join(base_path, image_url) if force or not exists(image_path) and browser: browser.get("file://" + join(base_path, file_path)) browser.execute_script(cleaner_js) browser.save_screenshot(join(base_path, image_url)) if browser: browser.close()
def render_sitemap( root_path: str, categories: Dict[str, Union[str, None, List[str]]], links_by_category: LinksByCategory, sitemap_template: Template, ): logger.info("Rendering sitemap.") with open(join(root_path, "sitemap.xml"), "w") as file: file.write( htmlmin( sitemap_template.render( root_path=root_path, links_by_category=links_by_category, categories=categories, render_date=datetime.date.today(), strftime=datetime.date.strftime, ), **HTMLMIN_KWARGS, ))
def process_html(pathSelf, raw): dependencies = re.finditer(r'(href|src)="(/?[^"/][^":]*\.(css|js|json))"', raw) for match in dependencies: dependency = match.group(0) prop = match.group(1) path = match.group(2) pathResolved = resolve_path(pathSelf, path) if pathResolved not in all_assets: print("# Skipping:", pathResolved, file=sys.stderr) continue pathDigest = digestify_path(path, pathResolved) raw = raw.replace(dependency, prop + '="' + pathDigest + '"') meta.deps.add((pathSelf, pathResolved)) raw = process_includes(pathSelf, raw) raw = process_paths(pathSelf, raw) return htmlmin(raw, remove_comments=True, remove_optional_attribute_quotes=False, )
def render_home( base_path: str, link_rows: List[LinkRow], categories: Dict[str, Union[str, None, List[str]]], template: Template, ): logger.info("Rendering homepage.") links = get_links_by_date(link_rows) last_update = datetime.date.today() file_path = join(base_path, "index.html") with open(file_path, "w") as file: file.write( htmlmin( template.render( latest_links=links[:50], root_path="./", categories=categories, last_update=last_update, num_of_links=len(link_rows), env=ENV, ), **HTMLMIN_KWARGS, ))
def minifyHTMLProc(srcText): srcText = srcText.replace("<script", "<script async") return htmlmin(srcText)