Newer
Older
//// Path configuration controls where files are generated and how links are
//// formed.
import scriptorium/models/page.{type Page}
import scriptorium/models/post.{type Post}
import scriptorium/utils/date.{type Month}
import scriptorium/utils/ints/day
pub const default_index = "/index"
/// The default path where the feed is accessible when hosted.
pub const default_feed = default_feed_file
/// The path configuration controls where files will be located and where links
/// will point to.
pub type PathConfiguration {
PathConfiguration(
/// The root path where the blog will be accessible. With starting slash but
/// without trailing slash, e.g. `/scriptorium_blog`. Note that if the blog is
/// accessible without a subpath, this value should be `""`.
root: String,
/// The index path. Note that the first page of the index is always written
/// into `"/"`, meaning `index.html`. This path is used for the rest of the
/// pages, e.g. `"/wibble"` would result in `/wibble/2.html`,
/// `/wibble/3.html` and so on.
/// List page path: given the original path such as `/tag/wibble` as a
/// string and the page number, forms the final path.
list_page: fn(String, Int) -> String,
/// HTML path: Append (or don't) the `.html` extension to the given path.
/// If you are using fancy URLs without extensions, override this to do
/// nothing.
/// Path and file name of the feed file that will be written, _without_ the
/// file extension! E.g. `/feed` will create a file `/feed.xml`.
pub const defaults = PathConfiguration(
root: default_root,
index: default_index,
single_post: default_single_post,
page: default_page,
tag: default_tag,
year: default_year_archive,
month: default_month_archive,
list_page: default_list_page,
html: default_html,
feed: default_feed,
feed_file: default_feed_file,
pub fn default_single_post(post: Post) {
let post_date = post.get_date(post)
let date_parts =
list.map(
[
post_date.year,
date.month_to_int(post_date.month),
day.to_int(post_date.day),
],
pad_int,
)
"/" <> string.join(date_parts, "/") <> "/" <> post.slug
pub fn default_page(page: Page) {
"/" <> page.slug
}
pub fn default_year_archive(year: Int) {
pub fn default_month_archive(year: Int, month: Month) {
default_year_archive(year)
<> string.pad_left(int.to_string(date.month_to_int(month)), 2, "0")
}
/// Get the given list path with a page number.
///
/// The first page does not get any appended page number.
pub fn default_list_page(path: String, page: Int) {
pub fn default_html(path: String) {
fn pad_int(number: Int) -> String {
number
|> int.to_string()
|> string.pad_left(to: 2, with: "0")
}