or
or .
# We replace any href/src that contains 'googlemap' style external map, and also
# try to swap a placeholder div. Simple: replace refs to 'assets/external/lp/...googlemap'
# with a real Google Maps embed iframe.
pattern = re.compile(r'(<[^>]*(?:href|src)=")([^"]*googlemap[^"]*)(\")', re.I)
iframe = ('')
html = pattern.sub(lambda m: m.group(1) + '#' + m.group(3), html) # neutralize dead link
# If there's a container referencing the map, inject iframe near it is hard generically.
# Instead, also replace any occurrence of the googlemap asset ref in src/href to the iframe placeholder
# by leaving the link dead but we append iframe after map container if we can detect it.
return html
def fix_lp_links(html, page_abs_path):
"""Rewrite dead /lp/www.mitsuthailand.com/... links to the corresponding local page."""
# Convert href="/lp/www.mitsuthailand.com/catalog/item/X" -> ../.../catalog/item/X/index.html
# We map by stripping the /lp/www.mitsuthailand.com prefix.
def repl(m):
prefix, u = m.group(1), m.group(2)
if "lp/www.mitsuthailand.com" not in u and "/lp/" not in u:
return m.group(0)
# extract the real path
m2 = re.search(r'(?:/lp/|/lp/www\.mitsuthailand\.com)(/.*)', u)
if not m2:
return m.group(0)
realpath = m2.group(1)
if realpath.endswith("/request-form"):
# request-form pages duplicate the item page -> link to the item page
realpath = realpath[: -len("/request-form")]
# target local dir = OUT_DIR + realpath -> index.html
# compute relative href from current page dir
target_file = os.path.join(OUT_DIR, realpath.lstrip("/"), "index.html")
if os.path.exists(target_file):
newu = os.path.relpath(target_file, os.path.dirname(page_abs_path)).replace(os.sep, "/")
return prefix + newu + '"'
return m.group(0)
return re.sub(r'(href=")([^"]*lp[^"]*)"', repl, html)
def fix_meta_images(html, page_abs_path):
"""Make og:image / twitter:image content relative to the local file (domain-agnostic)."""
def repl(m):
tag = m.group(0)
cm = re.search(r'content="([^"]*mitsuthailand\.com[^"]*)"', tag)
if not cm:
return tag
abs_url = cm.group(1)
parsed = urllib.parse.urlparse(abs_url)
# decode the path (files stored decoded)
rel_fs = urllib.parse.unquote(parsed.path).lstrip("/")
dest_file = os.path.join(OUT_DIR, rel_fs)
if not os.path.exists(dest_file):
return tag
# relative from current page
newu = os.path.relpath(dest_file, os.path.dirname(page_abs_path)).replace(os.sep, "/")
return tag.replace(cm.group(1), newu)
pat = re.compile(r']*(?:property="og:image"|name="twitter:image")[^>]*>', re.I)
return pat.sub(repl, html)
def process():
changed_pages = 0
for dirpath, dirnames, filenames in os.walk(OUT_DIR):
if "index.html" not in filenames:
continue
fpath = os.path.join(dirpath, "index.html")
with open(fpath, encoding="utf-8") as f:
html = f.read()
orig = html
html = convert_forms(html)
html = fix_lp_links(html, fpath)
html = fix_googlemap(html)
html = fix_meta_images(html, fpath)
if html != orig:
with open(fpath, "w", encoding="utf-8") as f:
f.write(html)
changed_pages += 1
print(" changed:", os.path.relpath(fpath, OUT_DIR))
print(f"Total pages changed: {changed_pages}")
if __name__ == "__main__":
process()