Skip to content

Instantly share code, notes, and snippets.

Created June 17, 2013 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5800488 to your computer and use it in GitHub Desktop.
Save anonymous/5800488 to your computer and use it in GitHub Desktop.
replace text
getPage("/content/acs1/en/pressroom/presspacs/2013/acs-presspac-march-20-2013").recurse{ p->
p.node.recurse{ n ->
def txtNode = n.get("text")
if(txtNode?.contains(p.name)){
def xml = new XmlSlurper().parseText(txtNode)?.childNodes()?.each(){
if(it.name =="img"){
def src = it.attributes().get("src")
println 'Page NAME : ' + p.name
println ' IMG TAG SRC : ' + src
println ' PARENT name ' + p.node.parent.name
def oldPath = "presspacs/" + p.name
def newPath = "presspacs/" + p.node.parent.name + "/" + p.name
println ' REPLACING.... : ' + oldPath +" with " + newPath
def repTxtNode = txtNode.replace(oldPath,newPath)
n.set("text",repTxtNode)
}
}
}
println 'SAVING SESSION'
session.save()
}
}
@jdorrance
Copy link

getPage("/content/acs1/en/pressroom/presspacs").recurse{ p->
p.node.recurse{ n ->
def txtNode = n.get("text")
if(txtNode?.contains('src="/content/acs1/en/pressroom/presspacs/' + p.name) && p.parent.name != "presspacs"){
println ( 'Page NAME : ' + p.name)
def oldPath = 'src="/content/acs1/en/pressroom/presspacs/' + p.name
def newPath = 'src="/content/acs1/en/pressroom/presspacs/' + p.parent.name + "/" + p.name
println(' REPLACING.... : ' + oldPath +" with " + newPath)
def repTxtNode = txtNode.replaceAll(oldPath,newPath)
n.set("text",repTxtNode)
println ('new txt node : ' + repTxtNode)
println ( ' SAVING SESSION')
session.save()
}else{
println("skipping " + n.path + " , does not match filter")
}

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment