diff --git a/README.md b/README.md index 72484cb..b63bd08 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,10 @@ To build the site, you can use the following methods on the `Site` type: (relative to the output directory). This is useful when generating files using arbitrary logic, such as an Atom feed. - `page(pattern, index, func)`: finds Markdown files matching `pattern`, turning - them into HTML files by calling the `func` closure. + them into HTML files by calling the `func` closure. If `index` is set to + `true`, the output is `NAME/index.html` (e.g. `articles/foo/index.html`). If + `index` is `false`, the output is instead `NAME.html` (e.g. + `articles/foo.html`). For example: @@ -80,7 +83,7 @@ import wobsite.(Site, Page) class async Main { fn async main { Site.build fn (site) { - site.page('/index.md') fn { + site.page('/index.md', index: false) fn { recover fn (_, page: Page) { Result.Ok(page.to_html([])) } } } diff --git a/src/wobsite.inko b/src/wobsite.inko index 3cab484..48f81df 100644 --- a/src/wobsite.inko +++ b/src/wobsite.inko @@ -498,19 +498,27 @@ class pub Site { # typical setting, this closure converts the Markdown to HTML and wraps it in # a layout of sorts. # - # Input files are mapped to output files as follows: + # Input files are mapped to output files as follows when using `index: true`: # - # - `./source/index.md` results in `./public/index.html` - # - `./source/foo.md` results in `./public/foo/index.html` - # - `./source/foo/bar/index.md` results in `./public/foo/bar/index.html` - # - `./source/foo/bar.md` results in `./public/foo/bar/index.html` + # - `./source/index.md` becomes `./public/index.html` + # - `./source/foo.md` becomes `./public/foo/index.html` + # - `./source/foo/bar/index.md` becomes `./public/foo/bar/index.html` + # - `./source/foo/bar.md` becomes `./public/foo/bar/index.html` + # + # When using `index: false`, the mapping is instead as follows: + # + # - `./source/index.md` becomes `./public/index.html` + # - `./source/foo.md` becomes `./public/foo.html` + # - `./source/foo/bar/index.md` becomes `./public/foo/bar.html` + # - `./source/foo/bar.md` becomes `./public/foo/bar.html` fn pub mut page( pattern: String, + index: Bool, builder: fn -> uni fn (ref Files, Page) -> Result[html.Document, String], ) { @files.matching(pattern).each fn (path) { @pending += 1 - spawn.page(recover path.clone, builder.call) + spawn.page(recover path.clone, index, builder.call) } } @@ -593,11 +601,12 @@ class async Worker { fn async page( source: uni Path, + index: Bool, layout: uni fn (ref Files, Page) -> Result[html.Document, String], ) { let source = recover source let rel_source = source.strip_prefix(@files.source).unwrap - let target = if rel_source.tail == INDEX_FILE { + let target = if rel_source.tail == INDEX_FILE or index.false? { @files.output.join(rel_source.with_extension('html')) } else { @files.output.join(rel_source.with_extension('').join('index.html')) diff --git a/test/test_wobsite.inko b/test/test_wobsite.inko index 61995a2..546bfde 100644 --- a/test/test_wobsite.inko +++ b/test/test_wobsite.inko @@ -414,7 +414,7 @@ test' let site = Site.new(dir.path.clone, dir.path.clone).unwrap - site.page('*.md') fn { + site.page('*.md', index: true) fn { recover fn (_, page: Page) { Result.Ok(page.to_html([])) } } @@ -422,6 +422,30 @@ test' t.equal(read(dir.path.join('foo/index.html')), Result.Ok("

\ntest

")) } + t.test('Site.page with a regular Markdown file and index: false') fn (t) { + let dir = TempPath.directory(t.id) + let md = '\ +--- +{ + "title": "test", + "date": "2024-01-01T13:00:00Z" +} +--- + +test' + + write(dir.path.join('foo.md'), md) + + let site = Site.new(dir.path.clone, dir.path.clone).unwrap + + site.page('*.md', index: false) fn { + recover fn (_, page: Page) { Result.Ok(page.to_html([])) } + } + + t.true(site.wait.ok?) + t.equal(read(dir.path.join('foo.html')), Result.Ok("

\ntest

")) + } + t.test('Site.page with the index Markdown file') fn (t) { let dir = TempPath.directory(t.id) let md = '\ @@ -438,7 +462,31 @@ test' let site = Site.new(dir.path.clone, dir.path.clone).unwrap - site.page('*.md') fn { + site.page('*.md', index: true) fn { + recover fn (_, page: Page) { Result.Ok(page.to_html([])) } + } + + t.true(site.wait.ok?) + t.equal(read(dir.path.join('index.html')), Result.Ok("

\ntest

")) + } + + t.test('Site.page with the index Markdown file and index: false') fn (t) { + let dir = TempPath.directory(t.id) + let md = '\ +--- +{ + "title": "test", + "date": "2024-01-01T13:00:00Z" +} +--- + +test' + + write(dir.path.join('index.md'), md) + + let site = Site.new(dir.path.clone, dir.path.clone).unwrap + + site.page('*.md', index: false) fn { recover fn (_, page: Page) { Result.Ok(page.to_html([])) } } @@ -461,7 +509,7 @@ test' let site = Site.new(dir.path.clone, dir.path.clone).unwrap - site.page('*.md') fn { + site.page('*.md', index: true) fn { recover fn (_, page: Page) { Result.Ok(page.to_html([])) } } @@ -484,7 +532,7 @@ test' let site = Site.new(dir.path.clone, Path.new('/dev/null/invalid')).unwrap - site.page('*.md') fn { + site.page('*.md', index: true) fn { recover fn (_, page: Page) { Result.Ok(page.to_html([])) } }