|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: GPL-2.0 |
| 3 | + |
| 4 | +"""Postprocesses the "book". |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +import os |
| 9 | +import pathlib |
| 10 | +import re |
| 11 | +import shutil |
| 12 | + |
| 13 | +def handle_markdown(string): |
| 14 | + if string.count("`") % 2 != 0: |
| 15 | + raise RuntimeError("Unbalanced backticks.") |
| 16 | + |
| 17 | + while True: |
| 18 | + if "`" not in string: |
| 19 | + return string |
| 20 | + |
| 21 | + # Should use `code`, but it requires fixing the general CSS. |
| 22 | + string = string.replace("`", '<span class="rfl-mono-font">', 1) |
| 23 | + string = string.replace("`", "</span>", 1) |
| 24 | + |
| 25 | +def process(path, content, html_menu, html_index_toc): |
| 26 | + # Fix index title (`index.html` and `Rust-for-Linux.html`). |
| 27 | + content = content.replace("<title>Rust for Linux - Rust for Linux</title>", "<title>Rust for Linux</title>") |
| 28 | + |
| 29 | + # Fix header. |
| 30 | + content = content.replace('<link rel="icon" href="favicon.svg">', "") |
| 31 | + content = content.replace('<link rel="shortcut icon" href="favicon.png">', """ |
| 32 | + <link rel="icon" href="Rust-for-Linux.svg"> |
| 33 | +
|
| 34 | + <meta property="og:url" content="https://rust-for-linux.com"> |
| 35 | + <meta property="og:type" content="website"> |
| 36 | + <meta property="og:title" content="Rust for Linux"> |
| 37 | + <meta property="og:description" content="Adding support for the Rust language to the Linux kernel"> |
| 38 | + <meta property="og:image" content="https://rust-for-linux.com/Rust-for-Linux.svg"> |
| 39 | + <script type="application/ld+json"> |
| 40 | + { |
| 41 | + "@context": "http://schema.org", |
| 42 | + "@type": "CreativeWork", |
| 43 | + "name": "Rust for Linux", |
| 44 | + "description": "Adding support for the Rust language to the Linux kernel", |
| 45 | + "url": "https://rust-for-linux.com", |
| 46 | + "image": "https://rust-for-linux.com/Rust-for-Linux.svg" |
| 47 | + } |
| 48 | + </script> |
| 49 | + <style> |
| 50 | + #logo { |
| 51 | + display: block; |
| 52 | + margin-left: auto; |
| 53 | + margin-right: auto; |
| 54 | + } |
| 55 | +
|
| 56 | + .rfl-menu-block { |
| 57 | + text-align: center; |
| 58 | + font-size: 1.8rem; |
| 59 | + font-weight: bold; |
| 60 | + } |
| 61 | +
|
| 62 | + .rfl-mono-font { |
| 63 | + font-family: var(--mono-font); |
| 64 | + } |
| 65 | +
|
| 66 | + .rfl-mobile-links { |
| 67 | + display: none; |
| 68 | + } |
| 69 | +
|
| 70 | + @media only screen and (max-width: 1080px) { |
| 71 | + .rfl-mobile-links { |
| 72 | + display: block; |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + .chapter li.chapter-item { |
| 77 | + margin-top: 0.2em !important; |
| 78 | + margin-left: 1.5em !important; |
| 79 | + } |
| 80 | +
|
| 81 | + .chapter li.part-title { |
| 82 | + margin-bottom: -0.3em !important; |
| 83 | + margin-top: 1em !important; |
| 84 | + } |
| 85 | + </style> |
| 86 | + """) |
| 87 | + |
| 88 | + # Provide custom menu. |
| 89 | + content = re.sub(r'<div class="sidebar-scrollbox">.*?</div>', html_menu, content, count=1, flags=re.DOTALL) |
| 90 | + |
| 91 | + # Append Table of Contents in index (`index.html` and `Rust-for-Linux.html`), as well as |
| 92 | + # the links for mobile. |
| 93 | + content = content.replace("<!-- Generated TOC -->", html_index_toc) |
| 94 | + |
| 95 | + return content |
| 96 | + |
| 97 | +def main(): |
| 98 | + # Read TOC generated by `pre.py`. |
| 99 | + with open("summary.json", "r", encoding="utf-8") as f: |
| 100 | + json_summary = json.load(f) |
| 101 | + |
| 102 | + # Generate menu. |
| 103 | + html_menu = "" |
| 104 | + for block_name in json_summary: |
| 105 | + html_block = "" |
| 106 | + for section_name in json_summary[block_name]: |
| 107 | + if section_name != "": |
| 108 | + html_block += f'<li class="part-title">{section_name}</li>' |
| 109 | + for (text, link) in json_summary[block_name][section_name]: |
| 110 | + html_block += f'<li class="chapter-item"><a href="{link}" tabindex="0">{handle_markdown(text)}</a></li>' |
| 111 | + |
| 112 | + html_menu += f""" |
| 113 | + <p class="rfl-menu-block">{block_name}</p> |
| 114 | + <ol class="chapter">{html_block}</ol> |
| 115 | + """ |
| 116 | + |
| 117 | + html_menu = f""" |
| 118 | + <div class="sidebar-scrollbox"> |
| 119 | + <a href="/"><img id="logo" src="Rust-for-Linux.svg" alt="Rust for Linux Logo"></a> |
| 120 | + {html_menu} |
| 121 | + </div> |
| 122 | + """ |
| 123 | + |
| 124 | + # Generate TOC for index pages. |
| 125 | + html_index_toc = {} |
| 126 | + for block_name in json_summary: |
| 127 | + html_index_toc[block_name] = "" |
| 128 | + for section_name in json_summary[block_name]: |
| 129 | + section_links = "" |
| 130 | + for (text, link) in json_summary[block_name][section_name]: |
| 131 | + section_links += f'<li><a href="{link}" tabindex="0">{handle_markdown(text)}</a></li>' |
| 132 | + |
| 133 | + if section_name != "": |
| 134 | + html_index_toc[block_name] += f"<h3>{section_name}</h3>\n" |
| 135 | + if section_links != "": |
| 136 | + html_index_toc[block_name] += f"<ul>{section_links}</ul>\n" |
| 137 | + |
| 138 | + html_index_toc = f""" |
| 139 | + <h2><a class="header" href="#the-project">The project</a></h2> |
| 140 | + {html_index_toc["The project"]} |
| 141 | + <div class="rfl-mobile-links"> |
| 142 | + <h2><a class="header" href="#links">Links</a></h2> |
| 143 | + {html_index_toc["Links"]} |
| 144 | + </div> |
| 145 | + """ |
| 146 | + |
| 147 | + # Post-process each `mdbook`-generated file. |
| 148 | + for path in pathlib.Path().glob("book/**/*.html"): |
| 149 | + with path.open("r+", encoding="utf-8") as f: |
| 150 | + new_content = process(path, f.read(), html_menu, html_index_toc) |
| 151 | + f.seek(0) |
| 152 | + f.truncate() |
| 153 | + f.write(new_content) |
| 154 | + |
| 155 | + # Remove unneeded files. |
| 156 | + os.remove("book/favicon.svg") |
| 157 | + os.remove("book/favicon.png") |
| 158 | + |
| 159 | + # Copy logo. |
| 160 | + shutil.copyfile("Rust-for-Linux.svg", "book/Rust-for-Linux.svg") |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + main() |
0 commit comments