Merge branch 'formatting-fixes' into 'develop'
[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 @finmoji [
28 "a_trusted_friend",
29 "alandislands",
30 "association",
31 "auroraborealis",
32 "baby_in_a_box",
33 "bear",
34 "black_gold",
35 "christmasparty",
36 "crosscountryskiing",
37 "cupofcoffee",
38 "education",
39 "fashionista_finns",
40 "finnishlove",
41 "flag",
42 "forest",
43 "four_seasons_of_bbq",
44 "girlpower",
45 "handshake",
46 "happiness",
47 "headbanger",
48 "icebreaker",
49 "iceman",
50 "joulutorttu",
51 "kaamos",
52 "kalsarikannit_f",
53 "kalsarikannit_m",
54 "karjalanpiirakka",
55 "kicksled",
56 "kokko",
57 "lavatanssit",
58 "losthopes_f",
59 "losthopes_m",
60 "mattinykanen",
61 "meanwhileinfinland",
62 "moominmamma",
63 "nordicfamily",
64 "out_of_office",
65 "peacemaker",
66 "perkele",
67 "pesapallo",
68 "polarbear",
69 "pusa_hispida_saimensis",
70 "reindeer",
71 "sami",
72 "sauna_f",
73 "sauna_m",
74 "sauna_whisk",
75 "sisu",
76 "stuck",
77 "suomimainittu",
78 "superfood",
79 "swan",
80 "the_cap",
81 "the_conductor",
82 "the_king",
83 "the_voice",
84 "theoriginalsanta",
85 "tomoffinland",
86 "torillatavataan",
87 "unbreakable",
88 "waiting",
89 "white_nights",
90 "woollysocks"
91 ]
92
93 @finmoji_with_filenames Enum.map(@finmoji, fn (finmoji) ->
94 {finmoji, "/finmoji/128px/#{finmoji}-128.png"}
95 end)
96
97 @emoji_from_file (with {:ok, file} <- File.read("config/emoji.txt") do
98 file
99 |> String.trim
100 |> String.split("\n")
101 |> Enum.map(fn(line) ->
102 [name, file] = String.split(line, ", ")
103 {name, file}
104 end)
105 else
106 _ -> []
107 end)
108
109 @emoji @finmoji_with_filenames ++ @emoji_from_file
110
111 def emojify(text, additional \\ nil) do
112 all_emoji = if additional do
113 Map.to_list(additional) ++ @emoji
114 else
115 @emoji
116 end
117
118 Enum.reduce(all_emoji, text, fn ({emoji, file}, text) ->
119 String.replace(text, ":#{emoji}:", "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{file}' />")
120 end)
121 end
122
123 def get_emoji(text) do
124 Enum.filter(@emoji, fn ({emoji, _}) -> String.contains?(text, ":#{emoji}:") end)
125 end
126
127 def get_custom_emoji() do
128 @emoji
129 end
130 end