2020-05-08 08:04:09 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
import bs4
|
|
|
|
import cssmin
|
|
|
|
import jinja2
|
|
|
|
import mkdocs.commands.build
|
|
|
|
|
|
|
|
import mdx_clickhouse
|
|
|
|
import test
|
|
|
|
import util
|
|
|
|
import website
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_amp_html(lang, args, root, site_temp, main_site_dir):
|
|
|
|
src_path = root
|
2022-03-22 16:39:58 +00:00
|
|
|
src_index = os.path.join(src_path, "index.html")
|
2020-05-08 08:04:09 +00:00
|
|
|
rel_path = os.path.relpath(src_path, site_temp)
|
2022-03-22 16:39:58 +00:00
|
|
|
dst_path = os.path.join(main_site_dir, rel_path, "amp")
|
|
|
|
dst_index = os.path.join(dst_path, "index.html")
|
2020-05-08 08:04:09 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
logging.debug(f"Generating AMP version for {rel_path} ({lang})")
|
2020-05-08 08:04:09 +00:00
|
|
|
os.makedirs(dst_path)
|
2022-03-22 16:39:58 +00:00
|
|
|
with open(src_index, "r") as f:
|
2020-05-08 08:04:09 +00:00
|
|
|
content = f.read()
|
2022-03-22 16:39:58 +00:00
|
|
|
css_in = " ".join(website.get_css_in(args))
|
2020-05-08 08:04:09 +00:00
|
|
|
command = f"purifycss --min {css_in} '{src_index}'"
|
|
|
|
logging.debug(command)
|
2022-03-22 16:39:58 +00:00
|
|
|
inline_css = subprocess.check_output(command, shell=True).decode("utf-8")
|
|
|
|
inline_css = inline_css.replace("!important", "").replace("/*!", "/*")
|
2020-05-08 08:04:09 +00:00
|
|
|
inline_css = cssmin.cssmin(inline_css)
|
2022-03-22 16:39:58 +00:00
|
|
|
content = content.replace("CUSTOM_CSS_PLACEHOLDER", inline_css)
|
2020-05-08 08:04:09 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
with open(dst_index, "w") as f:
|
2020-05-08 08:04:09 +00:00
|
|
|
f.write(content)
|
|
|
|
|
|
|
|
return dst_index
|
|
|
|
|
|
|
|
|
|
|
|
def build_amp(lang, args, cfg):
|
|
|
|
# AMP docs: https://amp.dev/documentation/
|
2022-03-22 16:39:58 +00:00
|
|
|
logging.info(f"Building AMP version for {lang}")
|
2020-05-08 08:04:09 +00:00
|
|
|
with util.temp_dir() as site_temp:
|
2022-03-22 16:39:58 +00:00
|
|
|
extra = cfg.data["extra"]
|
|
|
|
main_site_dir = cfg.data["site_dir"]
|
|
|
|
extra["is_amp"] = True
|
|
|
|
cfg.load_dict({"site_dir": site_temp, "extra": extra})
|
2020-05-08 08:04:09 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
mkdocs.commands.build.build(cfg)
|
|
|
|
except jinja2.exceptions.TemplateError:
|
|
|
|
if not args.version_prefix:
|
|
|
|
raise
|
|
|
|
mdx_clickhouse.PatchedMacrosPlugin.disabled = True
|
|
|
|
mkdocs.commands.build.build(cfg)
|
|
|
|
|
|
|
|
paths = []
|
|
|
|
for root, _, filenames in os.walk(site_temp):
|
2022-03-22 16:39:58 +00:00
|
|
|
if "index.html" in filenames:
|
|
|
|
paths.append(
|
|
|
|
prepare_amp_html(lang, args, root, site_temp, main_site_dir)
|
|
|
|
)
|
|
|
|
logging.info(f"Finished building AMP version for {lang}")
|
2020-05-08 08:04:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def html_to_amp(content):
|
2022-03-22 16:39:58 +00:00
|
|
|
soup = bs4.BeautifulSoup(content, features="html.parser")
|
2020-05-08 08:04:09 +00:00
|
|
|
|
|
|
|
for tag in soup.find_all():
|
2022-03-22 16:39:58 +00:00
|
|
|
if tag.attrs.get("id") == "tostring":
|
|
|
|
tag.attrs["id"] = "_tostring"
|
|
|
|
if tag.name == "img":
|
|
|
|
tag.name = "amp-img"
|
|
|
|
tag.attrs["layout"] = "responsive"
|
|
|
|
src = tag.attrs["src"]
|
|
|
|
if not (src.startswith("/") or src.startswith("http")):
|
|
|
|
tag.attrs["src"] = f"../{src}"
|
|
|
|
if not tag.attrs.get("width"):
|
|
|
|
tag.attrs["width"] = "640"
|
|
|
|
if not tag.attrs.get("height"):
|
|
|
|
tag.attrs["height"] = "320"
|
|
|
|
if tag.name == "iframe":
|
|
|
|
tag.name = "amp-iframe"
|
|
|
|
tag.attrs["layout"] = "responsive"
|
|
|
|
del tag.attrs["alt"]
|
|
|
|
del tag.attrs["allowfullscreen"]
|
|
|
|
if not tag.attrs.get("width"):
|
|
|
|
tag.attrs["width"] = "640"
|
|
|
|
if not tag.attrs.get("height"):
|
|
|
|
tag.attrs["height"] = "320"
|
|
|
|
elif tag.name == "a":
|
|
|
|
href = tag.attrs.get("href")
|
2020-05-08 08:04:09 +00:00
|
|
|
if href:
|
2022-03-22 16:39:58 +00:00
|
|
|
if not (href.startswith("/") or href.startswith("http")):
|
|
|
|
if "#" in href:
|
|
|
|
href, anchor = href.split("#")
|
2020-05-08 08:04:09 +00:00
|
|
|
else:
|
|
|
|
anchor = None
|
2022-03-22 16:39:58 +00:00
|
|
|
href = f"../{href}amp/"
|
2020-05-08 08:04:09 +00:00
|
|
|
if anchor:
|
2022-03-22 16:39:58 +00:00
|
|
|
href = f"{href}#{anchor}"
|
|
|
|
tag.attrs["href"] = href
|
2020-05-08 08:04:09 +00:00
|
|
|
content = str(soup)
|
|
|
|
return website.minify_html(content)
|