Skip to content

Commit

Permalink
add article sorter as anonymous function
Browse files Browse the repository at this point in the history
  • Loading branch information
zazaian committed Sep 14, 2019
1 parent 3f61e58 commit 53714ee
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/archivist/archive.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ defmodule Archivist.Archive do
@defaults [
content_dir: "priv/articles",
match_pattern: "**/*.ad",
article_sorter: &(&1[:published_at] >= &2[:published_at]),
content_parser: Earmark,
article_parser: Arcdown
]
Expand All @@ -55,6 +56,7 @@ defmodule Archivist.Archive do

content_dir = settings[:content_dir]
match_pattern = settings[:match_pattern]
article_sorter = settings[:article_sorter]

article_paths = Parser.get_paths(content_dir, match_pattern)
parsed_articles = Parser.parse_files(article_paths)
Expand All @@ -75,7 +77,7 @@ defmodule Archivist.Archive do

def articles do
unquote valid_articles
|> Enum.to_list
|> Enum.sort(article_sorter)
|> Macro.escape
end

Expand Down
6 changes: 3 additions & 3 deletions lib/archivist/parsers/article_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ defmodule Archivist.ArticleParser do
def parse_attrs(attr, articles) do
articles
|> Stream.flat_map(fn article -> Map.get(article, attr) end)
|> sanitize_parsed
|> sanitize_attrs
end

def parse_attr(attr, articles) do
articles
|> Stream.map(fn article -> Map.get(article, attr) end)
|> sanitize_parsed
|> sanitize_attrs
end

defp sanitize_parsed(parsed_vals) do
defp sanitize_attrs(parsed_vals) do
parsed_vals
|> Stream.reject(&is_nil/1)
|> Stream.uniq
Expand Down

0 comments on commit 53714ee

Please sign in to comment.