|
6 | 6 | //! This example demonstrates how to generate a multilingual static site
|
7 | 7 | //! with a language selector at the root of the `public` directory.
|
8 | 8 |
|
9 |
| -use anyhow::Result; |
10 | 9 | use anyhow::Context;
|
| 10 | +use anyhow::Result; |
11 | 11 | use http_handle::Server;
|
12 | 12 | use staticdatagen::compiler::service::compile;
|
13 | 13 | use std::fs::{self, write};
|
@@ -46,30 +46,41 @@ fn main() -> Result<()> {
|
46 | 46 | generate_language_selector(&public_root, &languages)?;
|
47 | 47 |
|
48 | 48 | // Serve the root public directory
|
49 |
| - let server = Server::new("127.0.0.1:3000", public_root.to_str().unwrap()); |
| 49 | + let server = |
| 50 | + Server::new("127.0.0.1:3000", public_root.to_str().unwrap()); |
50 | 51 | println!("Serving site at http://127.0.0.1:3000");
|
51 | 52 | let _ = server.start();
|
52 | 53 |
|
53 | 54 | Ok(())
|
54 | 55 | }
|
55 | 56 |
|
56 | 57 | /// Generates a root `index.html` file using the `templates/selector.html` template
|
57 |
| -fn generate_language_selector(public_root: &Path, languages: &[&str]) -> Result<()> { |
| 58 | +fn generate_language_selector( |
| 59 | + public_root: &Path, |
| 60 | + languages: &[&str], |
| 61 | +) -> Result<()> { |
58 | 62 | // Read the selector.html template
|
59 | 63 | let template_path = Path::new("./examples/templates/selector.html");
|
60 |
| - let template = fs::read_to_string(&template_path).context("Failed to read selector.html template")?; |
| 64 | + let template = fs::read_to_string(&template_path) |
| 65 | + .context("Failed to read selector.html template")?; |
61 | 66 |
|
62 | 67 | // Replace the placeholder with the language links
|
63 | 68 | let mut language_links = String::new();
|
64 | 69 | for lang in languages {
|
65 |
| - let link = format!("<li><a href=\"./{}/\">{}</a></li>\n", lang, lang.to_uppercase()); |
| 70 | + let link = format!( |
| 71 | + "<li><a href=\"./{}/\">{}</a></li>\n", |
| 72 | + lang, |
| 73 | + lang.to_uppercase() |
| 74 | + ); |
66 | 75 | language_links.push_str(&link);
|
67 | 76 | }
|
68 |
| - let output_html = template.replace("{{LANGUAGE_LINKS}}", &language_links); |
| 77 | + let output_html = |
| 78 | + template.replace("{{LANGUAGE_LINKS}}", &language_links); |
69 | 79 |
|
70 | 80 | // Write the generated HTML to `public/index.html`
|
71 | 81 | let index_path = public_root.join("index.html");
|
72 |
| - write(index_path, output_html).context("Failed to write language selector index.html")?; |
| 82 | + write(index_path, output_html) |
| 83 | + .context("Failed to write language selector index.html")?; |
73 | 84 | println!(" ✅ Generated language selector at root index.html using template");
|
74 | 85 |
|
75 | 86 | Ok(())
|
|
0 commit comments