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