extract date parser to a new file

This commit is contained in:
Nazar Kanaev
2021-04-04 20:45:13 +01:00
parent cf5856bdf7
commit 0828d6782e
2 changed files with 36 additions and 34 deletions

35
src/parser/util.go Normal file
View File

@@ -0,0 +1,35 @@
package parser
import (
"encoding/xml"
"io"
"regexp"
"strings"
"golang.org/x/net/html/charset"
)
func firstNonEmpty(vals ...string) string {
for _, val := range vals {
valTrimmed := strings.TrimSpace(val)
if len(valTrimmed) > 0 {
return valTrimmed
}
}
return ""
}
var linkRe = regexp.MustCompile(`(https?:\/\/\S+)`)
func plain2html(text string) string {
text = linkRe.ReplaceAllString(text, `<a href="$1">$1</a>`)
text = strings.ReplaceAll(text, "\n", "<br>")
return text
}
func xmlDecoder(r io.Reader) *xml.Decoder {
decoder := xml.NewDecoder(r)
decoder.Strict = false
decoder.CharsetReader = charset.NewReaderLabel
return decoder
}