Lower complexity of character parsing function
This commit is contained in:
parent
3a33853c4a
commit
4f557067df
@ -1,9 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/animenotifier/arn"
|
"github.com/animenotifier/arn"
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseCharacterDescription(input string) (output string, attributes []*arn.CharacterAttribute) {
|
func parseCharacterDescription(input string) (output string, attributes []*arn.CharacterAttribute) {
|
||||||
@ -25,14 +27,6 @@ func parseCharacterDescription(input string) (output string, attributes []*arn.C
|
|||||||
var lastAttribute *arn.CharacterAttribute
|
var lastAttribute *arn.CharacterAttribute
|
||||||
|
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
// line = strings.Replace(line, " (\n)", "", -1)
|
|
||||||
|
|
||||||
// Remove all kinds of starting and ending parantheses.
|
|
||||||
if strings.HasPrefix(line, "(") {
|
|
||||||
line = strings.TrimPrefix(line, "(")
|
|
||||||
line = strings.TrimSuffix(line, ")")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(line, ":") {
|
if !strings.Contains(line, ":") {
|
||||||
// Remove list indicators
|
// Remove list indicators
|
||||||
line = strings.TrimPrefix(line, "- ")
|
line = strings.TrimPrefix(line, "- ")
|
||||||
@ -50,10 +44,9 @@ func parseCharacterDescription(input string) (output string, attributes []*arn.C
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(line, ":")
|
name, value := parseAttribute(line)
|
||||||
name := strings.TrimSpace(parts[0])
|
|
||||||
value := strings.TrimSpace(parts[1])
|
|
||||||
|
|
||||||
|
if name != "" && value != "" {
|
||||||
lastAttribute = &arn.CharacterAttribute{
|
lastAttribute = &arn.CharacterAttribute{
|
||||||
Name: name,
|
Name: name,
|
||||||
Value: value,
|
Value: value,
|
||||||
@ -61,6 +54,7 @@ func parseCharacterDescription(input string) (output string, attributes []*arn.C
|
|||||||
|
|
||||||
attributes = append(attributes, lastAttribute)
|
attributes = append(attributes, lastAttribute)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -91,8 +85,31 @@ func parseCharacterDescription(input string) (output string, attributes []*arn.C
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Is it an attribute?
|
// Is it an attribute?
|
||||||
if strings.Contains(paragraph, ":") {
|
name, value := parseAttribute(paragraph)
|
||||||
parts := strings.Split(paragraph, ":")
|
|
||||||
|
if name != "" && value != "" {
|
||||||
|
attributes = append(attributes, &arn.CharacterAttribute{
|
||||||
|
Name: name,
|
||||||
|
Value: value,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
finalParagraphs = append(finalParagraphs, paragraph)
|
||||||
|
}
|
||||||
|
|
||||||
|
output = strings.Join(finalParagraphs, "\n\n")
|
||||||
|
output = strings.TrimSpace(output)
|
||||||
|
|
||||||
|
return output, attributes
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseAttribute(line string) (string, string) {
|
||||||
|
if !strings.Contains(line, ":") {
|
||||||
|
return "", ""
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(line, ":")
|
||||||
name := strings.TrimSpace(parts[0])
|
name := strings.TrimSpace(parts[0])
|
||||||
value := strings.TrimSpace(parts[1])
|
value := strings.TrimSpace(parts[1])
|
||||||
|
|
||||||
@ -110,54 +127,19 @@ func parseCharacterDescription(input string) (output string, attributes []*arn.C
|
|||||||
value = strings.TrimSuffix(value, "]")
|
value = strings.TrimSuffix(value, "]")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(name, "(") && strings.HasSuffix(value, ")") {
|
||||||
|
name = strings.TrimPrefix(name, "(")
|
||||||
|
value = strings.TrimSuffix(value, ")")
|
||||||
|
}
|
||||||
|
|
||||||
if name == "source" || name == "sources" {
|
if name == "source" || name == "sources" {
|
||||||
name = "Source"
|
name = "Source"
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(name) < 25 && len(value) < 40 && !strings.HasSuffix(value, ".") {
|
if len(name) > 25 || len(value) > 40 || strings.HasSuffix(value, ".") {
|
||||||
// fmt.Println(color.GreenString(name), color.YellowString(value))
|
return "", ""
|
||||||
attributes = append(attributes, &arn.CharacterAttribute{
|
|
||||||
Name: name,
|
|
||||||
Value: value,
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
finalParagraphs = append(finalParagraphs, paragraph)
|
fmt.Println(color.GreenString(name), color.YellowString(value))
|
||||||
|
return name, value
|
||||||
// originalLine := line
|
|
||||||
|
|
||||||
// line = strings.TrimSpace(line)
|
|
||||||
|
|
||||||
// colonPos := strings.Index(line, ":")
|
|
||||||
|
|
||||||
// // If a colon has not been found or the colon is too far,
|
|
||||||
// // treat it as a normal line.
|
|
||||||
// if colonPos == -1 || colonPos < 2 || colonPos > 25 {
|
|
||||||
// finalLines = append(finalLines, originalLine)
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
|
|
||||||
// key := line[:colonPos]
|
|
||||||
// value := line[colonPos+1:]
|
|
||||||
|
|
||||||
// value = strings.TrimSpace(value)
|
|
||||||
|
|
||||||
// if key == "source" {
|
|
||||||
// key = "Source"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// attributes = append(attributes, &arn.CharacterAttribute{
|
|
||||||
// Name: key,
|
|
||||||
// Value: value,
|
|
||||||
// })
|
|
||||||
|
|
||||||
// fmt.Println(color.CyanString(key), color.YellowString(value))
|
|
||||||
}
|
|
||||||
|
|
||||||
output = strings.Join(finalParagraphs, "\n\n")
|
|
||||||
output = strings.TrimSpace(output)
|
|
||||||
|
|
||||||
return output, attributes
|
|
||||||
}
|
}
|
||||||
|
@ -36,12 +36,12 @@ func Profile(ctx *aero.Context, viewUser *arn.User) string {
|
|||||||
"og:image": viewUser.AvatarLink("large"),
|
"og:image": viewUser.AvatarLink("large"),
|
||||||
"og:url": "https://" + ctx.App.Config.Domain + viewUser.Link(),
|
"og:url": "https://" + ctx.App.Config.Domain + viewUser.Link(),
|
||||||
"og:site_name": "notify.moe",
|
"og:site_name": "notify.moe",
|
||||||
"og:description": viewUser.Introduction,
|
"og:description": utils.CutLongDescription(viewUser.Introduction),
|
||||||
"og:type": "profile",
|
"og:type": "profile",
|
||||||
"profile:username": viewUser.Nick,
|
"profile:username": viewUser.Nick,
|
||||||
},
|
},
|
||||||
Meta: map[string]string{
|
Meta: map[string]string{
|
||||||
"description": viewUser.Introduction,
|
"description": utils.CutLongDescription(viewUser.Introduction),
|
||||||
"keywords": viewUser.Nick + ",profile",
|
"keywords": viewUser.Nick + ",profile",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user