Skip to content

Instantly share code, notes, and snippets.

@Anonyfox
Created October 19, 2015 10:45
Show Gist options
  • Save Anonyfox/038e17c298e08b6068a1 to your computer and use it in GitHub Desktop.
Save Anonyfox/038e17c298e08b6068a1 to your computer and use it in GitHub Desktop.
defmodule Microsites.Compile do
import File
def directory(folder \\ "elixir-lang.info") do
folder
|> reset_destination_folder
|> copy_assets
|> create_metadata
|> read_content_files
|> create_html_files
# |> IO.inspect
{:ok, folder}
end
defp reset_destination_folder(folder) do
rm_rf "./public/#{folder}"
mkdir "./public/#{folder}"
folder
end
defp copy_assets(folder) do
cp_r "./src/#{folder}/js", "./public/#{folder}/js"
cp_r "./src/#{folder}/css", "./public/#{folder}/css"
cp_r "./src/#{folder}/img", "./public/#{folder}/img"
folder
end
defp create_metadata(folder) do
%{
folder: folder,
author: "Maximilian Stroh",
domain_link: "http://#{folder}",
domain_name: humanize(folder),
js_files: ls!("./src/#{folder}/js"),
css_files: ls!("./src/#{folder}/css"),
content_files: ls!("./src/#{folder}/sites")
}
end
defp read_content_files(data) do
path = "./src/#{data.folder}/sites"
sites = Parallel.map data.content_files, fn file ->
%{
site_name: humanize(file),
site_link: String.replace(file, ".md", ".html"),
site_title: "#{humanize(file)} | #{data.domain_name}",
site_description: "desc bla bla",
site_html: Earmark.to_html(read!("#{path}/#{file}"))
}
end
Dict.put data, :sites, sites
end
defp create_html_files(data) do
Parallel.map data.sites, fn site ->
assigns = Dict.merge(site, data)
template = "./src/#{data.folder}/template.html.eex"
file = EEx.eval_file template, assigns: assigns
write "./public/#{data.folder}/#{site.site_link}", file
end
data
end
defp humanize(file) do
file
|> String.split(".")
|> List.first
|> String.replace("-"," ")
|> String.replace("_"," ")
|> String.split(" ")
|> Enum.map_join(" ", &String.capitalize/1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment