Skip to content

Commit

Permalink
fix(ssg): 🐛 fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Dec 26, 2024
1 parent e8e98ed commit f1c29ea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
25 changes: 18 additions & 7 deletions examples/multilingual_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//! This example demonstrates how to generate a multilingual static site
//! with a language selector at the root of the `public` directory.
use anyhow::Result;
use anyhow::Context;
use anyhow::Result;
use http_handle::Server;
use staticdatagen::compiler::service::compile;
use std::fs::{self, write};
Expand Down Expand Up @@ -46,30 +46,41 @@ fn main() -> Result<()> {
generate_language_selector(&public_root, &languages)?;

// Serve the root public directory
let server = Server::new("127.0.0.1:3000", public_root.to_str().unwrap());
let server =
Server::new("127.0.0.1:3000", public_root.to_str().unwrap());
println!("Serving site at http://127.0.0.1:3000");
let _ = server.start();

Ok(())
}

/// Generates a root `index.html` file using the `templates/selector.html` template
fn generate_language_selector(public_root: &Path, languages: &[&str]) -> Result<()> {
fn generate_language_selector(
public_root: &Path,
languages: &[&str],
) -> Result<()> {
// Read the selector.html template
let template_path = Path::new("./examples/templates/selector.html");
let template = fs::read_to_string(&template_path).context("Failed to read selector.html template")?;
let template = fs::read_to_string(&template_path)
.context("Failed to read selector.html template")?;

// Replace the placeholder with the language links
let mut language_links = String::new();
for lang in languages {
let link = format!("<li><a href=\"./{}/\">{}</a></li>\n", lang, lang.to_uppercase());
let link = format!(
"<li><a href=\"./{}/\">{}</a></li>\n",
lang,
lang.to_uppercase()
);
language_links.push_str(&link);
}
let output_html = template.replace("{{LANGUAGE_LINKS}}", &language_links);
let output_html =
template.replace("{{LANGUAGE_LINKS}}", &language_links);

// Write the generated HTML to `public/index.html`
let index_path = public_root.join("index.html");
write(index_path, output_html).context("Failed to write language selector index.html")?;
write(index_path, output_html)
.context("Failed to write language selector index.html")?;
println!(" ✅ Generated language selector at root index.html using template");

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion examples/quickstart_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ impl SiteGenerator {
let content_dir = fs::canonicalize(content_dir)?;
let output_dir = fs::canonicalize(output_dir)?;
let template_dir = fs::canonicalize(template_dir)?;
let site_dir = fs::canonicalize(site_dir.clone()).unwrap_or(site_dir);
let site_dir =
fs::canonicalize(site_dir.clone()).unwrap_or(site_dir);

// Create configuration
let config = ShokuninConfig::builder()
Expand Down

0 comments on commit f1c29ea

Please sign in to comment.