changeset 4447:2254e666cea3

refactored and extended theme.html_stylesheets() to make alternate stylesheets possible stylesheet definitions now can either be: 2-tuples: (media, href) # backwards compatibility or: 3-tuples: (media, href, title) # new, for defining alternate stylesheets This works for defs within themes as well as for defs in the wiki config. See also: http://www.w3.org/Style/Examples/007/alternatives.html
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 29 Nov 2008 22:42:52 +0100
parents cef1a2c6dad2
children a0864c9c4752
files MoinMoin/theme/__init__.py
diffstat 1 files changed, 33 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/theme/__init__.py	Tue Nov 25 21:30:32 2008 +0100
+++ b/MoinMoin/theme/__init__.py	Sat Nov 29 22:42:52 2008 +0100
@@ -631,6 +631,28 @@
 </ul>''' % ''.join(items)
         return html
 
+    def _stylesheet_link(self, theme, media, href, title=None):
+        """ 
+        Create a link tag for a stylesheet.
+
+        @param theme: True: href gives the basename of a theme stylesheet,
+                      False: href is a full url of a user/admin defined stylesheet.
+        @param media: 'all', 'screen', 'print', 'projection', ...
+        @param href: see param theme
+        @param title: optional title (for alternate stylesheets), see
+                      http://www.w3.org/Style/Examples/007/alternatives
+        @rtype: string
+        @return: stylesheet link html
+        """
+        if theme:
+            href = '%s/%s/css/%s.css' % (self.cfg.url_prefix_static, self.name, href)
+        attrs = 'type="text/css" charset="%s" media="%s" href="%s"' % (
+                self.stylesheetsCharset, media, href, )
+        if title:
+            return '<link rel="alternate stylesheet" %s title="%s">' % (attrs, title)
+        else:
+            return '<link rel="stylesheet" %s>' % attrs
+
     def html_stylesheets(self, d):
         """ Assemble html head stylesheet links
 
@@ -638,45 +660,32 @@
         @rtype: string
         @return: stylesheets links
         """
-        link = '<link rel="stylesheet" type="text/css" charset="%s" media="%s" href="%s">'
-
+        request = self.request
         # Check mode
         if d.get('print_mode'):
             media = d.get('media', 'print')
             stylesheets = getattr(self, 'stylesheets_' + media)
         else:
             stylesheets = self.stylesheets
-        usercss = self.request.user.valid and self.request.user.css_url
-
-        # Create stylesheets links
-        html = []
-        prefix = self.cfg.url_prefix_static
-        csshref = '%s/%s/css' % (prefix, self.name)
-        for media, basename in stylesheets:
-            href = '%s/%s.css' % (csshref, basename)
-            html.append(link % (self.stylesheetsCharset, media, href))
 
-            # Don't add user css url if it matches one of ours
-            if usercss and usercss == href:
-                usercss = None
+        theme_css = [self._stylesheet_link(True, *stylesheet) for stylesheet in stylesheets]
+        cfg_css = [self._stylesheet_link(False, *stylesheet) for stylesheet in request.cfg.stylesheets]
 
-        # admin configurable additional css (farm or wiki level)
-        for media, csshref in self.request.cfg.stylesheets:
-            html.append(link % (self.stylesheetsCharset, media, csshref))
-
-        csshref = '%s/%s/css/msie.css' % (prefix, self.name)
-        html.append("""
+        msie_css = """
 <!-- css only for MSIE browsers -->
 <!--[if IE]>
    %s
 <![endif]-->
-""" % link % (self.stylesheetsCharset, 'all', csshref))
+""" % self._stylesheet_link(True, 'all', 'msie')
 
         # Add user css url (assuming that user css uses same charset)
-        if usercss and usercss.lower() != "none":
-            html.append(link % (self.stylesheetsCharset, 'all', usercss))
+        href = request.user.valid and request.user.css_url
+        if href and href.lower() != "none":
+            user_css = self._stylesheet_link(False, 'all', href)
+        else:
+            user_css = ''
 
-        return '\n'.join(html)
+        return '\n'.join(theme_css + cfg_css + [msie_css, user_css])
 
     def shouldShowPageinfo(self, page):
         """ Should we show page info?