NOT PERFECT, BUT where I left off today

This commit is contained in:
Ryan Pandya
2022-09-06 19:06:20 -07:00
parent 841d0ab16e
commit ec6be15032
6 changed files with 82 additions and 22 deletions
@@ -17,11 +17,8 @@ defmodule LogsrvApi.Filesystem do
def dir(subdir) do
"#{dir()}/#{subdir}/"
end
def locate(Page, fd) do
dir(:pages) <> fd
end
def locate(Journal, fd) do
dir(:journals) <> fd
def locate(fd, module) do
dir(module.atom) <> fd
end
def all(Page) do
+62 -13
View File
@@ -1,23 +1,72 @@
defmodule LogsrvApi.Page do
alias LogsrvApi.{Filesystem,Page,Journal}
defstruct slug: "", title: "", date: "", content: "", filename: "", tags: [:fun]
def atom, do: :pages
def init(fd) do
title = fd
page = %Page{
filename: fd,
title: fd |> to_title,
slug: fd |> to_slug,
date: fd |> to_path |> date_modified
}
fd |> to_path |> File.read! |> split |> extract(page)
end
def to_title(fd) do
fd
|> String.replace(~r/_/," ")
|> String.replace(~r/\.md$/,"")
|> String.replace("%2F","/")
%{mtime: mtime} = (Page |> Filesystem.locate(fd) |> File.stat!)
date_modified = mtime |> NaiveDateTime.from_erl!() |> DateTime.from_naive!("Etc/UTC")
tags = [:fun]
%{
type: Page,
title: title |> String.capitalize(),
filename: fd,
date: date_modified,
tags: tags
}
|> String.capitalize
end
def to_slug(fd) do
fd
|> String.replace(~r/_/,"-")
|> String.replace(~r/\.md$/,"")
|> String.replace("%2F","_")
end
def to_path(fd) do
fd
|> Filesystem.locate(Page)
end
def date_modified(path) do
%{ctime: ctime} = (path |> File.stat!)
ctime
|> NaiveDateTime.from_erl!()
|> DateTime.from_naive!("Etc/UTC")
end
defp split(data) do
try do
[frontmatter, markdown] = String.split(data, ~r/\n-{3,}\n/, parts: 2)
{parse_yaml(frontmatter), Earmark.as_html!(markdown)}
rescue MatchError ->
{:nil, Earmark.as_html!(data)}
end
end
defp parse_yaml(yaml) do
[parsed] = :yamerl_constr.string(yaml)
parsed
end
defp extract({props, content}, post) do
%{post |
title: get_prop(props, "title") || post.title,
tags: get_prop(props, "tags"),
content: content}
end
defp get_prop(props, key) do
if is_nil(props) do
:nil
else
case :proplists.get_value(String.to_char_list(key), props) do
:undefined -> nil
x -> to_string(x)
end
end
end
end
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -24,7 +24,7 @@ defmodule LogsrvWeb.MixProject do
def application do
[
mod: {LogsrvWeb.Application, []},
extra_applications: [:logger, :runtime_tools]
extra_applications: [:logger, :runtime_tools, :timex, :yamerl]
]
end