Skip to content

Commit bc93442

Browse files
committed
Hack the file urls into something that might work on Windows.
1 parent 3e335d1 commit bc93442

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/main.rs

Lines changed: 18 additions & 16 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,26 +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(out_directory.as_ref().to_owned())
67-
.display()
68-
.to_string()
69-
.trim_start_matches('/')
70-
.replace(' ', "%20")
71-
);
72-
7360
Ok(Generator {
7461
handlebars,
7562
blogs: crate::blogs::load(posts_directory.as_ref())?,
7663
out_directory: out_directory.as_ref().into(),
77-
file_url,
7864
})
7965
}
8066

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+
8183
fn render(&self) -> Result<(), Box<dyn Error>> {
8284
// make sure our output directory exists
8385
fs::create_dir_all(&self.out_directory)?;
@@ -119,15 +121,15 @@ impl<'a> Generator<'a> {
119121

120122
let path = self.render_index(blog)?;
121123

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

124126
self.render_feed(blog)?;
125127
self.render_releases_feed(blog)?;
126128

127129
for (i, post) in blog.posts().iter().enumerate() {
128130
let path = self.render_post(blog, post)?;
129131
if i == 0 {
130-
println!("└─ Latest post: {}{}\n", self.file_url, path.display());
132+
println!("└─ Latest post: {}\n", self.file_url(&path));
131133
}
132134
}
133135

0 commit comments

Comments
 (0)