1 # Pleroma: A lightweight social networking server
2 # Copyright © 2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 # This file is derived from Earmark, under the following copyright:
6 # Copyright © 2014 Dave Thomas, The Pragmatic Programmers
7 # SPDX-License-Identifier: Apache-2.0
8 # Upstream: https://github.com/pragdave/earmark/blob/master/lib/earmark/html_renderer.ex
9 defmodule Pleroma.EarmarkRenderer do
14 alias Earmark.HtmlRenderer
17 import Earmark.Inline, only: [convert: 3]
18 import Earmark.Helpers.HtmlHelpers
19 import Earmark.Message, only: [add_messages_from: 2, get_messages: 1, set_messages: 2]
20 import Earmark.Context, only: [append: 2, set_value: 2]
21 import Earmark.Options, only: [get_mapper: 1]
24 def render(blocks, %Context{options: %Options{}} = context) do
25 messages = get_messages(context)
28 get_mapper(context.options).(
30 &render_block(&1, put_in(context.options.messages, []))
36 |> Enum.reduce(messages, fn ctx, messages1 -> messages1 ++ get_messages(ctx) end)
38 {put_in(context.options.messages, all_messages), html |> IO.iodata_to_binary()}
44 defp render_block(%Block.Para{lnb: lnb, lines: lines, attrs: attrs}, context) do
45 lines = convert(lines, lnb, context)
46 add_attrs(lines, "<p>#{lines.value}</p>", attrs, [], lnb)
52 defp render_block(%Block.Html{html: html}, context) do
56 defp render_block(%Block.HtmlComment{lines: lines}, context) do
60 defp render_block(%Block.HtmlOneline{html: html}, context) do
67 defp render_block(%Block.Ruler{lnb: lnb, attrs: attrs}, context) do
68 add_attrs(context, "<hr />", attrs, [], lnb)
75 %Block.Heading{lnb: lnb, level: level, content: content, attrs: attrs},
78 converted = convert(content, lnb, context)
79 html = "<h#{level}>#{converted.value}</h#{level}>"
80 add_attrs(converted, html, attrs, [], lnb)
87 defp render_block(%Block.BlockQuote{lnb: lnb, blocks: blocks, attrs: attrs}, context) do
88 {context1, body} = render(blocks, context)
89 html = "<blockquote>#{body}</blockquote>"
90 add_attrs(context1, html, attrs, [], lnb)
98 %Block.Table{lnb: lnb, header: header, rows: rows, alignments: aligns, attrs: attrs},
101 {context1, html} = add_attrs(context, "<table>", attrs, [], lnb)
102 context2 = set_value(context1, html)
106 append(add_trs(append(context2, "<thead>"), [header], "th", aligns, lnb), "</thead>")
108 # Maybe an error, needed append(context, html)
112 context4 = append(add_trs(append(context3, "<tbody>"), rows, "td", aligns, lnb), "</tbody>")
114 {context4, [context4.value, "</table>"]}
122 %Block.Code{lnb: lnb, language: language, attrs: attrs} = block,
123 %Context{options: options} = context
126 if language, do: ~s{ class="#{code_classes(language, options.code_class_prefix)}"}, else: ""
128 tag = ~s[<pre><code#{class}>]
129 lines = options.render_code.(block)
130 html = ~s[#{tag}#{lines}</code></pre>]
131 add_attrs(context, html, attrs, [], lnb)
139 %Block.List{lnb: lnb, type: type, blocks: items, attrs: attrs, start: start},
142 {context1, content} = render(items, context)
143 html = "<#{type}#{start}>#{content}</#{type}>"
144 add_attrs(context1, html, attrs, [], lnb)
147 # format a single paragraph list item, and remove the para tags
149 %Block.ListItem{lnb: lnb, blocks: blocks, spaced: false, attrs: attrs},
152 when length(blocks) == 1 do
153 {context1, content} = render(blocks, context)
154 content = Regex.replace(~r{</?p>}, content, "")
155 html = "<li>#{content}</li>"
156 add_attrs(context1, html, attrs, [], lnb)
159 # format a spaced list item
160 defp render_block(%Block.ListItem{lnb: lnb, blocks: blocks, attrs: attrs}, context) do
161 {context1, content} = render(blocks, context)
162 html = "<li>#{content}</li>"
163 add_attrs(context1, html, attrs, [], lnb)
170 defp render_block(%Block.FnList{blocks: footnotes}, context) do
172 Enum.map(footnotes, fn note ->
173 blocks = append_footnote_link(note)
174 %Block.ListItem{attrs: "#fn:#{note.number}", type: :ol, blocks: blocks}
177 {context1, html} = render_block(%Block.List{type: :ol, blocks: items}, context)
178 {context1, Enum.join([~s[<div class="footnotes">], "<hr />", html, "</div>"])}
181 #######################################
182 # Isolated IALs are rendered as paras #
183 #######################################
185 defp render_block(%Block.Ial{verbatim: verbatim}, context) do
186 {context, "<p>{:#{verbatim}}</p>"}
193 defp render_block(%Block.IdDef{}, context), do: {context, ""}
195 #####################################
196 # And here are the inline renderers #
197 #####################################
199 defdelegate br, to: HtmlRenderer
200 defdelegate codespan(text), to: HtmlRenderer
201 defdelegate em(text), to: HtmlRenderer
202 defdelegate strong(text), to: HtmlRenderer
203 defdelegate strikethrough(text), to: HtmlRenderer
205 defdelegate link(url, text), to: HtmlRenderer
206 defdelegate link(url, text, title), to: HtmlRenderer
208 defdelegate image(path, alt, title), to: HtmlRenderer
210 defdelegate footnote_link(ref, backref, number), to: HtmlRenderer
213 defp add_trs(context, rows, tag, aligns, lnb) do
216 |> Enum.zip(Stream.iterate(lnb, &(&1 + 1)))
219 |> Enum.reduce(context, fn {row, lnb}, ctx ->
220 append(add_tds(append(ctx, "<tr>"), row, tag, aligns, lnb), "</tr>")
224 defp add_tds(context, row, tag, aligns, lnb) do
225 Enum.reduce(1..length(row), context, add_td_fn(row, tag, aligns, lnb))
228 defp add_td_fn(row, tag, aligns, lnb) do
231 case Enum.at(aligns, n - 1, :default) do
233 align -> " style=\"text-align: #{align}\""
236 col = Enum.at(row, n - 1)
237 converted = convert(col, lnb, set_messages(ctx, []))
238 append(add_messages_from(ctx, converted), "<#{tag}#{style}>#{converted.value}</#{tag}>")
242 ###############################
243 # Append Footnote Return Link #
244 ###############################
246 defdelegate append_footnote_link(note), to: HtmlRenderer
247 defdelegate append_footnote_link(note, fnlink), to: HtmlRenderer
249 defdelegate render_code(lines), to: HtmlRenderer
251 defp code_classes(language, prefix) do
252 ["" | String.split(prefix || "")]
253 |> Enum.map(fn pfx -> "#{pfx}#{language}" end)