Created
March 7, 2020 15:09
-
-
Save zookzook/11251ea46ec6ef7880b7a96013298e82 to your computer and use it in GitHub Desktop.
Page.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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