|
| 1 | +import os |
| 2 | + |
| 3 | +SWITCH_VERSION = "v1.1.6" |
| 4 | + |
| 5 | +TEMPLATE_LATEST = """<!doctype html> |
| 6 | +<html> |
| 7 | + <head> |
| 8 | + <title>Redirecting to {{{{ latest.name }}}} branch/version</title> |
| 9 | + <meta charset="utf-8" /> |
| 10 | + <meta |
| 11 | + http-equiv="refresh" |
| 12 | + content="0; url=/{{{{ latest.name }}}}/{path}" |
| 13 | + /> |
| 14 | + <link rel="canonical" href="/{{{{ latest.name }}}}/{path}" /> |
| 15 | + </head> |
| 16 | +</html> |
| 17 | +""" |
| 18 | + |
| 19 | +TEMPLATE_V1 = f"""<!doctype html> |
| 20 | +<html> |
| 21 | + <head> |
| 22 | + <title>Redirecting to {SWITCH_VERSION}</title> |
| 23 | + <meta charset="utf-8" /> |
| 24 | + <meta |
| 25 | + http-equiv="refresh" |
| 26 | + content="0; url=/{SWITCH_VERSION}/{{path}}" |
| 27 | + /> |
| 28 | + <link rel="canonical" href="/{SWITCH_VERSION}/{{path}}" /> |
| 29 | + </head> |
| 30 | +</html> |
| 31 | +""" |
| 32 | + |
| 33 | + |
| 34 | +def generate_templates(old_doc_dir, include_dirs, latest_files, template_dir): |
| 35 | + os.makedirs(template_dir, exist_ok=True) |
| 36 | + |
| 37 | + for curdir, dirs, files in os.walk(old_doc_dir): |
| 38 | + if any([curdir.startswith(old_doc_dir + "/" + d) for d in include_dirs]) or curdir == old_doc_dir: |
| 39 | + html_files = [f for f in files if f.endswith("html")] |
| 40 | + if html_files: |
| 41 | + os.makedirs(os.path.join(template_dir, curdir), exist_ok=True) |
| 42 | + for file in html_files: |
| 43 | + file_path = os.path.join(curdir[(len(old_doc_dir) + 1) :], file) |
| 44 | + print("Writing", os.path.join(curdir[(len(old_doc_dir) + 1) :], file)) |
| 45 | + with open(os.path.join(template_dir, file_path), "w") as f: |
| 46 | + template = TEMPLATE_LATEST if file in latest_files else TEMPLATE_V1 |
| 47 | + content = template.format(path=file_path) |
| 48 | + f.write(content) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + old_doc_dir = "../docs/v1.1.6" |
| 53 | + include_dirs = ["_examples", "_images", "_modules", "api"] |
| 54 | + latest_files = ["index.html", "about.html"] |
| 55 | + template_dir = "polyversion/templates" |
| 56 | + |
| 57 | + generate_templates(old_doc_dir, include_dirs, latest_files, template_dir) |
0 commit comments