summaryrefslogtreecommitdiff
path: root/lib/web/views/layout_view.ex
blob: 747740fc047b3331e30137fa5fb0829966e979d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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 ->
          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")

    # "{h24}:{m} {WDfull} {D}", "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")

    content_tag(:time, if(with_relative, do: relative, else: detail), title: full)
  end
end