summaryrefslogblamecommitdiff
path: root/lib/lsg_web/views/layout_view.ex
blob: 41c53414dcec25b6d09ca096d683696520305177 (plain) (tree)
1
2

                              






































                                                                                                         





                                                 
                                                                      

          











                                                                      


                                      

                                                         
                                          
                                                                                                                                  
                                                                                    
                                          
       
 
                                                                                                          

                                                                                                                          
 
                                                                                    

     
   
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)

    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