fd8465c1cd34b259e55f9566c236c425d1d3a7d5
[akkoma] / lib / pleroma / formatter.ex
1 defmodule Pleroma.Formatter do
2 alias Pleroma.User
3 alias Pleroma.Web.MediaProxy
4
5 @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~\(\)]+[\w\/]/u
6 def linkify(text) do
7 Regex.replace(@link_regex, text, "<a href='\\0'>\\0</a>")
8 end
9
10 @tag_regex ~r/\#\w+/u
11 def parse_tags(text, data \\ %{}) do
12 Regex.scan(@tag_regex, text)
13 |> Enum.map(fn (["#" <> tag = full_tag]) -> {full_tag, String.downcase(tag)} end)
14 |> (fn map -> if data["sensitive"] in [true, "True", "true", "1"], do: [{"#nsfw", "nsfw"}] ++ map, else: map end).()
15 end
16
17 def parse_mentions(text) do
18 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
19 regex = ~r/@[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@?[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/u
20
21 Regex.scan(regex, text)
22 |> List.flatten
23 |> Enum.uniq
24 |> Enum.map(fn ("@" <> match = full_match) -> {full_match, User.get_cached_by_nickname(match)} end)
25 |> Enum.filter(fn ({_match, user}) -> user end)
26 end
27
28 def html_escape(text) do
29 Regex.split(@link_regex, text, include_captures: true)
30 |> Enum.map_every(2, fn chunk ->
31 {:safe, part} = Phoenix.HTML.html_escape(chunk)
32 part
33 end)
34 |> Enum.join("")
35 end
36
37 @finmoji [
38 "a_trusted_friend",
39 "alandislands",
40 "association",
41 "auroraborealis",
42 "baby_in_a_box",
43 "bear",
44 "black_gold",
45 "christmasparty",
46 "crosscountryskiing",
47 "cupofcoffee",
48 "education",
49 "fashionista_finns",
50 "finnishlove",
51 "flag",
52 "forest",
53 "four_seasons_of_bbq",
54 "girlpower",
55 "handshake",
56 "happiness",
57 "headbanger",
58 "icebreaker",
59 "iceman",
60 "joulutorttu",
61 "kaamos",
62 "kalsarikannit_f",
63 "kalsarikannit_m",
64 "karjalanpiirakka",
65 "kicksled",
66 "kokko",
67 "lavatanssit",
68 "losthopes_f",
69 "losthopes_m",
70 "mattinykanen",
71 "meanwhileinfinland",
72 "moominmamma",
73 "nordicfamily",
74 "out_of_office",
75 "peacemaker",
76 "perkele",
77 "pesapallo",
78 "polarbear",
79 "pusa_hispida_saimensis",
80 "reindeer",
81 "sami",
82 "sauna_f",
83 "sauna_m",
84 "sauna_whisk",
85 "sisu",
86 "stuck",
87 "suomimainittu",
88 "superfood",
89 "swan",
90 "the_cap",
91 "the_conductor",
92 "the_king",
93 "the_voice",
94 "theoriginalsanta",
95 "tomoffinland",
96 "torillatavataan",
97 "unbreakable",
98 "waiting",
99 "white_nights",
100 "woollysocks"
101 ]
102
103 @finmoji_with_filenames Enum.map(@finmoji, fn (finmoji) ->
104 {finmoji, "/finmoji/128px/#{finmoji}-128.png"}
105 end)
106
107 @emoji_from_file (with {:ok, default} <- File.read("config/emoji.txt") do
108 custom =
109 with {:ok, custom} <- File.read("config/custom_emoji.txt") do
110 custom
111 else
112 _e -> ""
113 end
114 (default <> "\n" <> custom)
115 |> String.trim()
116 |> String.split(~r/\n+/)
117 |> Enum.map(fn(line) ->
118 [name, file] = String.split(line, ~r/,\s*/)
119 {name, file}
120 end)
121 else
122 _ -> []
123 end)
124
125 @emoji @finmoji_with_filenames ++ @emoji_from_file
126
127 def emojify(text, additional \\ nil) do
128 all_emoji = if additional do
129 Map.to_list(additional) ++ @emoji
130 else
131 @emoji
132 end
133
134 Enum.reduce(all_emoji, text, fn ({emoji, file}, text) ->
135 emoji = HtmlSanitizeEx.strip_tags(emoji)
136 file = HtmlSanitizeEx.strip_tags(file)
137 String.replace(text, ":#{emoji}:", "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{MediaProxy.url(file)}' />")
138 end)
139 end
140
141 def get_emoji(text) do
142 Enum.filter(@emoji, fn ({emoji, _}) -> String.contains?(text, ":#{emoji}:") end)
143 end
144
145 def get_custom_emoji() do
146 @emoji
147 end
148 end