Skip to content

Commit 095a454

Browse files
committed
Fix file urls on Windows.
1 parent d4b8712 commit 095a454

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/main.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ struct Generator<'a> {
1818
handlebars: Handlebars<'a>,
1919
blogs: Vec<Blog>,
2020
out_directory: PathBuf,
21-
file_url: String,
2221
}
2322

2423
#[derive(Debug, Serialize)]
@@ -58,28 +57,29 @@ impl<'a> Generator<'a> {
5857
handlebars.register_templates_directory(".hbs", "templates")?;
5958
handlebars.register_helper("month_name", Box::new(hb_month_name_helper));
6059

61-
let file_url = format!(
62-
"file:///{}/",
63-
out_directory
64-
.as_ref()
65-
.canonicalize()
66-
.unwrap_or_else(|_| out_directory.as_ref().to_owned())
67-
.display()
68-
.to_string()
69-
.trim_start_matches('/')
70-
.replace(' ', "%20")
71-
.replace("\\\\?\\", "")
72-
.replace(std::path::MAIN_SEPARATOR, "/")
73-
);
74-
7560
Ok(Generator {
7661
handlebars,
7762
blogs: crate::blogs::load(posts_directory.as_ref())?,
7863
out_directory: out_directory.as_ref().into(),
79-
file_url,
8064
})
8165
}
8266

67+
fn file_url(&self, path: &Path) -> String {
68+
format!(
69+
"file:///{}/{}",
70+
self.out_directory
71+
.canonicalize()
72+
.unwrap_or_else(|_| self.out_directory.to_owned())
73+
.display()
74+
.to_string()
75+
.trim_start_matches('/')
76+
.replace(' ', "%20")
77+
.replace("\\\\?\\", ""),
78+
path.display()
79+
)
80+
.replace(std::path::MAIN_SEPARATOR, "/")
81+
}
82+
8383
fn render(&self) -> Result<(), Box<dyn Error>> {
8484
// make sure our output directory exists
8585
fs::create_dir_all(&self.out_directory)?;
@@ -121,15 +121,15 @@ impl<'a> Generator<'a> {
121121

122122
let path = self.render_index(blog)?;
123123

124-
println!("{}: {}{}", blog.title(), self.file_url, path.display());
124+
println!("{}: {}", blog.title(), self.file_url(&path));
125125

126126
self.render_feed(blog)?;
127127
self.render_releases_feed(blog)?;
128128

129129
for (i, post) in blog.posts().iter().enumerate() {
130130
let path = self.render_post(blog, post)?;
131131
if i == 0 {
132-
println!("└─ Latest post: {}{}\n", self.file_url, path.display());
132+
println!("└─ Latest post: {}\n", self.file_url(&path));
133133
}
134134
}
135135

0 commit comments

Comments
 (0)