Compare commits

..

4 commits

2 changed files with 34 additions and 9 deletions

View file

@ -1,15 +1,27 @@
defmodule JOL.Blog.Parser do
# Parses blog posts.
@zola_post_regex ~r/\+\+\+\n(?<attrs>.*)\n\+\+\+\n\n(?<body>.*)/s
@doc """
Psrses a Zola-style blogpost.
"""
@spec parse(String.t(), String.t()) ::
{%{
date: DateTime.t(),
draft: boolean(),
tags: [String.t()],
title: String.t()
}, String.t()}
def parse(_path, content) do
%{"attrs" => attrs, "body" => body} =
Regex.named_captures(~r/\+\+\+\n(?<attrs>.*)\n\+\+\+\n\n(?<body>.*)/s, content)
Regex.named_captures(@zola_post_regex, content)
{:ok, toml_attrs} = Toml.decode(attrs)
parsed_attrs = %{
title: toml_attrs["title"],
draft: toml_attrs["draft"],
tags: toml_attrs["taxonomies"]["tags"]
tags: toml_attrs["taxonomies"]["tags"],
date: toml_attrs["date"]
}
parsed_body = String.trim(body)

View file

@ -16,16 +16,29 @@ defmodule JOL.Blog.ParserTest do
Body!
"""
{:ok, content: content}
{attrs, body} = Parser.parse("/filename/doesnt/matter", content)
{:ok, content: content, attrs: attrs, body: body}
end
test "parses the body from zola-style posts", post do
{_attrs, body} = Parser.parse("/fake/filename", post.content)
assert body == "Body!"
assert post.body == "Body!"
end
test "parses the attrs from zola-style posts", post do
{attrs, _body} = Parser.parse("filepath", post.content)
assert attrs == %{title: "test post", draft: false, tags: ["howto", "hardware"]}
test "parses the title from zola-style posts", post do
assert post.attrs.title == "test post"
end
test "parses the tags from zola-style posts", post do
assert post.attrs.tags == ["howto", "hardware"]
end
test "parses the draft status from zola-style posts", post do
assert post.attrs.draft == false
end
test "parses the publish date from zola-style posts", post do
{:ok, known_date, _} = DateTime.from_iso8601("2024-01-02 14:00:00-05:00")
assert Date.compare(known_date, post.attrs.date) == :eq
end
end