mirror of
https://github.com/Theodor-Springmann-Stiftung/lenz-web.git
synced 2025-10-29 09:15:33 +00:00
Basic Briefansicht + parsing
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
package functions
|
||||
|
||||
import "html/template"
|
||||
import (
|
||||
"html/template"
|
||||
"strings"
|
||||
|
||||
xmlparsing "github.com/Theodor-Springmann-Stiftung/lenz-web/xml"
|
||||
)
|
||||
|
||||
func FirstLetter(s string) string {
|
||||
if len(s) == 0 {
|
||||
@@ -15,3 +20,41 @@ func Safe(s string) template.HTML {
|
||||
}
|
||||
return template.HTML(s)
|
||||
}
|
||||
|
||||
type LenzParseState struct{}
|
||||
|
||||
func ParseGeneric(s string) string {
|
||||
if len(s) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
builder := strings.Builder{}
|
||||
for elem, err := range xmlparsing.Iterate(s, LenzParseState{}) {
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
switch elem.Token.Type {
|
||||
case xmlparsing.StartElement:
|
||||
builder.WriteString("<div class=\"")
|
||||
builder.WriteString(elem.Token.Name)
|
||||
for key, value := range elem.Token.Attributes {
|
||||
builder.WriteString(" ")
|
||||
builder.WriteString(key)
|
||||
builder.WriteString("-")
|
||||
builder.WriteString(value)
|
||||
}
|
||||
|
||||
builder.WriteString("\">")
|
||||
}
|
||||
|
||||
if elem.Token.Type == xmlparsing.CharData {
|
||||
builder.WriteString(elem.Token.Data)
|
||||
}
|
||||
|
||||
if elem.Token.Type == xmlparsing.EndElement {
|
||||
builder.WriteString("</div>")
|
||||
}
|
||||
}
|
||||
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user