This repository has been archived by the owner on Apr 13, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
89 lines (75 loc) · 3.05 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// mauIRC - The original mauIRC web frontend
// Copyright (C) 2016 Tulir Asokan
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package util contains utilities
package util
import (
"fmt"
"regexp"
"strings"
)
var italicEncRegex = regexp.MustCompile("_([^_]*)_")
var boldEncRegex = regexp.MustCompile(`\*([^\*]*)\*`)
var underlineEncRegex = regexp.MustCompile("~([^~]*)~")
var bothColorEncRegex = regexp.MustCompile("c(1[0-5]|[0-9])>([^<]*)<")
var fgColorEncRegex = regexp.MustCompile("c(1[0-5]|[0-9]),(1[0-5]|[0-9])>([^<]*)<")
// EncodeMessage encodes the message markdown-ish styles and colors into IRC format
func EncodeMessage(msg string) string {
msg = italicEncRegex.ReplaceAllString(msg, "\x1D$1\x1D")
msg = boldEncRegex.ReplaceAllString(msg, "\x02$1\x02")
msg = underlineEncRegex.ReplaceAllString(msg, "\x1F$1\x1F")
msg = bothColorEncRegex.ReplaceAllString(msg, "\x03$1$2")
msg = fgColorEncRegex.ReplaceAllString(msg, "\x03$1,$2$3")
return msg
}
var colors = []string{
"#FFFFFF",
"#000000",
"#00007F",
"#009300",
"#FF0000",
"#7F0000",
"#9C009C",
"#FC7F00",
"#FFFF00",
"#00FC00",
"#009393",
"#00FFFF",
"#0000FC",
"#FF00FF",
"#7F7F7F",
"#D2D2D2",
}
var italicDecRegex = regexp.MustCompile("\x1D([^\x1D]*)?\x1D?")
var boldDecRegex = regexp.MustCompile("\x02([^\x02]*)?\x02?")
var underlineDecRegex = regexp.MustCompile("\x1F([^\x1F]*)?\x1F?")
var monospaceDecRegex = regexp.MustCompile("`([^`]*)`")
var bothColorDecRegex = regexp.MustCompile("\x03(1[0-5]|[0-9]),(1[0-5]|[0-9])([^\x03]*)?\x03?")
var fgColorDecRegex = regexp.MustCompile("\x03(1[0-5]|[0-9])([^\x03]*)?\x03?")
// DecodeMessage decodes the IRC styles and colors into a markdown-ish format
func DecodeMessage(msg string) string {
msg = italicDecRegex.ReplaceAllString(msg, "<i>$1</i>")
msg = boldDecRegex.ReplaceAllString(msg, "<b>$1</b>")
msg = underlineDecRegex.ReplaceAllString(msg, "<u>$1</u>")
msg = monospaceDecRegex.ReplaceAllString(msg, "<tt>$1</tt>")
msg = bothColorDecRegex.ReplaceAllString(msg, "<span style='color: $1; background-color: $2;'>$3</span>")
msg = fgColorDecRegex.ReplaceAllString(msg, "<span style='color: $1;'>$2</span>")
for i, color := range colors {
msg = strings.Replace(msg, fmt.Sprintf("color: %d;", i), fmt.Sprintf("#color: %s;", color), -1)
}
return msg
}
var removeFormatRegex = regexp.MustCompile("\x1D|\x02|\x1F|\x03")
// RemoveFormatChars removes all IRC format characters
func RemoveFormatChars(msg string) string {
return removeFormatRegex.ReplaceAllString(msg, "")
}