--- /dev/null
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+visio2sodipodi - pas de doc cette fois-ci... :-P
+
+Copyright : Agence universitaire de la Francophonie
+Licence : GNU General Public Licence, version 2
+Auteur : Jean Christophe André
+Date de création : 13 octobre 2009
+"""
+import sys
+import re
+from xml.dom import minidom
+
+SODIPODI_NS = "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+
+if len(sys.argv) != 2 or not sys.argv[1].endswith('.svg'):
+ print "Usage: %s <filename.svg>" % sys.argv[0]
+ sys.exit(1)
+filename = sys.argv[1]
+
+doc = minidom.parse(filename)
+
+svg = doc.getElementsByTagName('svg')
+if len(svg) != 1:
+ print u"Ce document n'est pas au format SVG."
+ sys.exit(1)
+svg = svg[0]
+
+if not svg.hasAttribute('xmlns:v'):
+ print u"Ce document n'est pas au format MS–Visio."
+ sys.exit(1)
+
+if svg.hasAttribute('xmlns:sodipodi'):
+ print u"Ce document est déjà compatible 'sodipodi'."
+ sys.exit(1)
+# ajout de l'espace de noms SODIPODI_NS
+svg.setAttribute('xmlns:sodipodi', SODIPODI_NS)
+
+default_class = svg.getAttribute('class')
+if default_class == '':
+ print u"Ce document ne contient pas de style par défaut ??"
+ sys.exit(1)
+
+style = svg.getElementsByTagName('style')
+if len(style) != 1:
+ print u"Ce document contient plusieurs sections de styles ??"
+ sys.exit(1)
+style = style[0]
+
+# ajout de "text-align:center" au style .st7
+for child in style.childNodes:
+ if child.nodeType == minidom.Node.CDATA_SECTION_NODE:
+ value = child.nodeValue
+ pat = r'\.st7 {([^}]*)}'
+ rep = r'.st7 {\1;text-align:center}'
+ value = re.sub(pat, rep, value, 1)
+ pat = r'(\.st7 {[^}]*font-size):[^;]*(;[^}]*})'
+ rep = r'\1:0.833336em\2'
+ value = re.sub(pat, rep, value, 1)
+ pat = r'(\.%s {[^}]*font-size):[^;]*(;[^}]*})' % default_class
+ rep = r'\1:8\2'
+ value = re.sub(pat, rep, value, 1)
+ child.nodeValue = value
+ break
+
+for text in svg.getElementsByTagName('text'):
+ childNodes = [child for child in text.childNodes]
+ for child in childNodes:
+ # encadrer tout TEXT dans un <text> par un <tspan>
+ if child.nodeType == minidom.Node.TEXT_NODE:
+ tspan = doc.createElement('tspan')
+ tspan.appendChild(doc.createTextNode(child.nodeValue))
+ text.replaceChild(tspan, child)
+ elif child.nodeType == minidom.Node.ELEMENT_NODE:
+ # virer les <v:newlineChar/>
+ if child.nodeName == 'v:newlineChar':
+ text.removeChild(child)
+ # y compris à l'intérieur des tspan
+ elif child.nodeName == 'tspan':
+ if child.hasAttribute('dy'):
+ tspan = doc.createElement('tspan')
+ tspan.setAttribute('sodipodi:role', 'line')
+ text.insertBefore(tspan, child)
+ node = child.firstChild
+ if node.nodeType == minidom.Node.ELEMENT_NODE and \
+ node.nodeName == 'v:newlineChar':
+ child.removeChild(node)
+ node = child.lastChild
+ if node.nodeType == minidom.Node.ELEMENT_NODE and \
+ node.nodeName == 'v:newlineChar':
+ child.removeChild(node)
+
+file = open(filename.replace('.svg','-new.svg'), 'wb')
+file.write(doc.toxml(encoding=doc.encoding))
+file.close()
+
+sys.exit(0)