defmodule LSGWeb.IrcController do use LSGWeb, :controller def index(conn, _) do doc = LSG.IRC.TxtHandler.irc_doc() commands = for mod <- (Application.get_env(:lsg, :irc)[:plugins] ++ Application.get_env(:lsg, :irc)[:handlers]) do mod.irc_doc() end |> Enum.reject(fn(i) -> i == nil end) render conn, "index.html", commands: commands end def txt(conn, %{"name" => name}), do: do_txt(conn, name) def txt(conn, _), do: do_txt(conn, nil) defp do_txt(conn, nil) do doc = LSG.IRC.TxtHandler.irc_doc() data = data() lines = Enum.reduce(data, 0, fn({_, lines}, acc) -> acc + Enum.count(lines) end) render conn, "txts.html", data: data, doc: doc, files: Enum.count(data), lines: lines end defp do_txt(conn, txt) do data = data() if Map.has_key?(data, txt) do render(conn, "txt.html", name: txt, data: data[txt], doc: nil) else conn |> put_status(404) end end defp data() do dir = Application.get_env(:lsg, :data_path) <> "/irc.txt/" Path.wildcard(dir <> "/*.txt") |> Enum.reduce(%{}, fn(path, m) -> path = String.split(path, "/") file = List.last(path) [key, "txt"] = String.split(file, ".", parts: 2) data = dir <> file |> File.read! |> String.split("\n") |> Enum.reject(fn(line) -> cond do line == "" -> true !line -> true true -> false end end) Map.put(m, key, data) end) |> Enum.sort |> Enum.into(Map.new) end end