2 # -*- coding: utf-8 -*-
4 visio2sodipodi - pas de doc cette fois-ci... :-P
6 Copyright : Agence universitaire de la Francophonie
7 Licence : GNU General Public Licence, version 2
8 Auteur : Jean Christophe André
9 Date de création : 13 octobre 2009
13 from xml
.dom
import minidom
15 SODIPODI_NS
= "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
17 if len(sys
.argv
) != 2 or not sys
.argv
[1].endswith('.svg'):
18 print "Usage: %s <filename.svg>" % sys
.argv
[0]
20 filename
= sys
.argv
[1]
22 doc
= minidom
.parse(filename
)
24 svg
= doc
.getElementsByTagName('svg')
26 print u
"Ce document n'est pas au format SVG."
30 if not svg
.hasAttribute('xmlns:v'):
31 print u
"Ce document n'est pas au format MS–Visio."
34 if svg
.hasAttribute('xmlns:sodipodi'):
35 print u
"Ce document est déjà compatible 'sodipodi'."
37 # ajout de l'espace de noms SODIPODI_NS
38 svg
.setAttribute('xmlns:sodipodi', SODIPODI_NS
)
40 default_class
= svg
.getAttribute('class')
41 if default_class
== '':
42 print u
"Ce document ne contient pas de style par défaut ??"
45 style
= svg
.getElementsByTagName('style')
47 print u
"Ce document contient plusieurs sections de styles ??"
51 # ajout de "text-align:center" au style .st7
52 for child
in style
.childNodes
:
53 if child
.nodeType
== minidom
.Node
.CDATA_SECTION_NODE
:
54 value
= child
.nodeValue
55 pat
= r
'\.st7 {([^}]*)}'
56 rep
= r
'.st7 {\1;text-align:center}'
57 value
= re
.sub(pat
, rep
, value
, 1)
58 pat
= r
'(\.st7 {[^}]*font-size):[^;]*(;[^}]*})'
59 rep
= r
'\1:0.833336em\2'
60 value
= re
.sub(pat
, rep
, value
, 1)
61 pat
= r
'(\.%s {[^}]*font-size):[^;]*(;[^}]*})' % default_class
63 value
= re
.sub(pat
, rep
, value
, 1)
64 child
.nodeValue
= value
67 for text
in svg
.getElementsByTagName('text'):
68 childNodes
= [child
for child
in text
.childNodes
]
69 for child
in childNodes
:
70 # encadrer tout TEXT dans un <text> par un <tspan>
71 if child
.nodeType
== minidom
.Node
.TEXT_NODE
:
72 tspan
= doc
.createElement('tspan')
73 tspan
.appendChild(doc
.createTextNode(child
.nodeValue
))
74 text
.replaceChild(tspan
, child
)
75 elif child
.nodeType
== minidom
.Node
.ELEMENT_NODE
:
76 # virer les <v:newlineChar/>
77 if child
.nodeName
== 'v:newlineChar':
78 text
.removeChild(child
)
79 # y compris à l'intérieur des tspan
80 elif child
.nodeName
== 'tspan':
81 if child
.hasAttribute('dy'):
82 tspan
= doc
.createElement('tspan')
83 tspan
.setAttribute('sodipodi:role', 'line')
84 text
.insertBefore(tspan
, child
)
85 node
= child
.firstChild
86 if node
.nodeType
== minidom
.Node
.ELEMENT_NODE
and \
87 node
.nodeName
== 'v:newlineChar':
88 child
.removeChild(node
)
89 node
= child
.lastChild
90 if node
.nodeType
== minidom
.Node
.ELEMENT_NODE
and \
91 node
.nodeName
== 'v:newlineChar':
92 child
.removeChild(node
)
94 file = open(filename
.replace('.svg','-new.svg'), 'wb')
95 file.write(doc
.toxml(encoding
=doc
.encoding
))