Skip to content

Instantly share code, notes, and snippets.

@zookzook
Created March 7, 2020 15:09
Show Gist options
  • Select an option

  • Save zookzook/11251ea46ec6ef7880b7a96013298e82 to your computer and use it in GitHub Desktop.

Select an option

Save zookzook/11251ea46ec6ef7880b7a96013298e82 to your computer and use it in GitHub Desktop.
Page.ex
defmodule Polaris.Page do
@moduledoc false
alias Polaris.Page
@enforce_keys [:filename, :title, :content]
defstruct [:filename, :section, :title, :content]
def parse!(filename) do
# Get the last two path segments from the filename
[section, file] = filename |> Path.split() |> Enum.take(-2)
section = case section do
"hocon" -> nil
other -> other
end
case Hocon.decode(File.read!(filename)) do
{:ok, data} -> %Page{filename: file, section: section, title: data["title"], content: data}
{:error, msg} -> %Page{filename: file, section: section, title: "Error", content: %{msg: msg}}
end
end
def make_key(nil, filename) do
filename
end
def make_key(year, filename) do
year <> "/" <> filename
end
def make_key(%Page{filename: filename, section: nil}) do
filename
end
def make_key(%Page{filename: filename, section: section}) do
section <> "/" <> filename
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment