summaryrefslogtreecommitdiff
path: root/lib/lsg_web/views/layout_view.ex
blob: b28d3c5e03467777974d5e42aa33c7c560d27428 (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
defmodule LSGWeb.LayoutView do
  use LSGWeb, :view

  def liquid_markdown(conn, text) do
    context_path = cond do
      conn.assigns[:chan] -> "/#{conn.assigns[:network]}/#{LSGWeb.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(Application.get_env(:lsg, :irc), :name, "ircbot")
    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)

    format = cond do
      date.year != now.year -> "{D}/{M}/{YYYY} {h24}:{m}"
      date.day == now.day -> "{h24}:{m}"
      (now_week == date_week) || (date_week == now_last_week && (Date.day_of_week(date) >= now_last_roll)) -> "{WDfull} {h24}:{m}"
      (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