fix image pages

This commit is contained in:
Nick Pegg 2025-05-06 21:16:29 -07:00
parent 141ea6dc5d
commit 2bacda9de5
3 changed files with 37 additions and 38 deletions

View file

@ -5,11 +5,11 @@
document.onkeydown = function(event) {
if (event.key == "ArrowLeft") {
{% if prev_image %}
location.href = "{{prev_image.html_path}}";
location.href = "{{prev_image.html_filename}}";
{% endif %}
} else if (event.key == "ArrowRight") {
{% if next_image %}
location.href = "{{next_image.html_path}}";
location.href = "{{next_image.html_filename}}";
{% endif %}
}
}
@ -17,13 +17,13 @@
{% block content %}
<div id="photo">
<img src="{{image.screen_path}}" />
<img src="{{image.screen_filename}}" />
</div>
<div id="nav">
<div>
{% if prev_image %}
<a href="{{prev_image.html_path}}">
<a href="{{prev_image.html_filename}}">
<i class="arrow arrow-left"></i>
</a>
{% endif %}
@ -35,7 +35,7 @@
</div>
<div>
{% if next_image %}
<a href="{{next_image.html_path}}">
<a href="{{next_image.html_filename}}">
<i class="arrow arrow-right"></i>
</a>
{% endif %}

View file

@ -105,7 +105,6 @@ impl TryFrom<&AlbumDir> for AlbumContext {
/// A Tera context for slide (individual image) pages
#[derive(Serialize, Debug)]
struct SlideContext {
// TODO: Path or String?
// Path required to get back to the root album
root_path: PathBuf,
image: Image,
@ -215,38 +214,37 @@ fn generate_html(config: &Config, album: &AlbumDir) -> anyhow::Result<()> {
for child in album.children.iter() {
dir_queue.push_back(&child);
}
}
let all_images: Vec<&Image> = album.iter().collect();
for (pos, img) in all_images.iter().enumerate() {
let img: &Image = *img;
let prev_image: Option<&Image> = match pos {
0 => None,
n => Some(&all_images[n - 1]),
};
let next_image: Option<&Image> = all_images.get(pos + 1).map(|i| *i);
for (pos, img) in album.images.iter().enumerate() {
let prev_image: Option<&Image> = match pos {
0 => None,
n => Some(&album.images[n - 1]),
};
let next_image: Option<&Image> = album.images.get(pos + 1);
// Find the path to the root by counting the parts of the path
let mut path_to_root = PathBuf::new();
if let Some(parent) = img.path.parent() {
let mut parent = parent.to_path_buf();
while parent.pop() {
path_to_root = path_to_root.join("..");
// Find the path to the root by counting the parts of the path
// Start with 1 .. to get out of the slides dir
let mut path_to_root = PathBuf::from("..");
if let Some(parent) = img.path.parent() {
let mut parent = parent.to_path_buf();
while parent.pop() {
path_to_root.push("..");
}
}
}
log::info!("Rendering image {}", img.html_path.display());
let ctx = SlideContext {
root_path: path_to_root,
image: img.clone(),
prev_image: prev_image.cloned(),
next_image: next_image.cloned(),
};
log::debug!("Image context: {ctx:?}");
fs::write(
output_path.join(&img.html_path),
tera.render("photo.html", &tera::Context::from_serialize(&ctx)?)?,
)?;
log::info!("Rendering image {}", img.html_path.display());
let ctx = SlideContext {
root_path: path_to_root,
image: img.clone(),
prev_image: prev_image.cloned(),
next_image: next_image.cloned(),
};
log::debug!("Image context: {ctx:?}");
fs::write(
output_path.join(&img.html_path),
tera.render("photo.html", &tera::Context::from_serialize(&ctx)?)?,
)?;
}
}
Ok(())

View file

@ -59,10 +59,11 @@ impl AlbumDir {
let mut description = String::new();
// Read in any associated description file
if entry_path.with_extension(".txt").exists() {
description = fs::read_to_string(&entry_path)?;
} else if entry_path.with_extension(".md").exists() {
let _contents = fs::read(entry_path)?;
if entry_path.with_extension("txt").exists() {
description =
fs::read_to_string(&entry_path.with_extension("txt"))?;
} else if entry_path.with_extension("md").exists() {
let _contents = fs::read(&entry_path.with_extension("md"))?;
// TODO: render markdown
todo!();
}