From 182a5b1e554f0cb6a15db62af17a1b41d1431ea1 Mon Sep 17 00:00:00 2001 From: SphericalKat Date: Tue, 17 Aug 2021 02:29:24 +0530 Subject: [PATCH] feat(pastes): decide highlighting rules using extension Signed-off-by: SphericalKat --- lib/ketbin_web/controllers/page_controller.ex | 11 +++++++---- lib/ketbin_web/controllers/user_auth.ex | 6 ++++-- lib/ketbin_web/templates/page/show.html.eex | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/ketbin_web/controllers/page_controller.ex b/lib/ketbin_web/controllers/page_controller.ex index 2b0544e..ed1b242 100644 --- a/lib/ketbin_web/controllers/page_controller.ex +++ b/lib/ketbin_web/controllers/page_controller.ex @@ -13,21 +13,24 @@ defmodule KetbinWeb.PageController do end def show(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do + [head | tail] = String.split(id, ".") + # fetch paste from db - paste = Pastes.get_paste!(id) + paste = Pastes.get_paste!(head) # paste is a url, redirect # regular paste, show content if paste.is_url do redirect(conn, external: paste.content) else - render(conn, "show.html", paste: paste, show_edit: show_edit) + render(conn, "show.html", paste: paste, show_edit: show_edit, extension: List.first(tail) || "") end end def showlink(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do - paste = Pastes.get_paste!(id) - render(conn, "show.html", paste: paste, show_edit: show_edit) + [head | tail] = String.split(id, ".") + paste = Pastes.get_paste!(head) + render(conn, "show.html", paste: paste, show_edit: show_edit, extension: List.first(tail) || "") end def raw(conn, %{"id" => id}) do diff --git a/lib/ketbin_web/controllers/user_auth.ex b/lib/ketbin_web/controllers/user_auth.ex index b103bc1..8a37115 100644 --- a/lib/ketbin_web/controllers/user_auth.ex +++ b/lib/ketbin_web/controllers/user_auth.ex @@ -97,12 +97,14 @@ defmodule KetbinWeb.UserAuth do end def owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do - paste = Pastes.get_paste!(id) + [head|_tail] = String.split(id, ".") + paste = Pastes.get_paste!(head) assign(conn, :show_edit, (user && user.id == paste.belongs_to) || false) end def ensure_owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do - paste = Pastes.get_paste!(id) + [head|_tail] = String.split(id, ".") + paste = Pastes.get_paste!(head) allow_edit = (user && user.id == paste.belongs_to) || false unless allow_edit do conn diff --git a/lib/ketbin_web/templates/page/show.html.eex b/lib/ketbin_web/templates/page/show.html.eex index 13e64de..5f0c5e6 100644 --- a/lib/ketbin_web/templates/page/show.html.eex +++ b/lib/ketbin_web/templates/page/show.html.eex @@ -8,5 +8,5 @@ <% end %> - <%= raw Ketbin.Utils.Syntax.highlight_text(@paste.content, "rs") %> + <%= raw Ketbin.Utils.Syntax.highlight_text(@paste.content, @extension) %>