Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

Commit c656d76

Browse files
committed
fix: folders with no html files will no longer be shown. show only file stem/name for the files and folders
Signed-off-by: Ryan Brue <[email protected]>
1 parent 68e1d60 commit c656d76

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ authors = ["Ryan Brue <[email protected]>"]
44
repository = "https://github.com/ryanabx/simple-ssg"
55
license = "MIT"
66
readme = "README.md"
7-
version = "3.0.2"
7+
version = "3.0.3"
88
edition = "2021"
99
description = "Plain and simple static site generator for Djot and Markdown light markup languages"
1010

src/main.rs

+60-14
Original file line numberDiff line numberDiff line change
@@ -328,42 +328,83 @@ fn generate_table_of_contents(
328328
web_prefix: Option<&str>,
329329
) -> String {
330330
let mut table_of_contents_html = "<ul>".to_string();
331+
log::debug!("<ul>");
331332
let mut prev_depth = 0;
333+
let mut prev_file_depth = 0;
334+
let mut prev_folders = Vec::new();
332335
for result in results {
333336
match result {
334337
FirstPassResult::Dir {
335338
depth,
336339
relative_path,
337340
} => {
341+
log::trace!("Dir: {}", &relative_path.to_string_lossy());
338342
let mut depth_diff = *depth as i32 - prev_depth as i32;
339343
while depth_diff < 0 {
340-
table_of_contents_html.push_str("</ul>");
344+
if prev_folders.pop().is_none() {
345+
let format_string = format!("</ul>");
346+
log::debug!("{} (Dir, depth_diff={})", &format_string, depth_diff);
347+
table_of_contents_html.push_str(&format_string);
348+
}
341349
depth_diff += 1;
342350
}
343351
prev_depth = *depth;
344352
if *depth > 0 {
345-
table_of_contents_html.push_str(&format!(
346-
"<li><b><u>{}:</u></b></li><ul>",
353+
log::trace!(
354+
"Adding {} to the folders stack (at depth {})",
347355
&relative_path.to_string_lossy(),
348-
));
356+
*depth
357+
);
358+
prev_folders.push(
359+
relative_path
360+
.file_name()
361+
.unwrap()
362+
.to_string_lossy()
363+
.to_string(),
364+
);
349365
}
350366
}
351367
FirstPassResult::HtmlOutput {
352368
relative_path,
353369
depth,
354370
..
355371
} => {
372+
log::trace!("File: {}", &relative_path.to_string_lossy());
356373
let mut depth_diff = *depth as i32 - prev_depth as i32;
357374
while depth_diff < 0 {
358-
table_of_contents_html.push_str("</ul>");
375+
if prev_folders.pop().is_none() {
376+
let format_string = format!("</ul>");
377+
log::debug!("{} (File, depth_diff={})", &format_string, depth_diff);
378+
table_of_contents_html.push_str(&format_string);
379+
}
359380
depth_diff += 1;
360381
}
382+
let mut pos_depth_diff = prev_folders.len();
383+
while pos_depth_diff > 0 {
384+
let folder_name = prev_folders.remove(0);
385+
let format_string = format!("<li><b><u>{}:</u></b></li>", &folder_name,);
386+
log::debug!(
387+
"{} (folder, depth={})",
388+
&format_string,
389+
(*depth - pos_depth_diff)
390+
);
391+
table_of_contents_html.push_str(&format_string);
392+
let format_string = format!("<ul>");
393+
log::debug!("{}, prev_folders-={}", &format_string, &folder_name);
394+
table_of_contents_html.push_str(&format_string);
395+
pos_depth_diff -= 1;
396+
}
361397
prev_depth = *depth;
398+
prev_file_depth = *depth;
362399
if relative_path == my_result {
363-
table_of_contents_html
364-
.push_str(&format!("<li><b>{}</b></li>", &relative_path.to_string_lossy()))
400+
let format_string = format!(
401+
"<li><b>{}</b></li>",
402+
&relative_path.file_stem().unwrap().to_string_lossy()
403+
);
404+
log::debug!("{} (file, depth={})", &format_string, *depth);
405+
table_of_contents_html.push_str(&format_string);
365406
} else {
366-
table_of_contents_html.push_str(&format!(
407+
let format_string = format!(
367408
"<li><a href=\"{}{}{}\">{}</a></li>",
368409
if my_depth > 1 {
369410
"../".repeat(my_depth - 1)
@@ -372,19 +413,24 @@ fn generate_table_of_contents(
372413
},
373414
&web_prefix.unwrap_or(""), // "./" if "" doesn't work
374415
&relative_path.to_string_lossy(),
375-
&relative_path.to_string_lossy()
376-
))
416+
&relative_path.file_stem().unwrap().to_string_lossy()
417+
);
418+
log::debug!("{} (file, depth={})", &format_string, *depth);
419+
table_of_contents_html.push_str(&format_string);
377420
}
378421
}
379422
}
380423
}
381-
382-
let mut depth_diff = 0 - prev_depth as i32;
424+
prev_depth -= prev_folders.len();
425+
log::trace!("prev_depth - {} = {}", prev_folders.len(), prev_depth);
426+
log::trace!("prev_file_depth = {}", prev_file_depth);
427+
let mut depth_diff = 0 - prev_file_depth as i32;
383428
while depth_diff < 0 {
384-
table_of_contents_html.push_str("</ul>");
429+
let format_string = format!("</ul>");
430+
log::debug!("{} (end, depth_diff={})", &format_string, depth_diff);
431+
table_of_contents_html.push_str(&format_string);
385432
depth_diff += 1;
386433
}
387434
// log::debug!("Table of contents: {}", &table_of_contents_html);
388-
389435
table_of_contents_html
390436
}

0 commit comments

Comments
 (0)