Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
bobiverse authored Jul 3, 2024
2 parents 6217df9 + 26ed7dd commit 55b4c71
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ImageProcesser.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func processImage(img *Image) (imgXMLStr string, err error) {
Space: "",
Local: "Default",
},
Attrs: []xml.Attr{
Attrs: []*xml.Attr{

Check failure on line 57 in ImageProcesser.go

View workflow job for this annotation

GitHub Actions / build

cannot use []*xml.Attr{…} (value of type []*xml.Attr) as []xml.Attr value in struct literal
{Name: xml.Name{Space: "", Local: "Extension"}, Value: imgExt},
{Name: xml.Name{Space: "", Local: "ContentType"}, Value: "image/" + imgExt},
},
Expand All @@ -78,7 +78,7 @@ func processImage(img *Image) (imgXMLStr string, err error) {
Space: "",
Local: "Relationship",
},
Attrs: []xml.Attr{
Attrs: []*xml.Attr{

Check failure on line 81 in ImageProcesser.go

View workflow job for this annotation

GitHub Actions / build

cannot use []*xml.Attr{…} (value of type []*xml.Attr) as []xml.Attr value in struct literal
{Name: xml.Name{Space: "", Local: "Id"}, Value: rid},
{Name: xml.Name{Space: "", Local: "Type"}, Value: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"},
{Name: xml.Name{Space: "", Local: "Target"}, Value: "media/" + imgPath},
Expand Down
43 changes: 42 additions & 1 deletion Template.stage.funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,48 @@ func (t *Template) replaceSingleParams(xnode *xmlNode, triggerParamOnly bool) {
}
t.replaceAndRunTrigger(p, n, triggerParamOnly)
}
xnodeList = append(xnodeList, n)

Check failure on line 156 in Template.stage.funcs.go

View workflow job for this annotation

GitHub Actions / build

undefined: xnodeList
})
paramAbsoluteKeyMap := map[string]*Param{}

Check failure on line 158 in Template.stage.funcs.go

View workflow job for this annotation

GitHub Actions / build

no new variables on left side of :=
t.params.Walk(func(p *Param) {
for _, v := range replaceAttr {

Check failure on line 160 in Template.stage.funcs.go

View workflow job for this annotation

GitHub Actions / build

undefined: replaceAttr
v.Value = string(p.replaceIn([]byte(v.Value)))
}
if p.Type != StringParam && p.Type != ImageParam {
return
}
paramAbsoluteKeyMap[p.AbsoluteKey] = p
})
for i := range xnodeList {

Check failure on line 168 in Template.stage.funcs.go

View workflow job for this annotation

GitHub Actions / build

undefined: xnodeList
n := xnodeList[i]

Check failure on line 169 in Template.stage.funcs.go

View workflow job for this annotation

GitHub Actions / build

undefined: xnodeList
for _, key := range n.GetContentPrefixList() {
p, ok := paramAbsoluteKeyMap[key]
if !ok {
continue
}
t.replaceAndRunTrigger(p, n, triggerParamOnly)
}
}
}

func (t *Template) replaceAndRunTrigger(p *Param, n *xmlNode, triggerParamOnly bool) {
// Trigger: does placeholder have trigger
if p.Trigger = p.extractTriggerFrom(n.Content); p.Trigger != nil {
// if
defer func() {
p.RunTrigger(n)
}()
}
if triggerParamOnly {
return
}
// Repalce by type
switch p.Type {
case StringParam:
t.replaceTextParam(n, p)
case ImageParam:
t.replaceImageParams(n, p)
}
}

func (t *Template) replaceAndRunTrigger(p *Param, n *xmlNode, triggerParamOnly bool) {

Check failure on line 200 in Template.stage.funcs.go

View workflow job for this annotation

GitHub Actions / build

method Template.replaceAndRunTrigger already declared at ./Template.stage.funcs.go:180:20
Expand Down Expand Up @@ -191,7 +232,7 @@ func (t *Template) enhanceMarkup(xnode *xmlNode) {
return
}
// n.XMLName.Local = "w-item"
n.Attrs = append(n.Attrs, xml.Attr{
n.Attrs = append(n.Attrs, &xml.Attr{
Name: xml.Name{Local: "list-id"},
Value: listID,
})
Expand Down
48 changes: 48 additions & 0 deletions xml.node.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@ func (xnode xmlNode) GetContentPrefixList() (ret []string) {
return
}

func (xnode xmlNode) GetContentPrefixList() (ret []string) {

Check failure on line 106 in xml.node.go

View workflow job for this annotation

GitHub Actions / build

method xmlNode.GetContentPrefixList already declared at ./xml.node.go:81:22
var record strings.Builder
start := false
length := len(xnode.Content)
for i, v := range xnode.Content {
if i == 0 {
continue
}

if v == '{' && xnode.Content[i-1] == '{' {
start = true
continue
}
if start && (v == ' ' || (v == '}' && length-1 > i && xnode.Content[i+1] == '}')) {
ret = append(ret, record.String())
record.Reset()
start = false
}
if start {
record.WriteByte(v)
}
}
return
}

func (xnode xmlNode) ContentHasPrefix(str string) bool {
splitContent := bytes.Split(xnode.Content, []byte(str))
if len(splitContent) == 1 {
Expand Down Expand Up @@ -213,6 +238,29 @@ func (xnode *xmlNode) walkWithEnd(fn func(*xmlNode) bool) {
})
}

// fn return true ,end walk
func (xnode *xmlNode) WalkWithEnd(fn func(*xmlNode) bool) {

Check failure on line 242 in xml.node.go

View workflow job for this annotation

GitHub Actions / build

method xmlNode.WalkWithEnd already declared at ./xml.node.go:225:23
// Using index to iterate nodes instead of for-range to process dynamic nodes
for i := 0; i < len(xnode.Nodes); i++ {
n := xnode.Nodes[i]

if n == nil {
continue
}

end := fn(n) // do your custom stuff

if end {
continue
}

if n.Nodes != nil {
// continue only if have deeper nodes
n.WalkWithEnd(fn)
}
}
}

// Walk down all nodes and do custom stuff with given function
func (xnode *xmlNode) WalkTree(depth int, fn func(int, *xmlNode)) {
if xnode.childFirst == nil {
Expand Down

0 comments on commit 55b4c71

Please sign in to comment.