euv-docs 0.47.0

Cross-platform documentation viewer and static site generator powered by euv and euv-ui.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use super::*;

/// Search, metadata, and lookup utilities for `DocSite`.
impl DocSite {
    /// Applies site-level and locale-level presentation metadata onto this site.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable documentation site reference.
    /// - `&DocSiteConfig` - Shared site frontmatter configuration.
    /// - `Option<&DocLocaleConfig>` - Optional active locale configuration.
    pub(crate) fn apply_locale_metadata(
        &mut self,
        cfg: &DocSiteConfig,
        locale: Option<&DocLocaleConfig>,
    ) {
        self.set_release_marker(EUV_DOCS_RELEASE_MARKER.to_string());
        if let Some(logo) = cfg.get_site().try_get_logo().as_deref()
            && !logo.trim().is_empty()
        {
            self.set_logo(logo.trim().to_string());
        }
        if let Some(link) = cfg.get_site().try_get_footer_link().as_deref()
            && !link.trim().is_empty()
        {
            self.set_footer_link(link.trim().to_string());
        }
        self.set_footer_items(cfg.get_site().get_footer_items().clone());
        self.set_locales(unique_locales(cfg.get_locales()));
        if let Some(loc) = locale {
            self.set_lang(if loc.get_lang().trim().is_empty() {
                DOCS_DEFAULT_LANG.to_string()
            } else {
                loc.get_lang().trim().to_string()
            });
            self.set_locale_prefix(loc.get_prefix().clone());
            if let Some(f) = loc.try_get_footer().as_deref() {
                self.set_footer(f.to_string());
            } else if let Some(f) = cfg.get_site().try_get_footer().as_deref() {
                self.set_footer(f.to_string());
            }
            self.set_toc_label(
                loc.try_get_toc_label()
                    .clone()
                    .unwrap_or_else(|| DOCS_DEFAULT_TOC_LABEL.to_string()),
            );
            self.set_prev_label(
                loc.try_get_prev_label()
                    .clone()
                    .unwrap_or_else(|| DOCS_DEFAULT_PREV_LABEL.to_string()),
            );
            self.set_next_label(
                loc.try_get_next_label()
                    .clone()
                    .unwrap_or_else(|| DOCS_DEFAULT_NEXT_LABEL.to_string()),
            );
            self.set_navbar(loc.get_navbar().clone());
        } else {
            self.set_lang(DOCS_DEFAULT_LANG.to_string());
            self.set_locale_prefix("/".to_string());
            if let Some(f) = cfg.get_site().try_get_footer().as_deref() {
                self.set_footer(f.to_string());
            }
            self.set_toc_label(DOCS_DEFAULT_TOC_LABEL.to_string());
            self.set_prev_label(DOCS_DEFAULT_PREV_LABEL.to_string());
            self.set_next_label(DOCS_DEFAULT_NEXT_LABEL.to_string());
        }
    }

    /// Returns the resolved BCP 47 language tag, defaulting to `"en-US"`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Documentation site reference.
    ///
    /// # Returns
    ///
    /// - `&str` - Active HTML language tag.
    #[must_use]
    pub fn resolved_lang(&self) -> &str {
        let lang: &str = self.get_lang().trim();
        if lang.is_empty() {
            DOCS_DEFAULT_LANG
        } else {
            lang
        }
    }

    /// Returns the resolved localized TOC title label.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Documentation site reference.
    ///
    /// # Returns
    ///
    /// - `&str` - Localized TOC title label.
    #[must_use]
    pub fn resolved_toc_label(&self) -> &str {
        let lbl: &str = self.get_toc_label().trim();
        if lbl.is_empty() {
            DOCS_DEFAULT_TOC_LABEL
        } else {
            lbl
        }
    }

    /// Returns the resolved localized previous-page pager label.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Documentation site reference.
    ///
    /// # Returns
    ///
    /// - `&str` - Localized previous-page label.
    #[must_use]
    pub fn resolved_prev_label(&self) -> &str {
        let lbl: &str = self.get_prev_label().trim();
        if lbl.is_empty() {
            DOCS_DEFAULT_PREV_LABEL
        } else {
            lbl
        }
    }

    /// Returns the resolved localized next-page pager label.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Documentation site reference.
    ///
    /// # Returns
    ///
    /// - `&str` - Localized next-page label.
    #[must_use]
    pub fn resolved_next_label(&self) -> &str {
        let lbl: &str = self.get_next_label().trim();
        if lbl.is_empty() {
            DOCS_DEFAULT_NEXT_LABEL
        } else {
            lbl
        }
    }

