1 # CSSTidy - CSS Printer
5 # This file is part of CSSTidy.
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.
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.
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
21 # @license http://opensource.org/licenses/gpl-license.php GNU Public License
23 # @author Dj Gilcrease (digitalxero at gmail dot com) 2005-2006
27 class CSSPrinter(object):
28 def __init__(self, parser):
31 self.__renderMethods = {'string': self.__renderString, 'file': self.__renderFile}
34 def prepare(self, css):
37 def render(self, output="string", *args, **kwargs):
38 return self.__renderMethods[output](*args, **kwargs)
41 def __renderString(self, *args, **kwargs):
43 template = self.parser.getSetting('template')
46 if template == 'highest_compression':
52 elif template == 'high_compression':
55 bottom_line_end = "\n"
58 elif template == 'default':
61 bottom_line_end = "\n\n"
64 elif template == 'low_compression':
67 bottom_line_end = "\n\n"
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
73 for item in self.parser._import:
74 ret += '@import(' + item + ');' + top_line_end
76 for item in self.parser._charset:
77 ret += '@charset(' + item + ');' + top_line_end
79 for item in self.parser._namespace:
80 ret += '@namespace(' + item + ');' + top_line_end
82 for media, css in self._css.iteritems():
83 for selector, cssdata in css.iteritems():
84 ret += selector + '{' + top_line_end
86 for item, value in cssdata.iteritems():
87 ret += indent + item + ':' + value + ';' + iner_line_end
89 ret += '}' + bottom_line_end
93 def __renderFile(self, filename=None, *args, **kwargs):
95 return self.__renderString()
98 f = open(filename, "w")
99 f.write(self.__renderString())