Last active
August 17, 2018 13:55
-
-
Save kevinchugh/18279ba9efc039dfc8932366c9ffeba1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
drawing_node_raw = " | |
<w:drawing> | |
<wp:anchor behindDoc=\"0\" distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\" simplePos=\"0\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\" relativeHeight=\"2\"> | |
<wp:simplePos x=\"0\" y=\"0\"/> | |
<wp:positionH relativeFrom=\"column\"> | |
<wp:align>center</wp:align> | |
</wp:positionH> | |
<wp:positionV relativeFrom=\"paragraph\"> | |
<wp:posOffset>635</wp:posOffset> | |
</wp:positionV> | |
<wp:extent cx=\"985520\" cy=\"492760\"/> | |
<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/> | |
<wp:wrapSquare wrapText=\"largest\"/> | |
<wp:docPr id=\"1\" name=\"Image2\" descr=\"\"/> | |
<wp:cNvGraphicFramePr> | |
<a:graphicFrameLocks xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" noChangeAspect=\"1\"/> | |
</wp:cNvGraphicFramePr> | |
<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"> | |
<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\"> | |
<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\"> | |
<pic:nvPicPr> | |
<pic:cNvPr id=\"1\" name=\"Image2\" descr=\"\"/> | |
<pic:cNvPicPr> | |
<a:picLocks noChangeAspect=\"1\" noChangeArrowheads=\"1\"/> | |
</pic:cNvPicPr> | |
</pic:nvPicPr> | |
<pic:blipFill> | |
<a:blip r:embed=\"rId2\"/> | |
<a:stretch> | |
<a:fillRect/> | |
</a:stretch> | |
</pic:blipFill> | |
<pic:spPr bwMode=\"auto\"> | |
<a:xfrm> | |
<a:off x=\"0\" y=\"0\"/> | |
<a:ext cx=\"985520\" cy=\"492760\"/> | |
</a:xfrm> | |
<a:prstGeom prst=\"rect\"> | |
<a:avLst/> | |
</a:prstGeom> | |
</pic:spPr> | |
</pic:pic> | |
</a:graphicData> | |
</a:graphic> | |
</wp:anchor> | |
</w:drawing>" | |
def find_root_most_children xml, basket | |
# recursive function to find rootmost xml nodes containing mustache template vars | |
# note, this is only for image files, the others should already have been replaced | |
# basket is the array you want to collect the nodes into | |
xml.children.each do |child| | |
if child.content.include? "{{" and child.content.include? "}}" | |
if child.children.empty? | |
# we are done | |
basket << child | |
else | |
# otherwise, recurse | |
find_root_most_children child, basket | |
end | |
end | |
end | |
basket | |
end | |
node = Nokogiri::XML::DocumentFragment.parse drawing_node_raw | |
xml = Nokogiri::XML.parse File.open("/tmp/document.xml").read | |
basket = [] | |
find_root_most_children xml,basket | |
basket[1].parent.replace node | |
File.open("/tmp/document2.xml","w+").write(xml.to_xml.to_s) | |
`cp /tmp/document2.xml ~/Documents` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment