Skip to content

Commit

Permalink
Merge pull request #460 from meshery/MUzairS15/svg-utils
Browse files Browse the repository at this point in the history
add svg utility funcs
  • Loading branch information
MUzairS15 committed Feb 15, 2024
2 parents b93a34a + 90e67ab commit 550a9fb
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
5 changes: 4 additions & 1 deletion utils/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func DryRunHelmChart(chart *chart.Chart) ([]byte, error) {

// Takes in the directory and converts HelmCharts/multiple manifests into a single K8s manifest
func ConvertToK8sManifest(path string, w io.Writer) error {
info, _ := os.Stat(path)
info, err := os.Stat(path)
if err != nil {
return utils.ErrReadDir(err, path)
}
helmChartPath := path
if !info.IsDir() {
helmChartPath, _ = strings.CutSuffix(path, filepath.Base(path))
Expand Down
109 changes: 109 additions & 0 deletions utils/svg_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package utils

import (
"bytes"
"encoding/xml"
"fmt"
"io"
"strconv"
"strings"
)

const XMLTAG = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE svg>"

// UpdateSVGString updates the width and height attributes of an SVG file and returns the modified SVG as a string.
func UpdateSVGString(svgStr string, width, height int) (string, error) {
// Create a reader for the SVG string.
r := strings.NewReader(svgStr)

// Create a decoder for the SVG string.
d := xml.NewDecoder(r)

// Create a buffer for the modified SVG string.
var b bytes.Buffer

// Create an encoder for the buffer.
e := xml.NewEncoder(&b)

// Iterate through the tokens in the SVG string.
for {
// Read the next token.
t, err := d.Token()
if err != nil {
if err == io.EOF {
break
}
return "", err
}

// If the token is an element name, check if it is an "svg" element.
if se, ok := t.(xml.StartElement); ok {
if se.Name.Local == "svg" {
// Set the width and height attributes to the desired values.
updatedH := false
updatedW := false
xmlnsindex := -1
for i, a := range se.Attr {
if a.Name.Local == "width" {
se.Attr[i].Value = strconv.Itoa(width)
updatedW = true
} else if a.Name.Local == "height" {
se.Attr[i].Value = strconv.Itoa(height)
updatedH = true
}
if a.Name.Local == "xmlns" {
xmlnsindex = i
}
}
if !updatedH {
se.Attr = append(se.Attr, xml.Attr{
Name: xml.Name{
Local: "height",
},
Value: strconv.Itoa(height),
})
}
if !updatedW {
se.Attr = append(se.Attr, xml.Attr{
Name: xml.Name{
Local: "width",
},
Value: strconv.Itoa(width),
})
}
if xmlnsindex > -1 {
se.Attr = append(se.Attr[0:xmlnsindex], se.Attr[xmlnsindex+1:]...)
}
} else {
for i, a := range se.Attr {
xmlnsindex := -1
nahbro := 0
if a.Name.Local == "xmlns" {
fmt.Println("found at ", i)
fmt.Println(a.Name)
xmlnsindex = i
nahbro++
}
if xmlnsindex > -1 {
se.Attr = append(se.Attr[0:xmlnsindex], se.Attr[xmlnsindex+1:]...)
}
}
}
t = se
}
// Write the modified token to the buffer.
if err := e.EncodeToken(t); err != nil {
return "", err
}
}

// Flush the encoder's buffer to the buffer.
if err := e.Flush(); err != nil {
return "", err
}
var svg string
if b.String() != "" {
svg = XMLTAG + b.String()
}
return svg, nil
}

0 comments on commit 550a9fb

Please sign in to comment.