1
0
mirror of https://github.com/openshift/installer.git synced 2026-02-05 06:46:36 +01:00
Files
installer/docs/user/aws/images/relink-dia.py
W. Trevor King 0e4b04441f docs/user/aws/images: Dia source for UPI arch diagram
Dia is from Stephen Cuppett, replacing the PNG he'd submitted via
39a926a918 (Adding initial user doc/guide & materials for UPI AWS
installation, 2019-03-12, #1408).

We aren't using the full file with all the layers yet, but I'm
building it anyway because folks without Dia may still want to look at
it ;).

SVGs generated with:

  $ dia --version
  Dia version 0.97.3, compiled 18:02:21 Feb 11 2017

relink-dia.py embeds the referenced icons in the SVG with def and use
[1,2] to avoid icon URIs like:

  file:///.../openshift/installer/docs/user/aws/images/./AWS-Architecture-Icons_SVG/Light-BG/_General%20AWS/AWS-General_AWS-Cloud_light-bg.svg

Ideally Dia would have a way to do this sort of thing automatically
with a command-line switch, but if it does, I can't find it.

[1]: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/def
[2]: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use
2019-04-05 14:23:03 -07:00

48 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Replace file:///... references with relative links and add the targets to Git.
import base64
import os
import sys
import urllib.parse
import xml.etree.ElementTree
def slug(uri):
return os.path.basename(uri)
def get_def(path):
tree = xml.etree.ElementTree.parse(path)
root = tree.getroot()
root.set('id', slug(path))
return root
def relink(path):
images = set()
tree = xml.etree.ElementTree.parse(path)
root = tree.getroot()
parents = {c: p for p in tree.getiterator() for c in p}
defs = xml.etree.ElementTree.Element('{http://www.w3.org/2000/svg}defs')
root.insert(0, defs)
for image in list(root.findall('{http://www.w3.org/2000/svg}image')):
uri = urllib.parse.unquote(image.get('{http://www.w3.org/1999/xlink}href')).split('/./', 1)[-1]
if uri not in images:
defs.append(get_def(path=uri))
images.add(uri)
parent = parents[image]
parent_index = list(parent).index(image)
parent.remove(image)
attrib = dict(image.items())
attrib['{http://www.w3.org/1999/xlink}href'] = '#' + slug(uri)
parent.insert(
parent_index,
xml.etree.ElementTree.Element('{http://www.w3.org/2000/svg}use', attrib=attrib))
tree.write(path)
for path in sys.argv[1:]:
relink(path=path)