Skip to content

Commit

Permalink
update: 适配任意消息元素的可能状况,除纯文本以外所有元素都可以拥有任意属性以及子元素且可以获取,同时删除了输出内容
Browse files Browse the repository at this point in the history
  • Loading branch information
WindowsSov8forUs committed Apr 12, 2024
1 parent e9c4489 commit 8939bc5
Show file tree
Hide file tree
Showing 11 changed files with 550 additions and 718 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
}
78 changes: 66 additions & 12 deletions pkg/message/message_element_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,7 +46,7 @@ func (e *MessageElementAt) Tag() string {
}

func (e *MessageElementAt) Stringify() string {
result := "<" + e.Tag()
result := ""
if e.Id != "" {
result += ` id="` + escape(e.Id, true) + `"`
}
Expand All @@ -57,21 +59,39 @@ func (e *MessageElementAt) Stringify() string {
if 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="` + escape(e.Id, true) + `"`
}
if e.Name != "" {
result += ` name="` + escape(e.Name, true) + `"`
}
return result + "/>"
result += e.stringifyAttributes()
childrenStr := e.stringifyChildren()
if childrenStr == "" {
return `<` + e.Tag() + 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="` + 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 8939bc5

Please sign in to comment.