Key shortcuts in source editor work as expected, but only with "Alt" key. Support...
[redakcja.git] / apps / compress / filters / csstidy_python / output.py
1 # CSSTidy - CSS Printer
2 #
3 # CSS Printer class
4 #
5 # This file is part of CSSTidy.
6 #
7 # CSSTidy is free software you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # CSSTidy is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with CSSTidy if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 #
21 # @license http://opensource.org/licenses/gpl-license.php GNU Public License
22 # @package csstidy
23 # @author Dj Gilcrease (digitalxero at gmail dot com) 2005-2006
24
25 import data
26
27 class CSSPrinter(object):
28     def __init__(self, parser):
29         self.parser = parser
30         self._css = {}
31         self.__renderMethods = {'string': self.__renderString, 'file': self.__renderFile}
32
33 #PUBLIC METHODS
34     def prepare(self, css):
35         self._css = css
36
37     def render(self, output="string", *args, **kwargs):
38         return self.__renderMethods[output](*args, **kwargs)
39
40 #PRIVATE METHODS
41     def __renderString(self, *args, **kwargs):
42         ##OPTIMIZE##
43         template = self.parser.getSetting('template')
44         ret = ""
45
46         if template == 'highest_compression':
47             top_line_end = ""
48             iner_line_end = ""
49             bottom_line_end = ""
50             indent = ""
51
52         elif template == 'high_compression':
53             top_line_end = "\n"
54             iner_line_end = ""
55             bottom_line_end = "\n"
56             indent = ""
57
58         elif template == 'default':
59             top_line_end = "\n"
60             iner_line_end = "\n"
61             bottom_line_end = "\n\n"
62             indent = ""
63
64         elif template == 'low_compression':
65             top_line_end = "\n"
66             iner_line_end = "\n"
67             bottom_line_end = "\n\n"
68             indent = "    "
69
70         if self.parser.getSetting('timestamp'):
71             ret += '/# CSSTidy ' + self.parser.version + ': ' + datetime.now().strftime("%a, %d %b %Y %H:%M:%S +0000") + ' #/' + top_line_end
72
73         for item in self.parser._import:
74             ret += '@import(' + item + ');' + top_line_end
75
76         for item in self.parser._charset:
77             ret += '@charset(' + item + ');' + top_line_end
78
79         for item in self.parser._namespace:
80             ret += '@namespace(' + item + ');' + top_line_end
81
82         for media, css in self._css.iteritems():
83             for selector, cssdata in css.iteritems():
84                 ret += selector + '{' + top_line_end
85
86                 for item, value in cssdata.iteritems():
87                     ret += indent +  item + ':' + value + ';' + iner_line_end
88
89                 ret += '}' + bottom_line_end
90
91         return ret
92
93     def __renderFile(self, filename=None, *args, **kwargs):
94         if filename is None:
95             return self.__renderString()
96
97         try:
98             f = open(filename, "w")
99             f.write(self.__renderString())
100         finally:
101             f.close()