diff --git a/lib/philomena_web/controllers/admin/dnp_entry_controller.ex b/lib/philomena_web/controllers/admin/dnp_entry_controller.ex index dd8c06b4..359454dd 100644 --- a/lib/philomena_web/controllers/admin/dnp_entry_controller.ex +++ b/lib/philomena_web/controllers/admin/dnp_entry_controller.ex @@ -1,7 +1,7 @@ defmodule PhilomenaWeb.Admin.DnpEntryController do use PhilomenaWeb, :controller - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.DnpEntries.DnpEntry alias Philomena.Repo import Ecto.Query @@ -45,7 +45,7 @@ defmodule PhilomenaWeb.Admin.DnpEntryController do bodies = dnp_entries |> Enum.map(&%{body: &1.conditions}) - |> TextRenderer.render_collection(conn) + |> MarkdownRenderer.render_collection(conn) dnp_entries = %{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)} diff --git a/lib/philomena_web/controllers/admin/mod_note_controller.ex b/lib/philomena_web/controllers/admin/mod_note_controller.ex index 275bd7e0..23f71815 100644 --- a/lib/philomena_web/controllers/admin/mod_note_controller.ex +++ b/lib/philomena_web/controllers/admin/mod_note_controller.ex @@ -1,7 +1,7 @@ defmodule PhilomenaWeb.Admin.ModNoteController do use PhilomenaWeb, :controller - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.ModNotes.ModNote alias Philomena.Polymorphic alias Philomena.ModNotes @@ -29,7 +29,7 @@ defmodule PhilomenaWeb.Admin.ModNoteController do |> order_by(desc: :id) |> Repo.paginate(conn.assigns.scrivener) - bodies = TextRenderer.render_collection(mod_notes, conn) + bodies = MarkdownRenderer.render_collection(mod_notes, conn) preloaded = Polymorphic.load_polymorphic(mod_notes, notable: [notable_id: :notable_type]) mod_notes = %{mod_notes | entries: Enum.zip(bodies, preloaded)} diff --git a/lib/philomena_web/controllers/admin/report_controller.ex b/lib/philomena_web/controllers/admin/report_controller.ex index 104a89ef..04e7735c 100644 --- a/lib/philomena_web/controllers/admin/report_controller.ex +++ b/lib/philomena_web/controllers/admin/report_controller.ex @@ -2,7 +2,7 @@ defmodule PhilomenaWeb.Admin.ReportController do use PhilomenaWeb, :controller alias Philomena.Elasticsearch - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.Reports.Report alias Philomena.Reports.Query alias Philomena.Polymorphic @@ -73,7 +73,7 @@ defmodule PhilomenaWeb.Admin.ReportController do reportable: [reportable_id: :reportable_type] ) - body = TextRenderer.render_one(%{body: report.reason}, conn) + body = MarkdownRenderer.render_one(%{body: report.reason}, conn) render(conn, "show.html", title: "Showing Report", report: report, body: body) end @@ -125,7 +125,7 @@ defmodule PhilomenaWeb.Admin.ReportController do mod_notes = mod_notes - |> TextRenderer.render_collection(conn) + |> MarkdownRenderer.render_collection(conn) |> Enum.zip(mod_notes) assign(conn, :mod_notes, mod_notes) diff --git a/lib/philomena_web/controllers/comment_controller.ex b/lib/philomena_web/controllers/comment_controller.ex index 4f2f4ef0..6a5d434f 100644 --- a/lib/philomena_web/controllers/comment_controller.ex +++ b/lib/philomena_web/controllers/comment_controller.ex @@ -1,7 +1,7 @@ defmodule PhilomenaWeb.CommentController do use PhilomenaWeb, :controller - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.Elasticsearch alias Philomena.{Comments.Query, Comments.Comment} import Ecto.Query @@ -39,7 +39,7 @@ defmodule PhilomenaWeb.CommentController do preload(Comment, [:deleted_by, image: [tags: :aliases], user: [awards: :badge]]) ) - rendered = TextRenderer.render_collection(comments.entries, conn) + rendered = MarkdownRenderer.render_collection(comments.entries, conn) comments = %{comments | entries: Enum.zip(rendered, comments.entries)} diff --git a/lib/philomena_web/controllers/conversation_controller.ex b/lib/philomena_web/controllers/conversation_controller.ex index 9a843df4..6fe1a74d 100644 --- a/lib/philomena_web/controllers/conversation_controller.ex +++ b/lib/philomena_web/controllers/conversation_controller.ex @@ -3,7 +3,7 @@ defmodule PhilomenaWeb.ConversationController do alias PhilomenaWeb.NotificationCountPlug alias Philomena.{Conversations, Conversations.Conversation, Conversations.Message} - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.Repo import Ecto.Query @@ -72,7 +72,7 @@ defmodule PhilomenaWeb.ConversationController do rendered = messages.entries - |> TextRenderer.render_collection(conn) + |> MarkdownRenderer.render_collection(conn) messages = %{messages | entries: Enum.zip(messages.entries, rendered)} diff --git a/lib/philomena_web/controllers/dnp_entry_controller.ex b/lib/philomena_web/controllers/dnp_entry_controller.ex index 408f5b33..ff3f1a07 100644 --- a/lib/philomena_web/controllers/dnp_entry_controller.ex +++ b/lib/philomena_web/controllers/dnp_entry_controller.ex @@ -2,7 +2,7 @@ defmodule PhilomenaWeb.DnpEntryController do use PhilomenaWeb, :controller alias Philomena.DnpEntries.DnpEntry - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.DnpEntries alias Philomena.Tags.Tag alias Philomena.ModNotes.ModNote @@ -44,7 +44,7 @@ defmodule PhilomenaWeb.DnpEntryController do bodies = dnp_entries |> Enum.map(&%{body: &1.conditions || "-"}) - |> TextRenderer.render_collection(conn) + |> MarkdownRenderer.render_collection(conn) dnp_entries = %{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)} @@ -61,7 +61,7 @@ defmodule PhilomenaWeb.DnpEntryController do dnp_entry = conn.assigns.dnp_entry [conditions, reason, instructions] = - TextRenderer.render_collection( + MarkdownRenderer.render_collection( [ %{body: dnp_entry.conditions || "-"}, %{body: dnp_entry.reason || "-"}, @@ -164,7 +164,7 @@ defmodule PhilomenaWeb.DnpEntryController do mod_notes = mod_notes - |> TextRenderer.render_collection(conn) + |> MarkdownRenderer.render_collection(conn) |> Enum.zip(mod_notes) assign(conn, :mod_notes, mod_notes) diff --git a/lib/philomena_web/controllers/image/comment_controller.ex b/lib/philomena_web/controllers/image/comment_controller.ex index ebb605c3..adff5887 100644 --- a/lib/philomena_web/controllers/image/comment_controller.ex +++ b/lib/philomena_web/controllers/image/comment_controller.ex @@ -2,7 +2,7 @@ defmodule PhilomenaWeb.Image.CommentController do use PhilomenaWeb, :controller alias PhilomenaWeb.CommentLoader - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.{Images.Image, Comments.Comment} alias Philomena.UserStatistics alias Philomena.Comments @@ -48,7 +48,7 @@ defmodule PhilomenaWeb.Image.CommentController do def index(conn, _params) do comments = CommentLoader.load_comments(conn, conn.assigns.image) - rendered = TextRenderer.render_collection(comments.entries, conn) + rendered = MarkdownRenderer.render_collection(comments.entries, conn) comments = %{comments | entries: Enum.zip(comments.entries, rendered)} @@ -56,7 +56,7 @@ defmodule PhilomenaWeb.Image.CommentController do end def show(conn, _params) do - rendered = TextRenderer.render_one(conn.assigns.comment, conn) + rendered = MarkdownRenderer.render_one(conn.assigns.comment, conn) render(conn, "show.html", layout: false, diff --git a/lib/philomena_web/controllers/image/description_controller.ex b/lib/philomena_web/controllers/image/description_controller.ex index dc92e536..6fdfee7e 100644 --- a/lib/philomena_web/controllers/image/description_controller.ex +++ b/lib/philomena_web/controllers/image/description_controller.ex @@ -1,7 +1,7 @@ defmodule PhilomenaWeb.Image.DescriptionController do use PhilomenaWeb, :controller - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.Images.Image alias Philomena.Images @@ -35,7 +35,7 @@ defmodule PhilomenaWeb.Image.DescriptionController do Images.reindex_image(image) body = - TextRenderer.render_one(%{body: image.description}, conn) + MarkdownRenderer.render_one(%{body: image.description}, conn) conn |> put_view(PhilomenaWeb.ImageView) diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index 52a82e5c..4d293421 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -4,7 +4,7 @@ defmodule PhilomenaWeb.ImageController do alias PhilomenaWeb.ImageLoader alias PhilomenaWeb.CommentLoader alias PhilomenaWeb.NotificationCountPlug - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.{ Images, @@ -63,13 +63,13 @@ defmodule PhilomenaWeb.ImageController do comments = CommentLoader.load_comments(conn, image) - rendered = TextRenderer.render_collection(comments.entries, conn) + rendered = MarkdownRenderer.render_collection(comments.entries, conn) comments = %{comments | entries: Enum.zip(comments.entries, rendered)} description = %{body: image.description} - |> TextRenderer.render_one(conn) + |> MarkdownRenderer.render_one(conn) interactions = Interactions.user_interactions([image], conn.assigns.current_user) diff --git a/lib/philomena_web/controllers/post/preview_controller.ex b/lib/philomena_web/controllers/post/preview_controller.ex index dd1b235b..b70bf5bd 100644 --- a/lib/philomena_web/controllers/post/preview_controller.ex +++ b/lib/philomena_web/controllers/post/preview_controller.ex @@ -1,7 +1,7 @@ defmodule PhilomenaWeb.Post.PreviewController do use PhilomenaWeb, :controller - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.Posts.Post alias Philomena.Repo @@ -11,7 +11,7 @@ defmodule PhilomenaWeb.Post.PreviewController do anonymous = params["anonymous"] == true post = %Post{user: user, body: body, anonymous: anonymous} - rendered = TextRenderer.render_one(post, conn) + rendered = MarkdownRenderer.render_one(post, conn) render(conn, "create.html", layout: false, post: post, body: rendered) end diff --git a/lib/philomena_web/controllers/post_controller.ex b/lib/philomena_web/controllers/post_controller.ex index e849b5ba..cafcff23 100644 --- a/lib/philomena_web/controllers/post_controller.ex +++ b/lib/philomena_web/controllers/post_controller.ex @@ -1,7 +1,7 @@ defmodule PhilomenaWeb.PostController do use PhilomenaWeb, :controller - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.Elasticsearch alias Philomena.{Posts.Query, Posts.Post} import Ecto.Query @@ -36,7 +36,7 @@ defmodule PhilomenaWeb.PostController do preload(Post, [:deleted_by, topic: :forum, user: [awards: :badge]]) ) - rendered = TextRenderer.render_collection(posts.entries, conn) + rendered = MarkdownRenderer.render_collection(posts.entries, conn) posts = %{posts | entries: Enum.zip(rendered, posts.entries)} diff --git a/lib/philomena_web/controllers/profile/commission_controller.ex b/lib/philomena_web/controllers/profile/commission_controller.ex index da2b93cf..20590279 100644 --- a/lib/philomena_web/controllers/profile/commission_controller.ex +++ b/lib/philomena_web/controllers/profile/commission_controller.ex @@ -3,7 +3,7 @@ defmodule PhilomenaWeb.Profile.CommissionController do alias Philomena.Commissions.Commission alias Philomena.Commissions - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.Users.User plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create, :edit, :update, :delete] @@ -37,15 +37,15 @@ defmodule PhilomenaWeb.Profile.CommissionController do item_descriptions = items |> Enum.map(&%{body: &1.description}) - |> TextRenderer.render_collection(conn) + |> MarkdownRenderer.render_collection(conn) item_add_ons = items |> Enum.map(&%{body: &1.add_ons}) - |> TextRenderer.render_collection(conn) + |> MarkdownRenderer.render_collection(conn) [information, contact, will_create, will_not_create] = - TextRenderer.render_collection( + MarkdownRenderer.render_collection( [ %{body: commission.information || ""}, %{body: commission.contact || ""}, diff --git a/lib/philomena_web/controllers/profile/detail_controller.ex b/lib/philomena_web/controllers/profile/detail_controller.ex index 17e88b0f..0461c31e 100644 --- a/lib/philomena_web/controllers/profile/detail_controller.ex +++ b/lib/philomena_web/controllers/profile/detail_controller.ex @@ -3,7 +3,7 @@ defmodule PhilomenaWeb.Profile.DetailController do alias Philomena.UserNameChanges.UserNameChange alias Philomena.ModNotes.ModNote - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.Polymorphic alias Philomena.Users.User alias Philomena.Repo @@ -30,7 +30,7 @@ defmodule PhilomenaWeb.Profile.DetailController do mod_notes = mod_notes - |> TextRenderer.render_collection(conn) + |> MarkdownRenderer.render_collection(conn) |> Enum.zip(mod_notes) name_changes = diff --git a/lib/philomena_web/controllers/profile_controller.ex b/lib/philomena_web/controllers/profile_controller.ex index 3fc31178..d1d6f98b 100644 --- a/lib/philomena_web/controllers/profile_controller.ex +++ b/lib/philomena_web/controllers/profile_controller.ex @@ -3,7 +3,7 @@ defmodule PhilomenaWeb.ProfileController do alias PhilomenaWeb.ImageLoader alias Philomena.Elasticsearch - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.UserStatistics.UserStatistic alias Philomena.Users.User alias Philomena.Bans @@ -131,14 +131,14 @@ defmodule PhilomenaWeb.ProfileController do recent_comments = recent_comments |> Enum.filter(&Canada.Can.can?(current_user, :show, &1.image)) - |> TextRenderer.render_collection(conn) + |> MarkdownRenderer.render_collection(conn) |> Enum.zip(recent_comments) about_me = - TextRenderer.render_one(%{body: user.description || ""}, conn) + MarkdownRenderer.render_one(%{body: user.description || ""}, conn) scratchpad = - TextRenderer.render_one(%{body: user.scratchpad || ""}, conn) + MarkdownRenderer.render_one(%{body: user.scratchpad || ""}, conn) commission_information = commission_info(user.commission, conn) @@ -218,7 +218,7 @@ defmodule PhilomenaWeb.ProfileController do defp commission_info(%{information: info}, conn) when info not in [nil, ""], - do: TextRenderer.render_one(%{body: info}, conn) + do: MarkdownRenderer.render_one(%{body: info}, conn) defp commission_info(_commission, _conn), do: "" @@ -285,7 +285,7 @@ defmodule PhilomenaWeb.ProfileController do mod_notes = mod_notes - |> TextRenderer.render_collection(conn) + |> MarkdownRenderer.render_collection(conn) |> Enum.zip(mod_notes) assign(conn, :mod_notes, mod_notes) diff --git a/lib/philomena_web/controllers/tag_controller.ex b/lib/philomena_web/controllers/tag_controller.ex index fc0116ec..49049744 100644 --- a/lib/philomena_web/controllers/tag_controller.ex +++ b/lib/philomena_web/controllers/tag_controller.ex @@ -5,7 +5,7 @@ defmodule PhilomenaWeb.TagController do alias Philomena.Elasticsearch alias Philomena.{Tags, Tags.Tag} alias Philomena.{Images, Images.Image} - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.Interactions import Ecto.Query @@ -62,10 +62,10 @@ defmodule PhilomenaWeb.TagController do interactions = Interactions.user_interactions(images, user) body = - TextRenderer.render_one(%{body: tag.description || ""}, conn) + MarkdownRenderer.render_one(%{body: tag.description || ""}, conn) dnp_bodies = - TextRenderer.render_collection( + MarkdownRenderer.render_collection( Enum.map(tag.dnp_entries, &%{body: &1.conditions || ""}), conn ) diff --git a/lib/philomena_web/controllers/topic_controller.ex b/lib/philomena_web/controllers/topic_controller.ex index 536d9336..4c433be2 100644 --- a/lib/philomena_web/controllers/topic_controller.ex +++ b/lib/philomena_web/controllers/topic_controller.ex @@ -5,7 +5,7 @@ defmodule PhilomenaWeb.TopicController do alias Philomena.{Forums.Forum, Topics.Topic, Posts.Post, Polls.Poll, PollOptions.PollOption} alias Philomena.{Forums, Topics, Polls, Posts} alias Philomena.PollVotes - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.Repo import Ecto.Query @@ -60,7 +60,7 @@ defmodule PhilomenaWeb.TopicController do |> preload([:deleted_by, :topic, topic: :forum, user: [awards: :badge]]) |> Repo.all() - rendered = TextRenderer.render_collection(posts, conn) + rendered = MarkdownRenderer.render_collection(posts, conn) posts = Enum.zip(posts, rendered) diff --git a/lib/philomena_web/image_loader.ex b/lib/philomena_web/image_loader.ex index 5aae5e81..b037a75a 100644 --- a/lib/philomena_web/image_loader.ex +++ b/lib/philomena_web/image_loader.ex @@ -2,7 +2,7 @@ defmodule PhilomenaWeb.ImageLoader do alias PhilomenaWeb.ImageSorter alias Philomena.Elasticsearch alias Philomena.Images.{Image, Query} - alias PhilomenaWeb.TextRenderer + alias PhilomenaWeb.MarkdownRenderer alias Philomena.Tags.Tag alias Philomena.Repo import Ecto.Query @@ -132,7 +132,7 @@ defmodule PhilomenaWeb.ImageLoader do defp render_bodies([tag], conn) do dnp_bodies = - TextRenderer.render_collection( + MarkdownRenderer.render_collection( Enum.map(tag.dnp_entries, &%{body: &1.conditions || ""}), conn ) @@ -140,7 +140,7 @@ defmodule PhilomenaWeb.ImageLoader do dnp_entries = Enum.zip(dnp_bodies, tag.dnp_entries) description = - TextRenderer.render_one(%{body: tag.description || ""}, conn) + MarkdownRenderer.render_one(%{body: tag.description || ""}, conn) [{tag, description, dnp_entries}] end diff --git a/lib/philomena_web/markdown_renderer.ex b/lib/philomena_web/markdown_renderer.ex index 242a20af..593daea5 100644 --- a/lib/philomena_web/markdown_renderer.ex +++ b/lib/philomena_web/markdown_renderer.ex @@ -8,11 +8,21 @@ defmodule PhilomenaWeb.MarkdownRenderer do @image_view Module.concat(["PhilomenaWeb.ImageView"]) - def render(text, conn) do - images = find_images(text) - representations = render_representations(images, conn) + def render_one(item, conn) do + hd(render_collection([item], conn)) + end - Markdown.to_html(text, representations) + def render_collection(collection, conn) do + representations = + collection + |> Enum.flat_map(fn %{body: text} -> + find_images(text) + end) + |> render_representations(conn) + + Enum.map(collection, fn %{body: text} -> + Markdown.to_html(text, representations) + end) end def render_unsafe(text, conn) do diff --git a/lib/philomena_web/text_renderer.ex b/lib/philomena_web/text_renderer.ex deleted file mode 100644 index 2e76a1ca..00000000 --- a/lib/philomena_web/text_renderer.ex +++ /dev/null @@ -1,13 +0,0 @@ -defmodule PhilomenaWeb.TextRenderer do - alias PhilomenaWeb.MarkdownRenderer - - def render_one(item, conn) do - hd(render_collection([item], conn)) - end - - def render_collection(items, conn) do - Enum.map(items, fn item -> - MarkdownRenderer.render(item.body, conn) - end) - end -end