Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique ids #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/svgutils/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
except ImportError:
from io import StringIO

import util

SVG_NAMESPACE = "http://www.w3.org/2000/svg"
SVG = "{%s}" % SVG_NAMESPACE
NSMAP = {None : SVG_NAMESPACE}
Expand Down Expand Up @@ -54,6 +56,12 @@ def __init__(self, element_list):

class SVGFigure(object):
def __init__(self, width=None, height=None):
# create a unique id
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, you could just use a class variable here, like

class SVGFigure(object):
    maxid = 0
    def __init__(..):

if not hasattr(SVGFigure, 'maxid'):
SVGFigure.maxid = 0
self.id = SVGFigure.maxid + 1
SVGFigure.maxid = self.id

self.root = etree.Element(SVG+"svg",nsmap=NSMAP)
self.root.set("version", "1.1")
if width or height:
Expand Down Expand Up @@ -96,13 +104,19 @@ def set_size(self, size):
self.root.set('width', w)
self.root.set('height', h)

def fromfile(fname):
def fromfile(fname, idprefix=None):
fig = SVGFigure()
fid = open(fname)
svg_file = etree.parse(fid)
fid.close()

# make id's unique in order to avoid clashes
if idprefix is None:
idprefix = 'inst%d_' % fig.id
fig_root = util.svg_id_prepend(svg_file.getroot(), prefix=idprefix)

fig.root = fig_root

fig.root = svg_file.getroot()
return fig

def fromstring(text):
Expand Down
25 changes: 25 additions & 0 deletions src/svgutils/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import copy
import re

def svg_id_prepend(root, prefix):
"""Append prefix to id attributes and url(#id) references"""

root = copy.deepcopy(root)

id_attributes = {}
## Replace id attributes
for e in root.findall("*//*[@id]"):
idvalue = e.attrib['id']
newidvalue = prefix + idvalue
e.attrib['id'] = newidvalue

id_attributes[idvalue] = newidvalue

## Update references
for e in root.xpath("//*[contains(@*, 'url(#')]"):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'url(#' is not enough. SVG also allows for simple references (like '#id'). I think it might be better to look for '#id' references for each renamed id

for attr, value in e.attrib.items():
matchobj = re.match('url\(#(.*)\)', value)
if matchobj and matchobj.groups()[0] in id_attributes:
e.attrib[attr] = 'url(#%s)' % id_attributes[matchobj.groups()[0]]

return root