mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-09-21 06:20:08 +00:00
fixed bug in textstyleparser
* the textstyleparser now properly handles `***` and marks the text as bold and italic. see #879
This commit is contained in:
parent
5cc379fde0
commit
a08db23a31
@ -23,6 +23,7 @@ const (
|
|||||||
MonoSpaceBegin = 6
|
MonoSpaceBegin = 6
|
||||||
StrikethroughBegin = 8
|
StrikethroughBegin = 8
|
||||||
SpoilerBegin = 9
|
SpoilerBegin = 9
|
||||||
|
BoldItalicBegin = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
const EscapeCharacter rune = '\\'
|
const EscapeCharacter rune = '\\'
|
||||||
@ -119,7 +120,14 @@ func (l *TextstyleParser) handleToken(tokenType int, signalCliStylingType string
|
|||||||
} else {
|
} else {
|
||||||
if l.tokens.Peek().Token == tokenType {
|
if l.tokens.Peek().Token == tokenType {
|
||||||
tokenBeginState := l.tokens.Pop()
|
tokenBeginState := l.tokens.Pop()
|
||||||
l.signalCliFormatStrings = append(l.signalCliFormatStrings, strconv.Itoa(tokenBeginState.BeginPos)+":"+strconv.Itoa(getUtf16StringLength(l.fullString)-tokenBeginState.BeginPos)+":"+signalCliStylingType)
|
length := getUtf16StringLength(l.fullString) - tokenBeginState.BeginPos
|
||||||
|
|
||||||
|
if tokenType == BoldItalicBegin {
|
||||||
|
l.signalCliFormatStrings = append(l.signalCliFormatStrings, strconv.Itoa(tokenBeginState.BeginPos)+":"+strconv.Itoa(length)+":"+Bold)
|
||||||
|
l.signalCliFormatStrings = append(l.signalCliFormatStrings, strconv.Itoa(tokenBeginState.BeginPos)+":"+strconv.Itoa(length)+":"+Italic)
|
||||||
|
} else {
|
||||||
|
l.signalCliFormatStrings = append(l.signalCliFormatStrings, strconv.Itoa(tokenBeginState.BeginPos)+":"+strconv.Itoa(length)+":"+signalCliStylingType)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
l.tokens.Push(TokenState{BeginPos: getUtf16StringLength(l.fullString), Token: tokenType})
|
l.tokens.Push(TokenState{BeginPos: getUtf16StringLength(l.fullString), Token: tokenType})
|
||||||
}
|
}
|
||||||
@ -137,14 +145,27 @@ func (l *TextstyleParser) Parse() (string, []string) {
|
|||||||
nextRune := l.peek()
|
nextRune := l.peek()
|
||||||
|
|
||||||
if c == '*' {
|
if c == '*' {
|
||||||
if nextRune == '*' { //Bold
|
if nextRune == '*' {
|
||||||
l.next()
|
l.next()
|
||||||
if prevChar == EscapeCharacter {
|
|
||||||
prevChar = c
|
if l.peek() == '*' { // Check for *** before treating it as **
|
||||||
l.fullString += "**"
|
l.next()
|
||||||
continue
|
|
||||||
|
if prevChar == EscapeCharacter {
|
||||||
|
prevChar = c
|
||||||
|
l.fullString += "***"
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
l.handleToken(BoldItalicBegin, "BOLD_ITALIC")
|
||||||
|
} else {
|
||||||
|
if prevChar == EscapeCharacter {
|
||||||
|
prevChar = c
|
||||||
|
l.fullString += "**"
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
l.handleToken(BoldBegin, Bold)
|
||||||
}
|
}
|
||||||
l.handleToken(BoldBegin, Bold)
|
|
||||||
} else { //Italic
|
} else { //Italic
|
||||||
if prevChar == EscapeCharacter {
|
if prevChar == EscapeCharacter {
|
||||||
prevChar = c
|
prevChar = c
|
||||||
|
|||||||
@ -155,5 +155,11 @@ func TestEscapeNew(t *testing.T) {
|
|||||||
message, signalCliFormatStrings := textstyleParser.Parse()
|
message, signalCliFormatStrings := textstyleParser.Parse()
|
||||||
expectMessageEqual(t, message, "Test ** * ~ Escape")
|
expectMessageEqual(t, message, "Test ** * ~ Escape")
|
||||||
expectFormatStringsEqual(t, signalCliFormatStrings, []string{})
|
expectFormatStringsEqual(t, signalCliFormatStrings, []string{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBoldItalic(t *testing.T) {
|
||||||
|
textstyleParser := NewTextstyleParser("***Bold Italic Text***")
|
||||||
|
message, signalCliFormatStrings := textstyleParser.Parse()
|
||||||
|
expectMessageEqual(t, message, "Bold Italic Text")
|
||||||
|
expectFormatStringsEqual(t, signalCliFormatStrings, []string{"0:16:BOLD", "0:16:ITALIC"})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user