stunwin dot com
stunwin.com
1import gleam/dict
2import gleam/int
3import gleam/list
4import gleam/result
5import lustre/ssg/djot
6import simplifile
7import tom
8
9pub type Post {
10 Post(
11 title: String,
12 slug: String,
13 image: String,
14 preview: String,
15 content: String,
16 tags: List(String),
17 time: Int,
18 )
19}
20
21pub fn all() -> List(#(String, Post)) {
22 let assert Ok(postdir) = simplifile.get_files("data/posts/")
23
24 postdir
25 |> list.map(build_post)
26}
27
28pub fn build_post(path: String) -> #(String, Post) {
29 echo path
30 let assert Ok(file) = simplifile.read(path)
31 let assert Ok(toml) = djot.metadata(file)
32
33 let meta_fields =
34 ["title", "slug", "image", "preview", "time"]
35 |> list.map(fn(field) {
36 echo field
37 let assert Ok(val) = tom.get_string(toml, [field])
38 #(field, val)
39 })
40
41 let assert Ok(tags) = tom.get_array(toml, ["tags"])
42 let tags =
43 list.map(tags, fn(x) {
44 let assert Ok(str) = tom.as_string(x)
45 str
46 })
47
48 let content = djot.content(file)
49
50 let p =
51 Post(
52 // TODO: unwrap hell.
53 title: list.key_find(meta_fields, "title") |> result.unwrap("title error"),
54 slug: list.key_find(meta_fields, "slug") |> result.unwrap("slug error"),
55 image: list.key_find(meta_fields, "image") |> result.unwrap("image error"),
56 preview: list.key_find(meta_fields, "preview")
57 |> result.unwrap("preview error"),
58 content: content,
59 tags: tags,
60 time: list.key_find(meta_fields, "time")
61 |> result.unwrap("time error")
62 |> int.parse
63 |> result.unwrap(000),
64 )
65
66 #(p.slug, p)
67}
68
69pub fn filtered_posts(
70 post_list: dict.Dict(String, Post),
71 target_tag: String,
72) -> dict.Dict(String, Post) {
73 post_list
74 |> dict.filter(fn(_k, v) { list.contains(v.tags, target_tag) })
75}