diff options
-rw-r--r-- | riki.md | 48 | ||||
-rw-r--r-- | src/name.rs | 63 |
2 files changed, 74 insertions, 37 deletions
@@ -27,7 +27,7 @@ be an empty HTML file._ given an installed riki given file site/empty.mdwn from empty when I run riki build --plain-body site output -then AST of site/empty.mdwn matches that of output/empty.html +then AST of site/empty.mdwn matches that of output/empty/index.html ~~~ @@ -43,7 +43,7 @@ be an HTML file with the same text, without extra elements._ given an installed riki given file site/page.mdwn from para when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -63,7 +63,7 @@ output must have a blockquote element. given an installed riki given file site/page.mdwn from blockquote when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -81,7 +81,7 @@ output must have a pre element. given an installed riki given file site/page.mdwn from indented-code when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -99,7 +99,7 @@ output must have a pre element. given an installed riki given file site/page.mdwn from fenced-code when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -120,7 +120,7 @@ must have an img element. given an installed riki given file site/page.mdwn from image-link when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -137,7 +137,7 @@ in HTML output._ given an installed riki given file site/page.mdwn from emph when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -154,7 +154,7 @@ strong element in HTML output._ given an installed riki given file site/page.mdwn from strong when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -171,7 +171,7 @@ element in HTML output._ given an installed riki given file site/page.mdwn from strike when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -190,7 +190,7 @@ supported._ given an installed riki given file site/page.mdwn from headings when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -212,7 +212,7 @@ element in HTML output._ given an installed riki given file site/page.mdwn from backticks when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -231,7 +231,7 @@ output._ given an installed riki given file site/page.mdwn from table when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -252,7 +252,7 @@ HTML output._ given an installed riki given file site/page.mdwn from rule when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -273,7 +273,7 @@ in HTML output._ given an installed riki given file site/page.mdwn from ul when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -294,7 +294,7 @@ the same as the Markdown.*** given an installed riki given file site/page.mdwn from ol when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ <!-- @@ -313,7 +313,7 @@ in HTML output._ given an installed riki given file site/page.mdwn from tasklist when I run riki build --plain-body site output -then AST of site/page.mdwn matches that of output/page.html +then AST of site/page.mdwn matches that of output/page/index.html ~~~ @@ -347,7 +347,7 @@ then stderr contains "definition list" given file site/page.mdwn from dl-3 when I run riki build --plain-body site output -then file output/page.html contains ": bar" +then file output/page/index.html contains ": bar" ~~~ ~~~{#dl-1 .file} @@ -381,10 +381,10 @@ given file site/dir/sibling.mdwn from empty given file site/dir/foo/child.mdwn from empty given file site/dir/foo/child/grandchild.mdwn from empty when I run riki build --plain-body site output -then file output/dir/foo.html contains "href="../absolute"" -then file output/dir/foo.html contains "href="sibling"" -then file output/dir/foo.html contains "href="foo/child"" -then file output/dir/foo.html contains "href="foo/child/grandchild"" +then file output/dir/foo/index.html contains "href="../absolute"" +then file output/dir/foo/index.html contains "href="sibling"" +then file output/dir/foo/index.html contains "href="foo/child"" +then file output/dir/foo/index.html contains "href="foo/child/grandchild"" ~~~ Note the uppercase link to the `child` page in the test page below. @@ -558,7 +558,7 @@ given an installed riki given file site/a.mdwn from use_shortcut given file site/b.mdwn from define_shortcut when I run riki build site output -then file output/a.html contains "<a href="https://example.com/foo/123">foo!123</a>" +then file output/a/index.html contains "<a href="https://example.com/foo/123">foo!123</a>" ~~~ ~~~{#use_shortcut .file .markdown} @@ -635,8 +635,8 @@ given an installed riki given file site/foo/page.mdwn from image given file site/bar/image.jpg from para when I run riki build --plain-body site output -then AST of site/foo/page.mdwn matches that of output/foo/page.html -then files site/bar//image.jpg and output/bar/image.jpg match +then AST of site/foo/page.mdwn matches that of output/foo/page/index.html +then files site/bar/image.jpg and output/bar/image.jpg match ~~~ ## Output directory tree diff --git a/src/name.rs b/src/name.rs index 4cb5f16..3cb0157 100644 --- a/src/name.rs +++ b/src/name.rs @@ -1,5 +1,7 @@ use crate::util::{join_subpath, make_path_absolute, make_path_relative_to}; +use log::trace; use std::cmp::Ordering; +use std::ffi::OsStr; use std::fmt; use std::path::{Path, PathBuf}; @@ -14,9 +16,16 @@ pub struct Name { impl Name { fn new(is_wikitext: bool, src: PathBuf, dest: PathBuf, page: PathBuf) -> Self { + trace!( + "Name::new: is_wikitext={} src={} dest={} page={}", + is_wikitext, + src.display(), + dest.display(), + page.display() + ); let page_name = page .file_name() - .unwrap_or_else(|| panic!("get filename from {}", page.display())) + .unwrap_or_else(|| OsStr::new("/")) .to_string_lossy() .to_string(); Self { @@ -94,25 +103,41 @@ impl NameBuilder { &self.destdir } - fn name(&self, path: &Path, ext: Option<&str>) -> Name { + pub fn page(&self, path: &Path) -> Name { assert!(path.starts_with(&self.srcdir)); let src = path.into(); let relative = make_path_relative_to(&self.srcdir, path); - let dest = join_subpath(&self.destdir, &relative); - let page = make_path_absolute(&relative); - if let Some(ext) = ext { - Name::new(true, src, dest.with_extension(ext), page.with_extension("")) + + let (dest, page) = if path.ends_with("index.mdwn") { + let dest = join_subpath(&self.destdir, &relative.with_extension("html")); + let dirname = relative + .parent() + .unwrap_or_else(|| panic!("expected parent directory for {}", relative.display())); + let page = make_path_absolute(dirname); + (dest, page) } else { - Name::new(false, src, dest, page) - } - } + let dirname = relative + .parent() + .unwrap_or_else(|| panic!("expected a parent for {}", relative.display())); + let basename = relative + .file_stem() + .unwrap_or_else(|| panic!("expected stem for {}", relative.display())); + let page_relative = dirname.join(basename); + let dest = join_subpath(&self.destdir, &page_relative.join("index.html")); + let page = make_path_absolute(&page_relative); + (dest, page) + }; - pub fn page(&self, path: &Path) -> Name { - self.name(path, Some("html")) + Name::new(true, src, dest, page) } pub fn file(&self, path: &Path) -> Name { - self.name(path, None) + assert!(path.starts_with(&self.srcdir)); + let src = path.into(); + let relative = make_path_relative_to(&self.srcdir, path); + let page = make_path_absolute(&relative); + let dest = join_subpath(&self.destdir, &relative); + Name::new(false, src, dest, page) } } @@ -164,12 +189,24 @@ mod test { fn builds_page_name() { let name = builder().page(Path::new("/src/foo/bar.mdwn")); assert_eq!(name.source_path(), Path::new("/src/foo/bar.mdwn")); - assert_eq!(name.destination_path(), Path::new("/dest/foo/bar.html")); + assert_eq!( + name.destination_path(), + Path::new("/dest/foo/bar/index.html") + ); assert_eq!(name.page_path(), Path::new("/foo/bar")); assert_eq!(name.page_name(), "bar"); } #[test] + fn builds_page_name_for_index_mdwn() { + let name = builder().page(Path::new("/src/foo/index.mdwn")); + assert_eq!(name.source_path(), Path::new("/src/foo/index.mdwn")); + assert_eq!(name.destination_path(), Path::new("/dest/foo/index.html")); + assert_eq!(name.page_path(), Path::new("/foo")); + assert_eq!(name.page_name(), "foo"); + } + + #[test] fn builds_file_name() { let name = builder().file(Path::new("/src/foo/bar.jpg")); assert_eq!(name.source_path(), Path::new("/src/foo/bar.jpg")); |