Skip to content

Instantly share code, notes, and snippets.

@jbn
Created January 5, 2018 01:19
Show Gist options
  • Save jbn/2afd54b0135c2a65ac906ec4c9d58a70 to your computer and use it in GitHub Desktop.
Save jbn/2afd54b0135c2a65ac906ec4c9d58a70 to your computer and use it in GitHub Desktop.
from lxml.etree import fromstring, tostring
def hatch_safe_svg(data):
# rsvg-convert doesn't resolve <pattern> fills from #fragment
# URIs if they occur downfile from the usage. This makes sure
# definitions come first.
doc = fromstring(data)
defs = [el for el in doc.getchildren()
if el.tag == "{http://www.w3.org/2000/svg}defs"]
for defn in defs:
doc.remove(defn)
doc.insert(0, defn)
return tostring(doc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment