I've been trying to figure this out for a few days and I've kind of exhausted even the most effective of google searching. What I've been trying to do is to open a file of the type HTML and with Go's library (http://golang.org/x/net/html) modify the img tags and their source to a known directory and set of files. So far I've been able to find the elements using this,
//Open the file and return a variable called file.
file, _ = os.Open(file.Name())
//Create the doc
doc, err := html.Parse(file)
//Check for err when generating the doc
check(err)
//Look for tags with img using an anonymous function.
var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "img" {
for _, img := range n.Attr {
if img.Key == "src" {
str := breakdownURL(img.Val) //Gets the ../../resource/(thing.h23.jpg) <-- That
//Creating a static address to add to the dynamic one
address := filepath.Join(filepath.Join("Resources", getFileNotExt(file)), str)
img.Val = address
break
}
}
}
for at := n.FirstChild; at != nil; at = at.NextSibling {
f(at)
}
}
f(doc)
That's been able to find the elements and append the correct directory but it's only modifying this doc file. I have no clue how to append it to the actual file. The only thought that I have is opening the doc as some kind of writing way and copying the new data from the doc to the file. Any help is greatly appreciated! Thank you so much for taking your time :).