Skip to content

Commit

Permalink
add deeply nested topics
Browse files Browse the repository at this point in the history
  • Loading branch information
zazaian committed Sep 17, 2019
1 parent 6a769af commit 336c8e3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/archivist/archive.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ defmodule Archivist.Archive do
end

def topics do
unquote topics
unquote Macro.escape(topics)
end

def tags do
Expand Down
17 changes: 12 additions & 5 deletions lib/archivist/parsers/article_parser.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
defmodule Archivist.ArticleParser do

alias Archivist.MapUtils

def get_paths(content_dir, pattern) do
content_dir
|> Path.relative_to_cwd
Expand Down Expand Up @@ -42,13 +45,17 @@ defmodule Archivist.ArticleParser do
def parse_topics(articles) do
articles
|> Stream.map(&Map.get(&1, :topics))
|> Stream.map(&nest_topics(&1))
|> Enum.to_list
|> Stream.map(&mapify_topics(&1))
|> Enum.reduce(%{}, fn topics_map, acc ->
MapUtils.deep_merge(acc, topics_map)
end)
end

defp nest_topics(topics_list) do
topics_list
defp mapify_topics(nested_topics) do
nested_topics
|> Enum.reverse
|> Enum.reduce([], fn topic, acc -> [topic | [acc]] end)
|> Enum.reduce(%{}, fn topic, acc ->
Map.put(%{}, topic, acc)
end)
end
end
6 changes: 3 additions & 3 deletions test/archivist/archive_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ defmodule ArchiveTest do
assert ArchiveMock.topics() == %{
"Fiction" => %{
"Sci-Fi" => %{
"Classic" => nil
"Classic" => %{}
},
},
"Films" => %{
"Sci-Fi" => %{
"Classic" => nil
"Classic" => %{}
},
"Action" => %{
"Crime" => nil
"Crime" => %{}
}
}
}
Expand Down

0 comments on commit 336c8e3

Please sign in to comment.