File: svg2html.py

package info (click to toggle)
jpoker 1.0.16-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 58,992 kB
  • ctags: 14,582
  • sloc: ruby: 3,122; xml: 954; python: 329; makefile: 187; ansic: 17; php: 2
file content (147 lines) | stat: -rw-r--r-- 6,074 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python
# -*- mode: python -*-
#
# Copyright (C) 2008 Johan Euphrosine <proppy@aminche.com>
# Copyright (C) 2007,2008 Loic Dachary <loic@dachary.org>
# Copyright (C) 2006 Mekensleep
#
# Mekensleep
# 24 rue vieille du temple
# 75004 Paris
#       licensing@mekensleep.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
#

from xml.sax import parseString
from xml.sax.handler import ContentHandler
from xml.dom import minidom
import string
import re

class SVGParse(ContentHandler):
    def __init__(self, string): 
        self.root = ""
        self.formats = []
        self.tuples = []
        self.doc = minidom.parseString(string)
        parseString(string, self)
    def __str__(self):
        return string.join(map(lambda format, values: format % tuple(values), self.formats, self.tuples), '')
    def startElement(self, name, attrs):
        if name == "svg":
            self.startElementSvg(attrs)
        elif name == "image":
            self.startElementImage(attrs)
        elif name == "g":
            self.startElementGroup(attrs)
    def startElementGroup(self, attrs):
        pass
    def endElementSvg(self, attrs):
        pass
    def endElementGroup(self, attrs):
        pass
    def endElement(self, name):
        if name == "svg":
            self.endElementSvg(name)
        elif name == "g":
            self.endElementGroup(name)

class SVG2HTML(SVGParse):
    def startElementSvg(self, attrs):
        self.formats.append('<html><head></head><body><div id="%s" class="jpoker_ptable jpoker_table">')
        self.tuples.append((attrs['id'],))
    def startElementImage(self, attrs):
        self.formats.append('<div id="%s" class="jpoker_ptable_%s"></div>')
        self.tuples.append((attrs['id'],attrs['id']))
    def startElementGroup(self, attrs):
        self.formats.append('<div id="%s">')
        self.tuples.append((attrs['id'],))
    def endElementSvg(self, anem):
        self.formats.append('</div></body></html>')
        self.tuples.append(())
    def endElementGroup(self, anem):
        self.formats.append('</div>')
        self.tuples.append(())

class SVG2JSON(SVGParse):
    def startElementSvg(self, attrs):
        self.formats.append("<div id=\\'%s{id}\\' class=\\'jpoker_ptable jpoker_table\\'>")
        self.tuples.append((attrs["id"],))
    def startElementImage(self, attrs):
        self.formats.append("<div id=\\'%s{id}\\' class=\\'jpoker_ptable_%s\\'></div>")
        self.tuples.append((attrs["id"],attrs["id"]))
    def startElementGroup(self, attrs):
        self.formats.append("<div id=\\'%s{id}\\'>")
        self.tuples.append((attrs["id"],))
    def endElementSvg(self, anem):
        self.formats.append("</div>")
        self.tuples.append(())
    def endElementGroup(self, anem):
        self.formats.append("</div>")
        self.tuples.append(())

class SVG2CSS(SVGParse):
    ignore = [ '../css/images/jpoker_table/money.png',
               '../css/images/jpoker_table/winner.png',
               '../css/images/jpoker_table/name.png',
               '../css/images/jpoker_table/winning_hand.png',
               '../css/images/jpoker_table/timeout_bar.png',
               '../css/images/jpoker_table/raise_amount.png',
               '../css/images/jpoker_table/timeout_bar.png',
               '../css/images/jpoker_table/chat_input.png',
               '../css/images/jpoker_table/chat_log.png',
               '../css/images/jpoker_table/muck_options.png',
               '../css/images/jpoker_table/status.png'
               ]
    transforms = []
    def startElementSvg(self, attrs):
        self.root = attrs['id']
        format = '.jpoker_table .jpoker_ptable { width:%spx; height:%spx; position:relative; background-image:url("images/jpoker_table/table_background.png"); }\n'
        self.formats.append(format)
        self.tuples.append((attrs['width'], attrs['height']))
    def startElementGroup(self, attrs):
        tx, ty = 0, 0
        if attrs.has_key('transform'):
            tx, ty = map(float, re.match('translate\((-?\d+\.?\d*.*),\s*(-?\d+\.?\d*.*)\)', attrs['transform']).groups())
        self.transforms.append((tx, ty))
    def endElementGroup(self, name):
        self.transforms.pop()
    def startElementImage(self, attrs):
        tx, ty = 0, 0
        if attrs.has_key('transform'):
            tx, ty = map(float, re.match('translate\((-?\d+\.?\d*.*),\s*(-?\d+\.?\d*.*)\)', attrs['transform']).groups())
        for gx, gy in self.transforms:
            tx += gx
            ty += gy
        values = [ attrs['id'], attrs['width'], attrs['height'], str(int(float(attrs['y'])+ty)), str(int(float(attrs['x'])+tx)) ]
        if attrs['xlink:href'] not in SVG2CSS.ignore:
            image_format = 'background-image:url("%s");'
            values.append(str(attrs['xlink:href']).replace('../css/', ''))
        else:
            image_format = ''
        format = '.jpoker_table .jpoker_ptable_%s { width:%spx; height:%spx; position:absolute; top:%spx; left:%spx; ' + image_format + '}\n'
        self.formats.append(format)
        self.tuples.append(values)

if __name__ == '__main__':
    import sys
    if len(sys.argv) == 2:
        if sys.argv[1] == "--html":
            print SVG2HTML(sys.stdin.read())
        elif sys.argv[1] == "--css":
            print SVG2CSS(sys.stdin.read())
        elif sys.argv[1] == "--json":
            print "'" + str(SVG2JSON(sys.stdin.read())) + "';"