74 lines
1.4 KiB
Elixir
74 lines
1.4 KiB
Elixir
defmodule Forum.Util do
|
|
@moduledoc """
|
|
General utilities for the Forum
|
|
"""
|
|
|
|
import Plug.Conn
|
|
|
|
@doc """
|
|
Converts Markdown to HTML
|
|
"""
|
|
def md(text) do
|
|
text
|
|
|> Earmark.as_html!()
|
|
|> String.replace("<img", "<img class=\"img-responsive\"")
|
|
|> Phoenix.HTML.raw()
|
|
end
|
|
|
|
@doc """
|
|
Gets the logged-in username for a conn
|
|
"""
|
|
def current_username(conn) do
|
|
get_session(conn, :current_user)
|
|
end
|
|
|
|
@doc """
|
|
Can current user sudo?
|
|
"""
|
|
def sudoer?(conn) do
|
|
get_session(conn, :sudoer)
|
|
end
|
|
|
|
@doc """
|
|
|
|
"""
|
|
def logged_in?(conn) do
|
|
get_session(conn, :loggedin)
|
|
end
|
|
|
|
@doc """
|
|
Returns true if the current user matches the user -- or the current user can sudo
|
|
"""
|
|
def can_edit?(conn, user) do
|
|
user.name == current_username(conn) or sudoer?(conn)
|
|
end
|
|
|
|
@doc """
|
|
Make a time pretty for displaying it
|
|
"""
|
|
def pp_time(time) do
|
|
time
|
|
|> Timex.local()
|
|
|> Timex.Format.DateTime.Formatter.format!("%a %b %e, %Y ~ %I:%M %P", :strftime)
|
|
|> String.downcase()
|
|
end
|
|
|
|
@doc """
|
|
Set custom options for kerosene
|
|
* lowercase text
|
|
* hide for one page
|
|
"""
|
|
def paginate(conn, kerosene) do
|
|
if kerosene.total_pages > 1 do
|
|
Kerosene.HTML.paginate(
|
|
conn,
|
|
kerosene,
|
|
next_label: ">>",
|
|
previous_label: "<<",
|
|
first_label: "first",
|
|
last_label: "last"
|
|
)
|
|
end
|
|
end
|
|
end
|