Code

InkWeb is a fetus, but is growing...
[inkscape.git] / share / extensions / inkwebeffect.py
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     scriptEl.text = \
36       inkex.etree.CDATA(
37         "\n/* inkweb.js - InkWebEffect:AutoUpdate */\n" + js.read()
38       )
39     js.close()
41   def ensureInkWebSupport(self):
42     # Search for the script tag with the inkweb.js code:
43     scriptEl = False
44     scripts = self.document.xpath('//script', namespaces=inkex.NSS)
45     for s in scripts:
46       inkex.errormsg(s)
47       if re.search(self.reUpdateJS, s.text):
48         inkex.errormsg("OK!")
49         scriptEl = s
51     if not scriptEl:
52       root = self.document.getroot()
53       scriptEl = inkex.etree.Element( "script" )
54       scriptEl.set( "id", "inkwebjs" )
55       scriptEl.set( "type", "text/javascript" )
56       root.insert( 0, scriptEl )
58     if self.mustAddInkWebJSCode(scriptEl): self.addInkWebJSCode(scriptEl)