Skip to content

Commit

Permalink
Merge pull request #1 from WindowsSov8forUs/master
Browse files Browse the repository at this point in the history
add: 完善元素的解析
  • Loading branch information
dezhishen committed Apr 12, 2024
2 parents 086f66e + 70c7d1f commit 07d2aa2
Show file tree
Hide file tree
Showing 12 changed files with 846 additions and 740 deletions.
57 changes: 53 additions & 4 deletions pkg/message/message_element.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package message

import (
"golang.org/x/net/html"
)

type MessageElement interface {
Tag() string
Stringify() string
Expand Down Expand Up @@ -31,9 +35,54 @@ func (e *ChildrenMessageElement) stringifyChildren() string {
return result
}

func (e *ChildrenMessageElement) stringifyByTag(tag string) string {
if e == nil || len(e.Children) == 0 {
return "<" + tag + " />"
func (e *ChildrenMessageElement) parseChildren(n *html.Node) (*ChildrenMessageElement, error) {
var children []MessageElement
err := parseHtmlChildrenNode(n, func(e MessageElement) {
children = append(children, e)
})
if err != nil {
return nil, err
}
result := &ChildrenMessageElement{
Children: children,
}
return result, nil
}

type ExtendAttributes struct {
Attributes map[string]string
}

func (e *ExtendAttributes) AddAttribute(key, value string) *ExtendAttributes {
result := e
if result == nil {
result = &ExtendAttributes{
Attributes: make(map[string]string),
}
}
return "<" + tag + ">" + e.stringifyChildren() + "</" + tag + ">"
result.Attributes[key] = value
return result
}

func (e *ExtendAttributes) Get(key string) (string, bool) {
if e == nil {
return "", false
}
v, ok := e.Attributes[key]
return v, ok
}

func (e *ExtendAttributes) stringifyAttributes() string {
if e == nil || len(e.Attributes) == 0 {
return ""
}
var result string
for k, v := range e.Attributes {
if v == "" {
result += " " + k
} else {
result += " " + k + `="` + escape(v, true) + `"`
}
}
return result
}
94 changes: 74 additions & 20 deletions pkg/message/message_element_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (e *MessageElementText) Tag() string {
}

func (e *MessageElementText) Stringify() string {
return e.Content
return escape(e.Content, true)
}

func (e *MessageElementText) Parse(n *html.Node) (MessageElement, error) {
Expand All @@ -33,6 +33,8 @@ func (e *MessageElementText) Parse(n *html.Node) (MessageElement, error) {

type MessageElementAt struct {
*noAliasMessageElement
*ChildrenMessageElement
*ExtendAttributes
Id string
Name string // 收发 目标用户的名称
Role string // 收发 目标角色
Expand All @@ -44,34 +46,52 @@ func (e *MessageElementAt) Tag() string {
}

func (e *MessageElementAt) Stringify() string {
result := "<" + e.Tag()
result := ""
if e.Id != "" {
result += ` id="` + e.Id + `"`
result += ` id="` + escape(e.Id, true) + `"`
}
if e.Name != "" {
result += ` name="` + e.Name + `"`
result += ` name="` + escape(e.Name, true) + `"`
}
if e.Role != "" {
result += ` role="` + e.Role + `"`
result += ` role="` + escape(e.Role, true) + `"`
}
if e.Type != "" {
result += ` type="` + e.Type + `"`
result += ` type="` + escape(e.Type, true) + `"`
}
return result + " />"
result += e.stringifyAttributes()
childrenStr := e.stringifyChildren()
if childrenStr == "" {
return `<` + e.Tag() + result + `/>`
}
return `<` + e.Tag() + result + `>` + childrenStr + `</` + e.Tag() + `>`
}

func (e *MessageElementAt) Parse(n *html.Node) (MessageElement, error) {
attrMap := attrList2MapVal(n.Attr)
return &MessageElementAt{
result := &MessageElementAt{
Id: attrMap["id"],
Name: attrMap["name"],
Role: attrMap["role"],
Type: attrMap["type"],
}, nil
}
for key, value := range attrMap {
if key != "id" && key != "name" && key != "role" && key != "type" {
result.ExtendAttributes = result.AddAttribute(key, value)
}
}
children, err := result.parseChildren(n)
if err != nil {
return nil, err
}
result.ChildrenMessageElement = children
return result, nil
}

type MessageElementSharp struct {
*noAliasMessageElement
*ChildrenMessageElement
*ExtendAttributes
Id string //收发 目标频道的 ID
Name string //收发 目标频道的名称
}
Expand All @@ -81,26 +101,44 @@ func (e *MessageElementSharp) Tag() string {
}

func (e *MessageElementSharp) Stringify() string {
result := "<" + e.Tag()
result := ""
if e.Id != "" {
result += ` id="` + e.Id + `"`
result += ` id="` + escape(e.Id, true) + `"`
}
if e.Name != "" {
result += ` name="` + e.Name + `"`
result += ` name="` + escape(e.Name, true) + `"`
}
result += e.stringifyAttributes()
childrenStr := e.stringifyChildren()
if childrenStr == "" {
return `<` + e.Tag() + result + `/>`
}
return result + " />"
return `<` + e.Tag() + result + `>` + childrenStr + `</` + e.Tag() + `>`
}

func (e *MessageElementSharp) Parse(n *html.Node) (MessageElement, error) {
attrMap := attrList2MapVal(n.Attr)
return &MessageElementSharp{
result := &MessageElementSharp{
Id: attrMap["id"],
Name: attrMap["name"],
}, nil
}
for key, value := range attrMap {
if key != "id" && key != "name" && key != "role" && key != "type" {
result.ExtendAttributes = result.AddAttribute(key, value)
}
}
children, err := result.parseChildren(n)
if err != nil {
return nil, err
}
result.ChildrenMessageElement = children
return result, nil
}

type MessageElementA struct {
*noAliasMessageElement
*ChildrenMessageElement
*ExtendAttributes
Href string
}

Expand All @@ -109,17 +147,33 @@ func (e *MessageElementA) Tag() string {
}

func (e *MessageElementA) Stringify() string {
result := "<" + e.Tag()
result := ""
if e.Href != "" {
result += ` href="` + e.Href + `"`
result += ` href="` + escape(e.Href, true) + `"`
}
return result + " />"
result += e.stringifyAttributes()
childrenStr := e.stringifyChildren()
if childrenStr == "" {
return `<` + e.Tag() + result + `/>`
}
return `<` + e.Tag() + result + `>` + childrenStr + `</` + e.Tag() + `>`
}
func (e *MessageElementA) Parse(n *html.Node) (MessageElement, error) {
attrMap := attrList2MapVal(n.Attr)
return &MessageElementA{
result := &MessageElementA{
Href: attrMap["href"],
}, nil
}
for key, value := range attrMap {
if key != "href" {
result.ExtendAttributes = result.AddAttribute(key, value)
}
}
children, err := result.parseChildren(n)
if err != nil {
return nil, err
}
result.ChildrenMessageElement = children
return result, nil
}

func init() {
Expand Down
Loading

0 comments on commit 07d2aa2

Please sign in to comment.