cardboard-sikuli/srctext.go

40 lines
1019 B
Go

package main
import (
"os"
"regexp"
qt "github.com/mappu/miqt/qt6"
)
// src_to_html converts plaintext Javascript source to HTML with embedded images
// for use with QTextEdit.
func src_to_html(s string) string {
// Convert "" to <img src=""> but only if the target file exists
return regexp.MustCompile(`"[0-9]+\.png"`).ReplaceAllStringFunc(s, func(match string) string {
rawName := match[1 : len(match)-1]
if _, err := os.Stat(rawName); err == nil {
return `<img src="` + rawName + `" />`
}
return match // no good
})
}
// html_to_src converts the mini HTML from a QTextEdit into plaintext Javascript
// source, with images converted back to string literals.
func html_to_src(s string) string {
// Convert <img src=""> to ""
patchedHtmlSrc := regexp.MustCompile(`<img src="[0-9]+\.png" />`).ReplaceAllStringFunc(s, func(match string) string {
return match[9 : len(match)-3]
})
tmp := qt.NewQTextEdit2()
tmp.SetHtml(patchedHtmlSrc)
ret := tmp.ToPlainText()
tmp.Delete()
return ret
}