package util import ( e "codeberg.org/pebbe/errors" "fmt" "os" "path/filepath" "regexp" "strings" ) var ( p = e.PanicErr reEOL = regexp.MustCompile(`[.!?]['"”’]?$`) reNEOL = regexp.MustCompile(`[.!?]['"”’]?\p{Lu}\p{Ll}+\.?`) reLET = regexp.MustCompile(`\p{Lu}`) reBody = regexp.MustCompile(`<[bB][rR][ /]*>`) ) func HtmlFix(body []byte) []byte { return reBody.ReplaceAllLiteral(body, []byte(" ")) } func HtmlFixString(body string) string { return reBody.ReplaceAllLiteralString(body, " ") } func AddEnd(s string) string { s = strings.TrimSpace(s) if s == "" { return "" } if reEOL.MatchString(s) { return s + "\n" } return s + ".\n" } func FixSpace(s string, opt ...bool) string { s = strings.Join(strings.Fields(s), " ") if len(opt) > 0 && opt[0] { s = reNEOL.ReplaceAllStringFunc(s, func(s1 string) string { if strings.HasSuffix(s1, ".") { // zoals: v.Chr. return s1 } i := reLET.FindStringIndex(s1)[0] return s1[:i] + " " + s1[i:] }) } return s } func MkLock(filename string) { pid := os.Getpid() link := fmt.Sprintf("%s.%d", filepath.Base(filename), pid) p(os.Symlink(link, filename)) name, err := os.Readlink(filename) p(err) if name != link { p(fmt.Errorf("wrong lock name %q, should be %q", name, link)) } }