    /// Searches pages matching `query` (case-insensitive) across title, route, and content blocks.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Documentation site reference.
    /// - `&str` - Search query text.
    ///
    /// # Returns
    ///
    /// - `Vec<&DocPage>` - Matching pages in display order.
    #[must_use]
    pub fn search(&self, query: &str) -> Vec<&DocPage> {
        let needle: String = query.trim().to_ascii_lowercase();
        if needle.is_empty() {
            return self.get_pages().iter().collect();
        }
        self.get_pages()
            .iter()
            .filter(|page: &&DocPage| page.matches_query(&needle))
            .collect()
    }

    /// Looks up a page by its canonical route path (normalizing trailing slashes and `.html`).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Documentation site reference.
    /// - `&str` - Route path to locate.
    ///
    /// # Returns
    ///
    /// - `Option<&DocPage>` - Matching page if present.
    #[must_use]
    pub fn find_by_route(&self, route: &str) -> Option<&DocPage> {
        if let Some(exact) = self
            .get_pages()
            .iter()
            .find(|page: &&DocPage| page.get_route() == route)
        {
            return Some(exact);
        }
        let cleaned: &str = route
            .split('#')
            .next()
            .unwrap_or(route)
            .split('?')
            .next()
            .unwrap_or(route)
            .trim_end_matches('/')
            .trim_end_matches(".html");
        let normalized: String = if cleaned.is_empty() {
            "/".to_string()
        } else if cleaned.starts_with('/') {
            cleaned.to_string()
        } else {
            format!("/{cleaned}")
        };
        self.get_pages()
            .iter()
            .find(|page: &&DocPage| page.get_route() == &normalized)
    }

    /// Builds compact full-text search index entries for every page in this site.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Documentation site reference.
    ///
    /// # Returns
    ///
    /// - `Vec<SearchIndexEntry>` - Ordered search records ready for JSON serialization.
    #[must_use]
    pub fn search_entries(&self) -> Vec<SearchIndexEntry> {
        self.get_pages()
            .iter()
            .map(|page: &DocPage| {
                let headings: Vec<String> = page
                    .get_toc()
                    .iter()
                    .map(|item: &DocTocItem| item.get_title().clone())
                    .collect();
                let mut excerpt: String = String::new();
                for block in page.get_blocks().iter() {
                    if excerpt.len() >= 360 {
                        break;
                    }
                    match block {
                        DocBlock::Paragraph(text)
                        | DocBlock::ListItem(text)
                        | DocBlock::BlockQuote(text) => {
                            if !excerpt.is_empty() {
                                excerpt.push(' ');
                            }
                            excerpt.push_str(text);
                        }
                        DocBlock::Table { headers, rows } => {
                            for h in headers.iter() {
                                if !excerpt.is_empty() {
                                    excerpt.push(' ');
                                }
                                excerpt.push_str(h);
                            }
                            for r in rows.iter().take(3) {
                                for cell in r.iter() {
                                    if !excerpt.is_empty() {
                                        excerpt.push(' ');
                                    }
                                    excerpt.push_str(cell);
                                }
                            }
                        }
                        DocBlock::Image { alt, .. } => {
                            if !alt.is_empty() {
                                if !excerpt.is_empty() {
                                    excerpt.push(' ');
                                }
                                excerpt.push_str(alt);
                            }
                        }
                        DocBlock::Heading { .. }
                        | DocBlock::Code { .. }
                        | DocBlock::HorizontalRule => {}
                    }
                }
                let trimmed: String = excerpt.chars().take(360).collect();
                SearchIndexEntry::new(
                    page.get_route().clone(),
                    page.get_title().clone(),
                    page.get_section().clone(),
                    page.get_summary().clone(),
                    headings,
                    trimmed,
                )
            })
            .collect()
    }
}

/// Query matching, reading-time estimation, and frontmatter metadata helpers for `DocPage`.
impl DocPage {
    /// Stores the raw Markdown body slice for full-fidelity HTML and native rendering.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable documentation page reference.
    /// - `String` - Raw Markdown body text without frontmatter.
    pub(crate) fn set_markdown_body(&mut self, body: String) {
        self.markdown_body = body;
    }

    /// Stores optional last-updated frontmatter stamp.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable documentation page reference.
    /// - `Option<String>` - Optional date or version string.
    pub(crate) fn set_updated(&mut self, updated: Option<String>) {
        self.updated = updated;
    }

