36 lines
936 B
Elixir
36 lines
936 B
Elixir
defmodule JOL.Blog.Parser do
|
|
@zola_post_regex ~r/\+\+\+\n(?<attrs>.*)\n\+\+\+\n\n(?<body>.*)/s
|
|
|
|
@doc """
|
|
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.
|
|
"""
|
|
@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"],
|
|
tags: toml_attrs["taxonomies"]["tags"],
|
|
date: toml_attrs["date"],
|
|
slug: toml_attrs["slug"]
|
|
}
|
|
|
|
parsed_body = String.trim(body)
|
|
|
|
{parsed_attrs, parsed_body}
|
|
end
|
|
end
|