Code

some love to webslicer_export.py
authorAurelio A. Heckert (a) <auriumgmaildotcom>
Tue, 27 Apr 2010 00:30:08 +0000 (21:30 -0300)
committerAurelio A. Heckert (a) <auriumgmaildotcom>
Tue, 27 Apr 2010 00:30:08 +0000 (21:30 -0300)
share/extensions/webslicer_export.py

index 02d049364baf7459e9ba32a99bc25d1f9a316f46..6b4ef75d832c251b29cb890742a4bd04e5b3a5cc 100755 (executable)
@@ -72,7 +72,7 @@ class WebSlicer_Export(WebSlicer_Effect):
                 self.html = open(os.path.join(self.options.dir,'layout.html'), 'w')
                 self.css  = open(os.path.join(self.options.dir,'style.css'), 'w')
                 self.html.write('Only a test yet\n\n')
-                self.css.write('Only a test yet\n\n')
+                self.css.write('/* Only a test yet */\n\n')
             except Exception as e:
                 inkex.errormsg( _('Can\'t create code files.') )
                 inkex.errormsg( _('Error: %s') % e )
@@ -84,45 +84,63 @@ class WebSlicer_Export(WebSlicer_Effect):
         # Close the HTML and CSS files:
         if self.options.with_code:
             self.html.close()
+            self.css.write( self.css_code() )
             self.css.close()
         # Delete the temporary SVG with invisible Slicer layer
         self.delete_the_temporary_svg()
+        #TODO: prevent inkex to return svg code to update Inkscape
+
+
+    svgNS = '{http://www.w3.org/2000/svg}'
+
 
     def create_the_temporary_svg(self):
         (ref, self.tmp_svg) = tempfile.mkstemp('.svg')
         layer = self.get_slicer_layer()
-        current_style = layer.attrib['style']
+        current_style = ('style' in layer.attrib) and layer.attrib['style'] or ''
         layer.attrib['style'] = 'display:none'
         self.document.write( self.tmp_svg );
         layer.attrib['style'] = current_style
 
+
     def delete_the_temporary_svg(self):
         os.remove( self.tmp_svg )
 
+
+    noid_element_count = 0
     def get_el_conf(self, el):
         desc = el.find('{http://www.w3.org/2000/svg}desc')
         conf = {}
-        if desc is not None:
-            #desc = desc.text.split("\n")
-            for line in desc.text.split("\n"):
-                if line.find(':') > 0:
-                    line = line.split(':')
-                    conf[line[0].strip()] = line[1].strip()
+        if desc is None:
+            desc = inkex.etree.SubElement(el, 'desc')
+        if desc.text is None:
+            desc.text = ''
+        for line in desc.text.split("\n"):
+            if line.find(':') > 0:
+                line = line.split(':')
+                conf[line[0].strip()] = line[1].strip()
+        if not 'html-id' in conf:
+            if el == self.get_slicer_layer():
+                return {'html-id':'#body#'}
+            else:
+                self.noid_element_count += 1
+                conf['html-id'] = 'element-'+str(self.noid_element_count)
+                desc.text += "\nhtml-id:"+conf['html-id']
         return conf
 
 
     def export_chids_of(self, parent):
-        nmspc = '{http://www.w3.org/2000/svg}'
+        parent_id = self.get_el_conf( parent )['html-id']
         for el in parent.getchildren():
             el_conf = self.get_el_conf( el )
-            if el.tag == nmspc+'g':
+            if el.tag == self.svgNS+'g':
                 if self.options.with_code:
                     self.register_group_code( el, el_conf )
                 else:
                     self.export_chids_of( el )
-            if el.tag in [ nmspc+'rect', nmspc+'path', nmspc+'circle' ]:
+            if el.tag in [ self.svgNS+'rect', self.svgNS+'path', self.svgNS+'circle' ]:
                 if self.options.with_code:
-                    self.register_unity_code( el, el_conf )
+                    self.register_unity_code( el, el_conf, parent_id )
                 self.export_img( el, el_conf )
 
 
@@ -135,19 +153,35 @@ class WebSlicer_Export(WebSlicer_Effect):
         self.html.write( '</div><!-- end id="G" -->\n' )
 
 
-    def register_unity_code(self, el, conf):
+    def register_unity_code(self, el, conf, parent_id):
         #inkex.errormsg( 'unity CSS and HTML' )
-        self.html.write( '<div id="image">\n' )
-        for att in conf:
-            self.html.write( '  <!-- {att} : {val} -->\n'.format(att=att, val=conf[att]) )
-        self.html.write( '</div><!-- end id="image" -->\n' )
+        css_selector = '#'+conf['html-id']
+        if not 'layout-disposition' in conf:
+            conf['layout-disposition'] = 'bg-el-norepeat'
+        if conf['layout-disposition'][0:9] == 'bg-parent':
+            if parent_id == '#body#':
+                css_selector = 'body'
+            else:
+                css_selector = '#'+parent_id
+            for att in conf:
+                self.html.write( '<!-- bg {att} : {val} -->\n'.format(att=att, val=conf[att]) )
+        else:
+            self.html.write( '<div id="image">\n' )
+            for att in conf:
+                self.html.write( '  <!-- {att} : {val} -->\n'.format(att=att, val=conf[att]) )
+            self.html.write( '</div><!-- end id="image" -->\n' )
+        self.reg_css( css_selector, 'background',
+                      'url("%s")' % self.img_name(el, conf) )
 
 
+    def img_name(self, el, conf):
+        return el.attrib['id']+'.png'
+
     def export_img(self, el, conf):
         (status, output) = commands.getstatusoutput(
             "inkscape -i '%s' -e '%s' '%s'" % (
                 el.attrib['id'],
-                os.path.join( self.options.dir, el.attrib['id']+'.png' ),
+                os.path.join( self.options.dir, self.img_name(el, conf) ),
                 self.tmp_svg
             )
         )
@@ -155,6 +189,22 @@ class WebSlicer_Export(WebSlicer_Effect):
         #inkex.errormsg( output )
 
 
+    _css = {}
+    def reg_css(self, selector, att, val):
+        if not selector in self._css: self._css[selector] = {}
+        if not att in self._css[selector]: self._css[selector][att] = []
+        self._css[selector][att].append( val )
+
+    def css_code(self):
+        code = ''
+        for selector in self._css:
+            code += '\n'+selector+' {\n'
+            for att in self._css[selector]:
+                code += '  '+ att +': '+ (', '.join(self._css[selector][att])) +';\n'
+            code += '}\n'
+        return code
+
+
 if __name__ == '__main__':
     e = WebSlicer_Export()
     e.affect()