1use crate::directives::html_escape;
15
16pub const LAYOUT_CSS: &str = include_str!("layout.css");
19
20#[derive(Debug, Clone, PartialEq)]
22pub struct NavGroup {
23 pub label: String,
25 pub items: Vec<(String, String)>,
27 pub active: bool,
29}
30
31#[derive(Debug, Clone, PartialEq)]
33pub struct TocEntry {
34 pub level: u8,
36 pub anchor: String,
38 pub text: String,
40}
41
42#[derive(Debug, Clone, PartialEq)]
44pub struct Chrome {
45 pub nav: Vec<NavGroup>,
47 pub active_route: String,
49 pub toc: Vec<TocEntry>,
51 pub prev: Option<(String, String)>,
53 pub next: Option<(String, String)>,
55 pub search_island: Option<String>,
58 pub search_index: Option<String>,
61}
62
63const THEME_SCRIPT: &str = "<script>const h=document.documentElement;const t=document.getElementById('theme-toggle');if(t){t.addEventListener('click',()=>{const n=h.dataset.theme==='light'?'dark':'light';h.dataset.theme=n;try{localStorage.setItem('docs-theme',n)}catch(e){}});}try{const s=localStorage.getItem('docs-theme');if(s)h.dataset.theme=s}catch(e){}</script>";
67
68#[must_use]
75pub fn page(doc: &str, site_title: &str, chrome: &Chrome, base_path: &str) -> String {
76 let url = |path: &str| {
78 if base_path.is_empty() {
79 format!("/{path}")
80 } else {
81 format!("{base_path}/{path}")
82 }
83 };
84 let mut nav_markup = String::from("<a class=\"brand\" href=\"");
85 nav_markup.push_str(&url(""));
86 nav_markup.push_str("\">");
87 nav_markup.push_str(&html_escape(site_title));
88 nav_markup.push_str("</a>");
89 for group in &chrome.nav {
90 nav_markup.push_str(&format!(
91 "<details class=\"nav-group{}\"><summary>{}</summary>",
92 if group.active { " open active" } else { "" },
93 html_escape(&group.label),
94 ));
95 for (route, title) in &group.items {
96 let class = if *route == chrome.active_route {
97 " class=\"active\""
98 } else {
99 ""
100 };
101 nav_markup.push_str(&format!(
102 "<a href=\"{r}\"{c}>{t}</a>",
103 r = html_escape(&url(route)),
104 c = class,
105 t = html_escape(title),
106 ));
107 }
108 nav_markup.push_str("</details>");
109 }
110
111 let toc_markup = if chrome.toc.len() >= 2 {
112 let mut toc = String::from("<aside class=\"toc\"><p class=\"toc-title\">On this page</p>");
113 for entry in &chrome.toc {
114 toc.push_str(&format!(
115 "<a href=\"#{a}\" class=\"toc-l{lvl}\">{t}</a>",
116 a = html_escape(&entry.anchor),
117 lvl = entry.level,
118 t = html_escape(&entry.text),
119 ));
120 }
121 toc.push_str("</aside>");
122 toc
123 } else {
124 String::new()
125 };
126
127 let pager_markup = |link: &Option<(String, String)>,
128 class: &'static str,
129 label: &'static str| {
130 match link {
131 Some((route, title)) => format!(
132 "<a class=\"pager {class}\" href=\"{r}\"><span class=\"pager-label\">{l}</span><span class=\"pager-title\">{t}</span></a>",
133 r = html_escape(&url(route)),
134 l = label,
135 t = html_escape(title),
136 ),
137 None => String::new(),
138 }
139 };
140
141 let search_header = chrome.search_island.as_ref().map_or_else(String::new, |chunk| {
142 let index = chrome
143 .search_index
144 .clone()
145 .unwrap_or_else(|| url("search.json"));
146 format!(
147 "<docs-island component-url=\"{u}\" client=\"idle\"><script type=\"application/json\" data-island-props>{{\"index\":\"{i}\"}}</script></docs-island>",
148 u = html_escape(chunk),
149 i = html_escape(&index),
150 )
151 });
152
153 let css_link = url("_assets/layout.css");
154
155 let doc = doc.replacen(
156 "</head>",
157 &format!(
158 "<link rel=\"stylesheet\" href=\"{}\"></head>",
159 html_escape(&css_link)
160 ),
161 1,
162 );
163 let doc = doc.replacen(
164 "<body>",
165 &format!(
166 "<body><header class=\"site-header\"><a class=\"brand\" href=\"{home}\">{b}</a>{search}<button id=\"theme-toggle\" aria-label=\"toggle theme\"></button></header>\
167<nav class=\"sidebar\">{nav}</nav>\
168<div class=\"page\"><main>",
169 home = html_escape(&url("")),
170 b = html_escape(site_title),
171 search = search_header,
172 nav = nav_markup,
173 ),
174 1,
175 );
176 doc.replacen(
177 "</body></html>",
178 &format!(
179 "<div class=\"pager\">{prev}{next}</div></main>{toc}</div>{THEME_SCRIPT}</body></html>",
180 prev = pager_markup(&chrome.prev, "prev", "Previous"),
181 next = pager_markup(&chrome.next, "next", "Next"),
182 toc = toc_markup,
183 ),
184 1,
185 )
186}
187
188#[must_use]
192pub fn extract_toc(body: &str) -> Vec<TocEntry> {
193 let mut toc = Vec::new();
194 let mut rest = body;
195 loop {
196 let next_pos = match (rest.find("<h2 id=\""), rest.find("<h3 id=\"")) {
197 (Some(a), Some(b)) => Some(a.min(b)),
198 (Some(a), None) => Some(a),
199 (None, Some(b)) => Some(b),
200 (None, None) => None,
201 };
202 let Some(pos) = next_pos else { break };
203 let Some(window) = rest.get(pos..) else { break };
204 let level = if window.starts_with("<h2") { 2 } else { 3 };
205 let Some(id_rel) = window.find("id=\"").map(|p| p + 4) else {
206 break;
207 };
208 let Some(end_rel) = window.get(id_rel..).and_then(|s| s.find('"')) else {
209 break;
210 };
211 let id_end = id_rel + end_rel;
212 let Some(anchor) = window.get(id_rel..id_end).map(str::to_string) else {
213 break;
214 };
215 let Some(gt_rel) = window.get(id_end..).and_then(|s| s.find('>')) else {
216 break;
217 };
218 let text_start = id_end + gt_rel + 1;
219 let Some(lt_rel) = window.get(text_start..).and_then(|s| s.find('<')) else {
220 break;
221 };
222 let text_end = text_start + lt_rel;
223 let Some(text) = window
224 .get(text_start..text_end)
225 .map(|s| s.trim().to_string())
226 else {
227 break;
228 };
229 if !text.is_empty() {
230 toc.push(TocEntry {
231 level,
232 anchor,
233 text,
234 });
235 }
236 rest = match window.get(text_end..) {
237 Some(r) => r,
238 None => break,
239 };
240 }
241 toc
242}