1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2009 Aurelio A. Heckert, aurium (a) gmail dot com
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 '''
20 import inkex, sys, os, re
22 class InkWebEffect(inkex.Effect):
23 def __init__(self):
24 inkex.Effect.__init__(self)
25 self.reUpdateJS = '/\\*\\s* inkweb.js [^*]* InkWebEffect:AutoUpdate \\s*\\*/'
27 def mustAddInkWebJSCode(self, scriptEl):
28 if not scriptEl.text: return True
29 if len(scriptEl.text) == 0: return True
30 if re.search(self.reUpdateJS, scriptEl.text): return True
31 return False
33 def addInkWebJSCode(self, scriptEl):
34 js = open( os.path.join(sys.path[0], "inkweb.js"), 'r' )
35 if hasattr(inkex.etree, "CDATA"):
36 scriptEl.text = \
37 inkex.etree.CDATA(
38 "\n/* inkweb.js - InkWebEffect:AutoUpdate */\n" + js.read()
39 )
40 else:
41 scriptEl.text = \
42 "\n/* inkweb.js - InkWebEffect:AutoUpdate */\n" + js.read()
43 js.close()
45 def ensureInkWebSupport(self):
46 # Search for the script tag with the inkweb.js code:
47 scriptEl = None
48 scripts = self.document.xpath('//svg:script', namespaces=inkex.NSS)
49 for s in scripts:
50 if re.search(self.reUpdateJS, s.text):
51 scriptEl = s
53 if scriptEl is None:
54 root = self.document.getroot()
55 scriptEl = inkex.etree.Element( "script" )
56 scriptEl.set( "id", "inkwebjs" )
57 scriptEl.set( "type", "text/javascript" )
58 root.insert( 0, scriptEl )
60 if self.mustAddInkWebJSCode(scriptEl):
61 self.addInkWebJSCode(scriptEl)