logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

formatter.ex (5344B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Formatter do
  5. alias Pleroma.HTML
  6. alias Pleroma.User
  7. @safe_mention_regex ~r/^(\s*(?<mentions>(@.+?\s+){1,})+)(?<rest>.*)/s
  8. @link_regex ~r"((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+"ui
  9. @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
  10. defp linkify_opts do
  11. Pleroma.Config.get(Pleroma.Formatter) ++
  12. [
  13. hashtag: true,
  14. hashtag_handler: &Pleroma.Formatter.hashtag_handler/4,
  15. mention: true,
  16. mention_handler: &Pleroma.Formatter.mention_handler/4,
  17. phone: false,
  18. extra_prefixes: Pleroma.Config.get([:uri_schemes, :valid_schemes])
  19. ]
  20. end
  21. def escape_mention_handler("@" <> nickname = mention, buffer, _, _) do
  22. case User.get_cached_by_nickname(nickname) do
  23. %User{} ->
  24. # escape markdown characters with `\\`
  25. # (we don't want something like @user__name to be parsed by markdown)
  26. String.replace(mention, @markdown_characters_regex, "\\\\\\1")
  27. _ ->
  28. buffer
  29. end
  30. end
  31. def mention_handler("@" <> nickname, buffer, opts, acc) do
  32. case User.get_cached_by_nickname(nickname) do
  33. %User{} = user ->
  34. {mention_from_user(user, opts),
  35. %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, user})}}
  36. _ ->
  37. {buffer, acc}
  38. end
  39. end
  40. def mention_from_user(%User{id: id} = user, opts \\ %{mentions_format: :full}) do
  41. user_url = user.uri || user.ap_id
  42. nickname_text = get_nickname_text(user.nickname, opts)
  43. Phoenix.HTML.Tag.content_tag(
  44. :span,
  45. Phoenix.HTML.Tag.content_tag(
  46. :a,
  47. ["@", Phoenix.HTML.Tag.content_tag(:span, nickname_text)],
  48. "data-user": id,
  49. class: "u-url mention",
  50. href: user_url,
  51. rel: "ugc"
  52. ),
  53. class: "h-card"
  54. )
  55. |> Phoenix.HTML.safe_to_string()
  56. end
  57. def hashtag_handler("#" <> tag = tag_text, _buffer, _opts, acc) do
  58. tag = String.downcase(tag)
  59. url = "#{Pleroma.Web.Endpoint.url()}/tag/#{tag}"
  60. link =
  61. Phoenix.HTML.Tag.content_tag(:a, tag_text,
  62. class: "hashtag",
  63. "data-tag": tag,
  64. href: url,
  65. rel: "tag ugc"
  66. )
  67. |> Phoenix.HTML.safe_to_string()
  68. {link, %{acc | tags: MapSet.put(acc.tags, {tag_text, tag})}}
  69. end
  70. @doc """
  71. Parses a text and replace plain text links with HTML. Returns a tuple with a result text, mentions, and hashtags.
  72. If the 'safe_mention' option is given, only consecutive mentions at the start the post are actually mentioned.
  73. """
  74. @spec linkify(String.t(), keyword()) ::
  75. {String.t(), [{String.t(), User.t()}], [{String.t(), String.t()}]}
  76. def linkify(text, options \\ []) do
  77. options = linkify_opts() ++ options
  78. if options[:safe_mention] && Regex.named_captures(@safe_mention_regex, text) do
  79. %{"mentions" => mentions, "rest" => rest} = Regex.named_captures(@safe_mention_regex, text)
  80. acc = %{mentions: MapSet.new(), tags: MapSet.new()}
  81. {text_mentions, %{mentions: mentions}} = Linkify.link_map(mentions, acc, options)
  82. {text_rest, %{tags: tags}} = Linkify.link_map(rest, acc, options)
  83. {text_mentions <> text_rest, MapSet.to_list(mentions), MapSet.to_list(tags)}
  84. else
  85. acc = %{mentions: MapSet.new(), tags: MapSet.new()}
  86. {text, %{mentions: mentions, tags: tags}} = Linkify.link_map(text, acc, options)
  87. {text, MapSet.to_list(mentions), MapSet.to_list(tags)}
  88. end
  89. end
  90. @doc """
  91. Escapes a special characters in mention names.
  92. """
  93. def mentions_escape(text, options \\ []) do
  94. options =
  95. Keyword.merge(options,
  96. mention: true,
  97. url: false,
  98. mention_handler: &Pleroma.Formatter.escape_mention_handler/4
  99. )
  100. if options[:safe_mention] && Regex.named_captures(@safe_mention_regex, text) do
  101. %{"mentions" => mentions, "rest" => rest} = Regex.named_captures(@safe_mention_regex, text)
  102. Linkify.link(mentions, options) <> Linkify.link(rest, options)
  103. else
  104. Linkify.link(text, options)
  105. end
  106. end
  107. def markdown_to_html(text) do
  108. Earmark.as_html!(text, %Earmark.Options{compact_output: true, smartypants: false})
  109. end
  110. def html_escape({text, mentions, hashtags}, type) do
  111. {html_escape(text, type), mentions, hashtags}
  112. end
  113. def html_escape(text, "text/html") do
  114. HTML.filter_tags(text)
  115. end
  116. def html_escape(text, "text/plain") do
  117. Regex.split(@link_regex, text, include_captures: true)
  118. |> Enum.map_every(2, fn chunk ->
  119. {:safe, part} = Phoenix.HTML.html_escape(chunk)
  120. part
  121. end)
  122. |> Enum.join("")
  123. end
  124. def truncate(text, max_length \\ 200, omission \\ "...") do
  125. # Remove trailing whitespace
  126. text = Regex.replace(~r/([^ \t\r\n])([ \t]+$)/u, text, "\\g{1}")
  127. if String.length(text) < max_length do
  128. text
  129. else
  130. length_with_omission = max_length - String.length(omission)
  131. String.slice(text, 0, length_with_omission) <> omission
  132. end
  133. end
  134. defp get_nickname_text(nickname, %{mentions_format: :full}), do: User.full_nickname(nickname)
  135. defp get_nickname_text(nickname, _), do: User.local_nickname(nickname)
  136. end