Allow configuring root path, parse from blog_url by default

This commit is contained in:
Mikko Ahlroth 2024-04-14 18:16:04 +03:00
parent f840c2c648
commit 992d000fac
5 changed files with 43 additions and 25 deletions

View file

@ -1,3 +1,4 @@
import gleam/uri
import gloss/config.{type Configuration, Compiling, Configuration, Rendering} import gloss/config.{type Configuration, Compiling, Configuration, Rendering}
import gloss/rendering/views/single_post import gloss/rendering/views/single_post
import gloss/rendering/views/base import gloss/rendering/views/base
@ -28,6 +29,8 @@ pub fn default_config(
author: config.Author, author: config.Author,
copyright: String, copyright: String,
) -> Configuration { ) -> Configuration {
let assert Ok(parsed_url) = uri.parse(blog_url)
Configuration( Configuration(
blog_name: blog_name, blog_name: blog_name,
blog_url: blog_url, blog_url: blog_url,
@ -44,7 +47,7 @@ pub fn default_config(
posts_per_page: 10, posts_per_page: 10,
posts_in_feed: 20, posts_in_feed: 20,
), ),
paths: paths.defaults, paths: paths.PathConfiguration(..paths.defaults, root: parsed_url.path),
parser: parser.default_parse, parser: parser.default_parse,
writer: writer.write, writer: writer.write,
output_path: "./output", output_path: "./output",

View file

@ -5,6 +5,8 @@ import gloss/models/page.{type Page}
import gloss/paths/post as post_paths import gloss/paths/post as post_paths
import gloss/utils/date.{type Month} import gloss/utils/date.{type Month}
pub const default_root = ""
pub const default_index = "/index" pub const default_index = "/index"
pub const default_feed_file = "/feed.xml" pub const default_feed_file = "/feed.xml"
@ -13,6 +15,7 @@ pub const default_feed = default_feed_file
pub type PathConfiguration { pub type PathConfiguration {
PathConfiguration( PathConfiguration(
root: String,
index: String, index: String,
single_post: fn(Post) -> String, single_post: fn(Post) -> String,
page: fn(Page) -> String, page: fn(Page) -> String,
@ -27,6 +30,7 @@ pub type PathConfiguration {
} }
pub const defaults = PathConfiguration( pub const defaults = PathConfiguration(
root: default_root,
index: default_index, index: default_index,
single_post: default_single_post, single_post: default_single_post,
page: default_page, page: default_page,

View file

@ -9,9 +9,7 @@ import lustre/element/html.{
a, body, footer, h1, head, header, html, li, link, main, meta, nav, p, section, a, body, footer, h1, head, header, html, li, link, main, meta, nav, p, section,
title, ul, title, ul,
} }
import lustre/attribute.{ import lustre/attribute.{attribute, href, id, name, rel, role, style, type_}
attribute, href, id, name, rel, role, style, type_, value,
}
import gloss/models/database.{type Database} import gloss/models/database.{type Database}
import gloss/models/menu.{type MenuItem} import gloss/models/menu.{type MenuItem}
import gloss/utils/ordered_tree import gloss/utils/ordered_tree
@ -60,16 +58,16 @@ fn view(
attribute("content", "width=device-width, initial-scale=1"), attribute("content", "width=device-width, initial-scale=1"),
]), ]),
title([], title_text), title([], title_text),
link([href("/css/normalize.css"), rel("stylesheet")]), link([href(config.paths.root <> "/css/normalize.css"), rel("stylesheet")]),
link([href("/css/magick.css"), rel("stylesheet")]), link([href(config.paths.root <> "/css/magick.css"), rel("stylesheet")]),
link([href("/css/custom.css"), rel("stylesheet")]), link([href(config.paths.root <> "/css/custom.css"), rel("stylesheet")]),
link([ link([
href(config.paths.feed), href(config.paths.root <> config.paths.feed),
rel("alternate"), rel("alternate"),
type_("application/atom+xml"), type_("application/atom+xml"),
]), ]),
case config.author.url { case config.author.url {
option.Some(url) -> link([rel("me"), value(url)]) option.Some(url) -> link([rel("me"), href(url)])
_ -> element.none() _ -> element.none()
}, },
..extra_meta ..extra_meta
@ -77,7 +75,7 @@ fn view(
body([], [ body([], [
header([id("title"), role("banner")], [ header([id("title"), role("banner")], [
h1([], [ h1([], [
a([href(config.paths.html(config.paths.index))], [ a([href(config.paths.html(config.paths.root <> config.paths.index))], [
text(config.blog_name), text(config.blog_name),
]), ]),
]), ]),
@ -147,7 +145,7 @@ fn tags(db: Database, config: Configuration) {
li([], [ li([], [
a( a(
[ [
href(config.paths.html(config.paths.tag(tag))), href(config.paths.html(config.paths.root <> config.paths.tag(tag))),
style([#("font-size", int.to_string(percentage) <> "%")]), style([#("font-size", int.to_string(percentage) <> "%")]),
], ],
[text(tag)], [text(tag)],
@ -168,12 +166,10 @@ fn archives(db: Database, config: Configuration) {
li([], [ li([], [
a( a(
[ [
href( href(config.paths.html(
config.paths.html(config.paths.list_page( config.paths.root
config.paths.year(year), <> config.paths.list_page(config.paths.year(year), 1),
1,
)), )),
),
], ],
[text(int.to_string(year))], [text(int.to_string(year))],
), ),
@ -185,12 +181,13 @@ fn archives(db: Database, config: Configuration) {
li([], [ li([], [
a( a(
[ [
href( href(config.paths.html(
config.paths.html(config.paths.list_page( config.paths.root
<> config.paths.list_page(
config.paths.month(year, month), config.paths.month(year, month),
1, 1,
)),
), ),
)),
], ],
[ [
text( text(

View file

@ -39,7 +39,11 @@ pub fn view(list_info: ListInfo, root_path: String, config: Configuration) {
li([], [ li([], [
a( a(
list.flatten([ list.flatten([
[href(config.paths.html(config.paths.list_page(root_path, page)))], [
href(config.paths.html(
config.paths.root <> config.paths.list_page(root_path, page),
)),
],
link_attributes, link_attributes,
]), ]),
[text(link_text)], [text(link_text)],
@ -73,7 +77,9 @@ fn nextprev_link(
li([], [ li([], [
a( a(
[ [
href(config.paths.html(config.paths.list_page(root_path, target))), href(config.paths.html(
config.paths.root <> config.paths.list_page(root_path, target),
)),
attribute("aria-label", label), attribute("aria-label", label),
], ],
[span([attribute("aria-hidden", "true")], [element.text(text)])], [span([attribute("aria-hidden", "true")], [element.text(text)])],

View file

@ -22,7 +22,8 @@ pub fn list_view(db: Database, config: Configuration) {
} }
fn view(post: CompiledPost, is_full: Bool, _db: Database, config: Configuration) { fn view(post: CompiledPost, is_full: Bool, _db: Database, config: Configuration) {
let post_url = config.paths.html(config.paths.single_post(post.orig)) let post_url =
config.paths.html(config.paths.root <> config.paths.single_post(post.orig))
let content = case post.content.short, is_full { let content = case post.content.short, is_full {
option.Some(content), False -> content option.Some(content), False -> content
@ -42,7 +43,14 @@ fn view(post: CompiledPost, is_full: Bool, _db: Database, config: Configuration)
[], [],
list.map(post.orig.tags, fn(tag) { list.map(post.orig.tags, fn(tag) {
li([], [ li([], [
a([href(config.paths.html(config.paths.tag(tag)))], [text(tag)]), a(
[
href(config.paths.html(
config.paths.root <> config.paths.tag(tag),
)),
],
[text(tag)],
),
]) ])
}), }),
), ),