JOL/lib/jol/blog/parser.ex

36 lines
936 B
Elixir
Raw Permalink Normal View History

defmodule JOL.Blog.Parser do
@zola_post_regex ~r/\+\+\+\n(?<attrs>.*)\n\+\+\+\n\n(?<body>.*)/s
2024-05-23 20:46:43 +00:00
@doc """
2024-05-23 20:53:16 +00:00
Psrses a blog post.
Right now it only supports Zola style posts. I didn't have a ton of them
written in that format but writing the parser was fun.
2024-05-23 20:46:43 +00:00
"""
@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(@zola_post_regex, content)
{:ok, toml_attrs} = Toml.decode(attrs)
parsed_attrs = %{
title: toml_attrs["title"],
draft: toml_attrs["draft"],
2024-05-23 20:48:53 +00:00
tags: toml_attrs["taxonomies"]["tags"],
date: toml_attrs["date"],
slug: toml_attrs["slug"]
}
parsed_body = String.trim(body)
{parsed_attrs, parsed_body}
end
end