Skip to content

Commit

Permalink
Rewrite irc2md()
Browse files Browse the repository at this point in the history
  • Loading branch information
hloeung committed Sep 16, 2023
1 parent 8a26135 commit fbb00d7
Showing 1 changed file with 55 additions and 59 deletions.
114 changes: 55 additions & 59 deletions mm-go-irckit/server_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,73 +814,69 @@ func CmdWhois(s Server, u *User, msg *irc.Message) error {

//nolint:funlen,gocognit,gocyclo,cyclop
func irc2md(msg string) string {
data := []byte(msg)
emphasis_supported := map[byte][]byte{
'\x02': []byte{'*', '*'}, // Bold 0x02 ** (**text**)
'\x1d': []byte{'_'}, // Italics 0x1D _ (_text_)
'\x11': []byte{'`'}, // Monospace 0x11 ` (`text`)
'\x0f': []byte{' '}, // Reset 0x0F (**text\x0f)
}
emphasis_unsupported := map[byte]string{
'\x1f': "", // Underline 0x1f
'\x1e': "", // Strikethr 0x1e
}

var buf []byte
emphasis := ""
for _, char := range data {
switch {
// Bold 0x02 ** (**text**)
case char == '\x02' && emphasis == "":
buf = append(buf, '*', '*')
emphasis = "b"
case char == '\x02' && emphasis == "b":
buf = append(buf, '*', '*')
emphasis = ""
// Italics 0x1D _ (_text_)
case char == '\x1d' && emphasis == "":
buf = append(buf, '_')
emphasis = "i"
case char == '\x1d' && emphasis == "i":
buf = append(buf, '_')
emphasis = ""
// Bold+Ital 0x02+0x1D **_ (**_text_**)
case char == '\x1d' && emphasis == "b":
buf = append(buf, '_')
emphasis = "bi"
case char == '\x1d' && emphasis == "bi":
buf = append(buf, '_')
emphasis = "b"
// Monospace 0x11 ` (`text`)
case char == '\x11' && emphasis == "":
buf = append(buf, '`')
emphasis = "m"
case char == '\x11' && emphasis == "m":
buf = append(buf, '`')
emphasis = ""

// Reset 0x0F (**text\x0f)
case char == '\x0f' && emphasis != "":
switch {
case emphasis == "b":
buf = append(buf, '*', '*')
emphasis = ""
case emphasis == "i":
buf = append(buf, '_')
emphasis = ""
case emphasis == "bi":
buf = append(buf, '_', '*', '*')
emphasis = ""
case emphasis == "m":
buf = append(buf, '`')
emphasis = ""
}

default:
var cur_emphasis []byte
for _, char := range []byte(msg) {
var ok bool
var emp []byte

if _, ok = emphasis_unsupported[char]; ok {
buf = append(buf, char)
continue
}

if emp, ok = emphasis_supported[char]; !ok {
buf = append(buf, char)
continue
}

// Reset formatting
if char == '\x0f' {
// Close off any current emphasis
for _, c := range cur_emphasis {
buf = append(buf, emphasis_supported[c]...)
}
cur_emphasis = nil
continue
}

buf = append(buf, emp...)

// Remove closing emphasis.
found := false
var new_emphasis []byte
for _, c := range cur_emphasis {
if !found && c == char {
found = true
continue
}
new_emphasis = append(new_emphasis, c)
}

if found {
cur_emphasis = new_emphasis
continue
}

cur_emphasis = append([]byte{char}, cur_emphasis...)
}

switch {
case emphasis == "b":
buf = append(buf, '*', '*')
case emphasis == "i":
buf = append(buf, '_')
case emphasis == "bi":
buf = append(buf, '_', '*', '*')
case emphasis == "m":
buf = append(buf, '`')
for _, c := range cur_emphasis {
buf = append(buf, emphasis_supported[c]...)
}
cur_emphasis = nil

return string(buf)
}

0 comments on commit fbb00d7

Please sign in to comment.