webgen/lib/merger.ex

40 lines
1.2 KiB
Elixir

defmodule Merger do
def child_spec(opts) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]},
type: :supervisor,
restart: :temporary,
shutdown: 500
}
end
def start_link(opts) do
Supervisor.start_link(__MODULE__, :ok, opts)
end
def init(:ok) do
File.cd(Application.fetch_env!(:webgen, :root))
[merg | file] = String.split(File.read!("./gen-conf"), "\n")
Enum.each(file, fn i ->
if String.at(i, 0) != "#" do
docomp(i, merg)
end
end)
Supervisor.init([], [strategy: :one_for_one])
end
def docomp(file, merg) do
[title, content] = String.split(File.read!(file), "\n", parts: 2)
first = String.replace(String.replace(content, ~r/\n.*/s, "", global: false), ~r/<.*?>/, "")
file = String.replace(file, ~r/blogindex$/, "")
url = String.replace(String.replace(file, ~r/^index\//, ""), ~r/^inde?x$/, "");
File.write([file, ".html"], String.replace(
String.replace(
String.replace(
String.replace(merg, "$file", url),
"$first", first),
"$title", title),
"$content", String.replace(
String.replace(content, "\n\n", "</p><p>"), "\n", "<br/>"
)
))
end
end