2026-01-30 15:56:58 +00:00
package main
import (
2026-08-16 20:07:53 +01:00
"fmt"
2026-08-03 19:39:57 +01:00
"html"
"regexp"
"strings"
2026-08-20 11:01:25 +01:00
"github.com/diamondburned/gotk4/pkg/gtk/v4"
2026-01-30 15:56:58 +00:00
)
2026-08-03 19:39:57 +01:00
var (
urlRE = regexp . MustCompile ( `https?://[^\s]+` )
boldRE = regexp . MustCompile ( `\*([^*]+)\*` )
italicRE = regexp . MustCompile ( `_([^_]+)_` )
strikeRE = regexp . MustCompile ( `~([^~]+)~` )
codeRE = regexp . MustCompile ( "`([^`]+)`" )
)
2026-01-30 15:56:58 +00:00
2026-08-16 20:07:53 +01:00
func XEPToPango ( text string , correction bool ) string {
2026-08-08 13:54:20 +01:00
formatInline := func ( s string ) string {
2026-08-03 19:39:57 +01:00
s = html . EscapeString ( s )
s = boldRE . ReplaceAllString ( s , "<b>$1</b>" )
s = italicRE . ReplaceAllString ( s , "<i>$1</i>" )
s = strikeRE . ReplaceAllString ( s , "<s>$1</s>" )
2026-08-08 13:54:20 +01:00
s = codeRE . ReplaceAllString ( s , `<tt>$1</tt>` )
2026-08-03 19:39:57 +01:00
return s
}
2026-08-08 13:54:20 +01:00
format := func ( s string ) string {
lines := strings . Split ( s , "\n" )
for i , line := range lines {
2026-08-16 20:07:53 +01:00
lines [ i ] = formatInline ( line )
2026-08-08 13:54:20 +01:00
}
return strings . Join ( lines , "\n" )
}
2026-08-03 19:39:57 +01:00
var b strings . Builder
last := 0
for _ , m := range urlRE . FindAllStringIndex ( text , - 1 ) {
start , end := m [ 0 ], m [ 1 ]
b . WriteString ( format ( text [ last : start ]))
url := html . EscapeString ( text [ start : end ])
b . WriteString ( `<a href="` )
b . WriteString ( url )
b . WriteString ( `">` )
b . WriteString ( url )
b . WriteString ( `</a>` )
last = end
}
b . WriteString ( format ( text [ last :]))
2026-08-16 20:07:53 +01:00
str := b . String ()
if correction {
str = fmt . Sprintf ( "%s %s" , str , loadedLocale [ "correction" ])
}
return str
2026-01-30 15:56:58 +00:00
}
2026-08-20 11:01:25 +01:00
func GenerateRetraction ( lab * gtk . Label , retraction Retraction ) {
if retraction . Moderated . OccupantID . ID != "" {
lab . AddCSSClass ( "moderated" )
lab . SetTooltipMarkup ( fmt . Sprintf ( "<span weight='bold'>This message was <span foreground='magenta'>retracted</span> by a moderator.</span>\nReason: '%s'" , html . EscapeString ( retraction . Reason )))
} else {
lab . AddCSSClass ( "retracted" )
lab . SetTooltipMarkup ( fmt . Sprintf ( "<span weight='bold'>This message was retracted.</span>\nReason: '%s'" , html . EscapeString ( retraction . Reason )))
}
}