Compare commits

..

No commits in common. "441bc58cf8c73bbe754a01f1792f0a0ecabe543c" and "0eadfcf6ccf18ccfb943526784346d472b16fc67" have entirely different histories.

2 changed files with 9 additions and 34 deletions

View file

@ -1,27 +1,15 @@
defmodule JOL.Blog.Parser do
@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()}
# Parses blog posts.
def parse(_path, content) do
%{"attrs" => attrs, "body" => body} =
Regex.named_captures(@zola_post_regex, content)
Regex.named_captures(~r/\+\+\+\n(?<attrs>.*)\n\+\+\+\n\n(?<body>.*)/s, content)
{:ok, toml_attrs} = Toml.decode(attrs)
parsed_attrs = %{
title: toml_attrs["title"],
draft: toml_attrs["draft"],
tags: toml_attrs["taxonomies"]["tags"],
date: toml_attrs["date"]
tags: toml_attrs["taxonomies"]["tags"]
}
parsed_body = String.trim(body)

View file

@ -16,29 +16,16 @@ defmodule JOL.Blog.ParserTest do
Body!
"""
{attrs, body} = Parser.parse("/filename/doesnt/matter", content)
{:ok, content: content, attrs: attrs, body: body}
{:ok, content: content}
end
test "parses the body from zola-style posts", post do
assert post.body == "Body!"
{_attrs, body} = Parser.parse("/fake/filename", post.content)
assert body == "Body!"
end
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
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"]}
end
end