02d049364baf7459e9ba32a99bc25d1f9a316f46
1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2010 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 from webslicer_effect import *
21 import inkex
22 import gettext
23 import os.path
24 import tempfile
25 import commands
27 _ = gettext.gettext
29 class WebSlicer_Export(WebSlicer_Effect):
31 def __init__(self):
32 WebSlicer_Effect.__init__(self)
33 self.OptionParser.add_option("--dir",
34 action="store", type="string",
35 dest="dir",
36 help="")
37 self.OptionParser.add_option("--create-dir",
38 action="store", type="inkbool",
39 default=False,
40 dest="create_dir",
41 help="")
42 self.OptionParser.add_option("--with-code",
43 action="store", type="inkbool",
44 default=False,
45 dest="with_code",
46 help="")
48 def effect(self):
49 # The user must supply a directory to export:
50 if is_empty( self.options.dir ):
51 inkex.errormsg(_('You must to give a directory to export the slices.'))
52 return {'error':'You must to give a directory to export the slices.'}
53 # No directory separator at the path end:
54 if self.options.dir[-1] == '/' or self.options.dir[-1] == '\\':
55 self.options.dir = self.options.dir[0:-1]
56 # Test if the directory exists:
57 if not os.path.exists( self.options.dir ):
58 if self.options.create_dir:
59 # Try to create it:
60 try:
61 os.makedirs( self.options.dir )
62 except Exception as e:
63 inkex.errormsg( _('Can\'t create "%s".') % self.options.dir )
64 inkex.errormsg( _('Error: %s') % e )
65 return {'error':'Can\'t create the directory to export.'}
66 else:
67 inkex.errormsg(_('The directory "%s" does not exists.') % self.options.dir)
68 return
69 # Create HTML and CSS files, if the user wants:
70 if self.options.with_code:
71 try:
72 self.html = open(os.path.join(self.options.dir,'layout.html'), 'w')
73 self.css = open(os.path.join(self.options.dir,'style.css'), 'w')
74 self.html.write('Only a test yet\n\n')
75 self.css.write('Only a test yet\n\n')
76 except Exception as e:
77 inkex.errormsg( _('Can\'t create code files.') )
78 inkex.errormsg( _('Error: %s') % e )
79 return {'error':'Can\'t create code files.'}
80 # Create the temporary SVG with invisible Slicer layer to export image pieces
81 self.create_the_temporary_svg()
82 # Start what we really want!
83 self.export_chids_of( self.get_slicer_layer() )
84 # Close the HTML and CSS files:
85 if self.options.with_code:
86 self.html.close()
87 self.css.close()
88 # Delete the temporary SVG with invisible Slicer layer
89 self.delete_the_temporary_svg()
91 def create_the_temporary_svg(self):
92 (ref, self.tmp_svg) = tempfile.mkstemp('.svg')
93 layer = self.get_slicer_layer()
94 current_style = layer.attrib['style']
95 layer.attrib['style'] = 'display:none'
96 self.document.write( self.tmp_svg );
97 layer.attrib['style'] = current_style
99 def delete_the_temporary_svg(self):
100 os.remove( self.tmp_svg )
102 def get_el_conf(self, el):
103 desc = el.find('{http://www.w3.org/2000/svg}desc')
104 conf = {}
105 if desc is not None:
106 #desc = desc.text.split("\n")
107 for line in desc.text.split("\n"):
108 if line.find(':') > 0:
109 line = line.split(':')
110 conf[line[0].strip()] = line[1].strip()
111 return conf
114 def export_chids_of(self, parent):
115 nmspc = '{http://www.w3.org/2000/svg}'
116 for el in parent.getchildren():
117 el_conf = self.get_el_conf( el )
118 if el.tag == nmspc+'g':
119 if self.options.with_code:
120 self.register_group_code( el, el_conf )
121 else:
122 self.export_chids_of( el )
123 if el.tag in [ nmspc+'rect', nmspc+'path', nmspc+'circle' ]:
124 if self.options.with_code:
125 self.register_unity_code( el, el_conf )
126 self.export_img( el, el_conf )
129 def register_group_code(self, group, conf):
130 #inkex.errormsg( 'group CSS and HTML' )
131 self.html.write( '<div id="G">\n' )
132 for att in conf:
133 self.html.write( ' <!-- {att} : {val} -->\n'.format(att=att, val=conf[att]) )
134 self.export_chids_of( group )
135 self.html.write( '</div><!-- end id="G" -->\n' )
138 def register_unity_code(self, el, conf):
139 #inkex.errormsg( 'unity CSS and HTML' )
140 self.html.write( '<div id="image">\n' )
141 for att in conf:
142 self.html.write( ' <!-- {att} : {val} -->\n'.format(att=att, val=conf[att]) )
143 self.html.write( '</div><!-- end id="image" -->\n' )
146 def export_img(self, el, conf):
147 (status, output) = commands.getstatusoutput(
148 "inkscape -i '%s' -e '%s' '%s'" % (
149 el.attrib['id'],
150 os.path.join( self.options.dir, el.attrib['id']+'.png' ),
151 self.tmp_svg
152 )
153 )
154 #inkex.errormsg( status )
155 #inkex.errormsg( output )
158 if __name__ == '__main__':
159 e = WebSlicer_Export()
160 e.affect()