From d14408c7e054619f2648eb0480fe55f60397e8c2 Mon Sep 17 00:00:00 2001 From: href Date: Thu, 2 Sep 2021 07:05:42 +0200 Subject: txt: don't search in files with dot, display them below in web --- lib/lsg_irc/txt_plugin.ex | 10 ++++++--- lib/lsg_web/controllers/irc_controller.ex | 34 ++++++++++++++++--------------- lib/lsg_web/templates/irc/txts.html.eex | 29 +++++++++++++++++++++++--- 3 files changed, 51 insertions(+), 22 deletions(-) (limited to 'lib') diff --git a/lib/lsg_irc/txt_plugin.ex b/lib/lsg_irc/txt_plugin.ex index 93c4bb1..2c9dfca 100644 --- a/lib/lsg_irc/txt_plugin.ex +++ b/lib/lsg_irc/txt_plugin.ex @@ -168,9 +168,13 @@ defmodule LSG.IRC.TxtPlugin do result = with_stateful_results(msg, {:bang,"txt",msg.network,msg.channel,grep}, fn() -> Enum.reduce(state.triggers, [], fn({trigger, data}, acc) -> - Enum.reduce(data, acc, fn({l, _}, acc) -> - [{trigger, l} | acc] - end) + if !String.contains?(trigger, ".") do + Enum.reduce(data, acc, fn({l, _}, acc) -> + [{trigger, l} | acc] + end) + else + acc + end end) |> Enum.filter(fn({_, line}) -> line diff --git a/lib/lsg_web/controllers/irc_controller.ex b/lib/lsg_web/controllers/irc_controller.ex index 317bb27..9ddeefc 100644 --- a/lib/lsg_web/controllers/irc_controller.ex +++ b/lib/lsg_web/controllers/irc_controller.ex @@ -19,19 +19,19 @@ defmodule LSGWeb.IrcController do end def txt(conn, %{"name" => name}) do - case String.split(name, ".", parts: 2) do - [name, "txt"] -> - data = data() - if Map.has_key?(data, name) do - lines = Enum.join(data[name], "\n") - text(conn, lines) - else - conn - |> put_status(404) - |> text("Not found") - end - _ -> - do_txt(conn, name) + if String.contains?(name, ".txt") do + name = String.replace(name, ".txt", "") + data = data() + if Map.has_key?(data, name) do + lines = Enum.join(data[name], "\n") + text(conn, lines) + else + conn + |> put_status(404) + |> text("Not found") + end + else + do_txt(conn, name) end end def txt(conn, _), do: do_txt(conn, nil) @@ -40,10 +40,12 @@ defmodule LSGWeb.IrcController do defp do_txt(conn, nil) do doc = LSG.IRC.TxtPlugin.irc_doc() data = data() - lines = Enum.reduce(data, 0, fn({_, lines}, acc) -> acc + Enum.count(lines) end) + main = Enum.filter(data, fn({trigger, _}) -> !String.contains?(trigger, ".") end) |> Enum.into(Map.new) + system = Enum.filter(data, fn({trigger, _}) -> String.contains?(trigger, ".") end) |> Enum.into(Map.new) + lines = Enum.reduce(main, 0, fn({_, lines}, acc) -> acc + Enum.count(lines) end) conn |> assign(:title, "txt") - |> render("txts.html", data: data, doc: doc, files: Enum.count(data), lines: lines) + |> render("txts.html", data: main, doc: doc, files: Enum.count(main), lines: lines, system: system) end defp do_txt(conn, txt) do @@ -75,7 +77,7 @@ defmodule LSGWeb.IrcController do |> Enum.reduce(%{}, fn(path, m) -> path = String.split(path, "/") file = List.last(path) - [key, "txt"] = String.split(file, ".", parts: 2) + key = String.replace(file, ".txt", "") data = dir <> file |> File.read! |> String.split("\n") diff --git a/lib/lsg_web/templates/irc/txts.html.eex b/lib/lsg_web/templates/irc/txts.html.eex index 66321b5..68d35f5 100644 --- a/lib/lsg_web/templates/irc/txts.html.eex +++ b/lib/lsg_web/templates/irc/txts.html.eex @@ -1,8 +1,8 @@

<%= @lines %> lignes dans <%= @files %> fichiers - — - Aide + Aide + Fichiers système

-
<%= LSGWeb.LayoutView.liquid_markdown(@conn, @doc) %>
+
<%= LSGWeb.LayoutView.liquid_markdown(@conn, @doc) %>
+
+

+ Fichiers système +

+ + + +
-- cgit v1.2.3