    /// Stores landing-page frontmatter metadata (`home`, `heroText`, `actions`, `stats`, `features`).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable documentation page reference.
    /// - `bool` - Whether this page uses the home landing layout.
    /// - `Option<String>` - Optional hero display heading.
    /// - `Vec<DocHomeAction>` - Landing-page call-to-action buttons.
    /// - `Vec<DocHomeStat>` - Landing-page metric tiles.
    /// - `Vec<DocHomeFeature>` - Landing-page feature cards.
    pub(crate) fn set_home_meta(
        &mut self,
        is_home: bool,
        hero_text: Option<String>,
        actions: Vec<DocHomeAction>,
        stats: Vec<DocHomeStat>,
        features: Vec<DocHomeFeature>,
    ) {
        self.is_home = is_home;
        self.hero_text = hero_text;
        self.actions = actions;
        self.stats = stats;
        self.features = features;
    }

    /// Marks whether this page should be omitted from sidebar navigation trees.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable documentation page reference.
    /// - `bool` - `true` when `sidebar: false` is set in frontmatter.
    pub(crate) fn set_hide_sidebar(&mut self, hide: bool) {
        self.hide_sidebar = hide;
    }

    /// Returns the raw Markdown body slice without frontmatter.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Documentation page reference.
    ///
    /// # Returns
    ///
    /// - `&str` - Raw Markdown body text.
    #[must_use]
    pub fn markdown_source(&self) -> &str {
        self.get_markdown_body().as_str()
    }

    /// Estimates the word count of this document across prose and Markdown source.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Documentation page reference.
    ///
    /// # Returns
    ///
    /// - `usize` - Estimated readable word count.
    #[must_use]
    pub fn word_count(&self) -> usize {
        let body: &str = self.get_markdown_body().as_str();
        if !body.trim().is_empty() {
            let ws_words: usize = body.split_whitespace().count();
            let cjk_chars: usize = body
                .chars()
                .filter(|c: &char| {
                    let cp: u32 = *c as u32;
                    (0x4E00..=0x9FFF).contains(&cp)
                        || (0x3040..=0x30FF).contains(&cp)
                        || (0xAC00..=0xD7AF).contains(&cp)
                })
                .count();
            return ws_words.saturating_add(cjk_chars / 2).max(1);
        }
        let mut words: usize = 0;
        for block in self.get_blocks().iter() {
            match block {
                DocBlock::Heading { text, .. }
                | DocBlock::Paragraph(text)
                | DocBlock::ListItem(text)
                | DocBlock::BlockQuote(text) => {
                    words += text.split_whitespace().count();
                }
                DocBlock::Code { code, .. } => {
                    words += code.split_whitespace().count() / 2;
                }
                DocBlock::Table { headers, rows } => {
                    for h in headers.iter() {
                        words += h.split_whitespace().count();
                    }
                    for r in rows.iter() {
                        for cell in r.iter() {
                            words += cell.split_whitespace().count();
                        }
                    }
                }
                DocBlock::Image { alt, .. } => {
                    words += alt.split_whitespace().count();
                }
                DocBlock::HorizontalRule => {}
            }
        }
        words.max(1)
    }

    /// Estimates the reading time in whole minutes (`>= 1`).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Documentation page reference.
    ///
    /// # Returns
    ///
    /// - `usize` - Estimated reading time in minutes.
    #[must_use]
    pub fn reading_minutes(&self) -> usize {
        let words: usize = self.word_count();
        words.div_ceil(DOCS_WORDS_PER_MINUTE).max(1)
    }

    /// Returns `true` if `needle` (already lowercased) matches the title, route, summary, or any block.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Documentation page reference.
    /// - `&str` - Lowercased search query.
    ///
    /// # Returns
    ///
    /// - `bool` - Whether the page matches.
    #[must_use]
    pub fn matches_query(&self, needle: &str) -> bool {
        if self.get_title().to_ascii_lowercase().contains(needle)
            || self.get_route().to_ascii_lowercase().contains(needle)
            || self.get_summary().to_ascii_lowercase().contains(needle)
        {
            return true;
        }
        self.get_blocks()
            .iter()
            .any(|block: &DocBlock| match block {
                DocBlock::Heading { text, .. } => text.to_ascii_lowercase().contains(needle),
                DocBlock::Paragraph(text)
                | DocBlock::ListItem(text)
                | DocBlock::BlockQuote(text) => text.to_ascii_lowercase().contains(needle),
                DocBlock::Code { code, .. } => code.to_ascii_lowercase().contains(needle),
                DocBlock::Image { alt, .. } => alt.to_ascii_lowercase().contains(needle),
                DocBlock::Table { headers, rows } => {
                    let in_headers: bool = headers
                        .iter()
                        .any(|h: &String| h.to_ascii_lowercase().contains(needle));
                    let in_rows: bool = rows.iter().any(|row: &Vec<String>| {
                        row.iter()
                            .any(|cell: &String| cell.to_ascii_lowercase().contains(needle))
                    });
                    in_headers || in_rows
                }
                DocBlock::HorizontalRule => false,
            })
    }
}