From 2d83df8b32bff7f0028923bb5b64dc0b55f20d03 Mon Sep 17 00:00:00 2001 From: Jordan Bracco Date: Tue, 20 Dec 2022 00:21:54 +0000 Subject: Nola rename: The Big Move, Refs T77 --- lib/nola_web/views/alcoolog_view.ex | 6 +++ lib/nola_web/views/error_helpers.ex | 40 ++++++++++++++++++ lib/nola_web/views/error_view.ex | 17 ++++++++ lib/nola_web/views/irc_view.ex | 3 ++ lib/nola_web/views/layout_view.ex | 81 +++++++++++++++++++++++++++++++++++++ lib/nola_web/views/network_view.ex | 4 ++ lib/nola_web/views/open_id_view.ex | 4 ++ lib/nola_web/views/page_view.ex | 3 ++ 8 files changed, 158 insertions(+) create mode 100644 lib/nola_web/views/alcoolog_view.ex create mode 100644 lib/nola_web/views/error_helpers.ex create mode 100644 lib/nola_web/views/error_view.ex create mode 100644 lib/nola_web/views/irc_view.ex create mode 100644 lib/nola_web/views/layout_view.ex create mode 100644 lib/nola_web/views/network_view.ex create mode 100644 lib/nola_web/views/open_id_view.ex create mode 100644 lib/nola_web/views/page_view.ex (limited to 'lib/nola_web/views') diff --git a/lib/nola_web/views/alcoolog_view.ex b/lib/nola_web/views/alcoolog_view.ex new file mode 100644 index 0000000..ad52472 --- /dev/null +++ b/lib/nola_web/views/alcoolog_view.ex @@ -0,0 +1,6 @@ +defmodule NolaWeb.AlcoologView do + use NolaWeb, :view + require Integer + +end + diff --git a/lib/nola_web/views/error_helpers.ex b/lib/nola_web/views/error_helpers.ex new file mode 100644 index 0000000..25214bd --- /dev/null +++ b/lib/nola_web/views/error_helpers.ex @@ -0,0 +1,40 @@ +defmodule NolaWeb.ErrorHelpers do + @moduledoc """ + Conveniences for translating and building error messages. + """ + + use Phoenix.HTML + + @doc """ + Generates tag for inlined form input errors. + """ + def error_tag(form, field) do + Enum.map(Keyword.get_values(form.errors, field), fn (error) -> + content_tag :span, translate_error(error), class: "help-block" + end) + end + + @doc """ + Translates an error message using gettext. + """ + def translate_error({msg, opts}) do + # Because error messages were defined within Ecto, we must + # call the Gettext module passing our Gettext backend. We + # also use the "errors" domain as translations are placed + # in the errors.po file. + # Ecto will pass the :count keyword if the error message is + # meant to be pluralized. + # On your own code and templates, depending on whether you + # need the message to be pluralized or not, this could be + # written simply as: + # + # dngettext "errors", "1 file", "%{count} files", count + # dgettext "errors", "is invalid" + # + if count = opts[:count] do + Gettext.dngettext(NolaWeb.Gettext, "errors", msg, msg, count, opts) + else + Gettext.dgettext(NolaWeb.Gettext, "errors", msg, opts) + end + end +end diff --git a/lib/nola_web/views/error_view.ex b/lib/nola_web/views/error_view.ex new file mode 100644 index 0000000..5cad939 --- /dev/null +++ b/lib/nola_web/views/error_view.ex @@ -0,0 +1,17 @@ +defmodule NolaWeb.ErrorView do + use NolaWeb, :view + + def render("404.html", _assigns) do + "Page not found" + end + + def render("500.html", _assigns) do + "Internal server error" + end + + # In case no render clause matches or no + # template is found, let's render it as 500 + def template_not_found(_template, assigns) do + render "500.html", assigns + end +end diff --git a/lib/nola_web/views/irc_view.ex b/lib/nola_web/views/irc_view.ex new file mode 100644 index 0000000..331d91f --- /dev/null +++ b/lib/nola_web/views/irc_view.ex @@ -0,0 +1,3 @@ +defmodule NolaWeb.IrcView do + use NolaWeb, :view +end diff --git a/lib/nola_web/views/layout_view.ex b/lib/nola_web/views/layout_view.ex new file mode 100644 index 0000000..2bffc6f --- /dev/null +++ b/lib/nola_web/views/layout_view.ex @@ -0,0 +1,81 @@ +defmodule NolaWeb.LayoutView do + use NolaWeb, :view + + def liquid_markdown(conn, text) do + context_path = cond do + conn.assigns[:chan] -> "/#{conn.assigns[:network]}/#{NolaWeb.format_chan(conn.assigns[:chan])}" + conn.assigns[:network] -> "/#{conn.assigns[:network]}/-" + true -> "/-" + end + + {:ok, ast} = Liquex.parse(text) + context = Liquex.Context.new(%{ + "context_path" => context_path + }) + {content, _} = Liquex.render(ast, context) + content + |> to_string() + |> Earmark.as_html!() + |> raw() + end + + def page_title(conn) do + target = cond do + conn.assigns[:chan] -> + "#{conn.assigns.chan} @ #{conn.assigns.network}" + conn.assigns[:network] -> conn.assigns.network + true -> Keyword.get(Nola.name()) + end + + breadcrumb_title = Enum.map(Map.get(conn.assigns, :breadcrumbs)||[], fn({title, _href}) -> title end) + + title = [conn.assigns[:title], breadcrumb_title, target] + |> List.flatten() + |> Enum.uniq() + |> Enum.filter(fn(x) -> x end) + |> Enum.intersperse(" / ") + |> Enum.join() + + content_tag(:title, title) + end + + def format_time(date, with_relative \\ true) do + alias Timex.Format.DateTime.Formatters + alias Timex.Timezone + date = if is_integer(date) do + date + |> DateTime.from_unix!(:millisecond) + |> DateTime.shift_zone!("Europe/Paris", Tzdata.TimeZoneDatabase) + else + date + |> DateTime.shift_zone!("Europe/Paris", Tzdata.TimeZoneDatabase) + end + + now = DateTime.now!("Europe/Paris", Tzdata.TimeZoneDatabase) + + now_week = Timex.iso_week(now) + date_week = Timex.iso_week(date) + + {y, w} = now_week + now_last_week = {y, w-1} + now_last_roll = 7-Timex.days_to_beginning_of_week(now) + + date_date = DateTime.to_date(date) + now_date = DateTime.to_date(date) + + format = cond do + date.year != now.year -> "{D}/{M}/{YYYY} {h24}:{m}" + date_date == now_date -> "{h24}:{m}" + (now_week == date_week) || (date_week == now_last_week && (Date.day_of_week(date) >= now_last_roll)) -> "{WDfull} {h24}:{m}" + (now.year == date.year && now.month == date.month) -> "{WDfull} {D} {h24}:{m}" + true -> "{WDfull} {D} {M} {h24}:{m}" + end + + {:ok, relative} = Formatters.Relative.relative_to(date, Timex.now("Europe/Paris"), "{relative}", "fr") + {:ok, full} = Formatters.Default.lformat(date, "{WDfull} {D} {YYYY} {h24}:{m}", "fr") #"{h24}:{m} {WDfull} {D}", "fr") + {:ok, detail} = Formatters.Default.lformat(date, format, "fr") #"{h24}:{m} {WDfull} {D}", "fr") + + content_tag(:time, if(with_relative, do: relative, else: detail), [title: full]) + end + +end diff --git a/lib/nola_web/views/network_view.ex b/lib/nola_web/views/network_view.ex new file mode 100644 index 0000000..7a24db1 --- /dev/null +++ b/lib/nola_web/views/network_view.ex @@ -0,0 +1,4 @@ +defmodule NolaWeb.NetworkView do + use NolaWeb, :view + +end diff --git a/lib/nola_web/views/open_id_view.ex b/lib/nola_web/views/open_id_view.ex new file mode 100644 index 0000000..bd8089b --- /dev/null +++ b/lib/nola_web/views/open_id_view.ex @@ -0,0 +1,4 @@ +defmodule NolaWeb.OpenIdView do + use NolaWeb, :view + +end diff --git a/lib/nola_web/views/page_view.ex b/lib/nola_web/views/page_view.ex new file mode 100644 index 0000000..1bfaadd --- /dev/null +++ b/lib/nola_web/views/page_view.ex @@ -0,0 +1,3 @@ +defmodule NolaWeb.PageView do + use NolaWeb, :view +end -- cgit v1.2.3