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