Make alternate stylesheets possible
Implemented by http://hg.moinmo.in/moin/1.8/rev/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
For years browsers offer the ability to provide users with alternate stylesheets. For example, a "high contrast" style or "large fonts" or maybe just an alternate layout or color scheme some users prefer. You can see this explained and demonstrated at http://www.w3.org/Style/Examples/007/alternatives
MoinMoin lets you specify additional stylesheets using the config option stylesheets = [], however in order to use the above feature you must provide a "title" for your stylesheet and use the format:
<link rel="alternate stylesheet" title="Wide Screen" href="wide.css">
MoinMoin currently does not provide a way to push in a title.
Using Moin 1.6.4 as my platform, in ThemeBase there is a method html_stylesheets that builds the links to the stylesheet. I've created a method that gets used with map() to provide a backwards compatible way to provide the title. For example, imagine this definition of stylesheets in wikiconf.py:
# traditional definition stylesheets = [('screen', '/css/layout.css'), ('screen', '/css/colors.css')] # traditionally invalid definition stylesheets = [('/css/layout.css'), # invalid because media type not specified ('screen', '/css/darkblue.css', 'Dark blue theme')] # invalid because too many values
I've modified html_stylesheets and added the normalize_stylesheets method in order to work around this. It does the following:
- Permits specifying only a css file path and assumes 'screen' media type
- Allows three tuple items in the stylesheets list, the third tuple is the "title" attribute
- If the title attribute is set then it uses rel="alternate stylesheet" instead of rel="stylesheet"
These methods can be added directly to a theme but would be more broadly useful if they were incorporated into the main development branch. My only comment is that it was not clear to me the difference between stylesheets and self.request.cfg.stylesheets. If the stylesheets is never going to have the third "title" option then it would simplify the code somewhat to get rid of normalize_stylesheets and move it's logic into the for loop that iterates over self.request.cfg.stylesheets.
See attachment for a patch for 1.6.4
1 diff -r 02f68dc10be0 MoinMoin/theme/__init__.py
2 --- a/MoinMoin/theme/__init__.py Sun Aug 03 14:51:11 2008 +0200
3 +++ b/MoinMoin/theme/__init__.py Thu Nov 20 17:07:27 2008 -0600
4 @@ -625,6 +625,7 @@
5 @return: stylesheets links
6 """
7 link = '<link rel="stylesheet" type="text/css" charset="%s" media="%s" href="%s">'
8 + altlink = '<link rel="alternate stylesheet" type="text/css" charset="%s" media="%s" href="%s" title="%s">'
9
10 # Check mode
11 if d.get('print_mode'):
12 @@ -647,8 +648,19 @@
13 usercss = None
14
15 # admin configurable additional css (farm or wiki level)
16 - for media, csshref in self.request.cfg.stylesheets:
17 - html.append(link % (self.stylesheetsCharset, media, csshref))
18 + for stylesheet in self.request.cfg.stylesheets:
19 + media = csshref = title = ''
20 + if len(stylesheet) == 3:
21 + media, csshref, title = stylesheet
22 + elif len(stylesheet) == 2:
23 + media, csshref = stylesheet
24 + elif len(stylesheet) == 1:
25 + media = 'screen'
26 + csshref = stylesheet[0]
27 + if title :
28 + html.append(altlink % (self.stylesheetsCharset, media, csshref, title))
29 + else:
30 + html.append(link % (self.stylesheetsCharset, media, csshref))
31
32 csshref = '%s/%s/css/msie.css' % (prefix, self.name)
33 html.append("""
-- MatthewNuzum 2008-11-20 23:47:40
Matthew, thanks for the patch! I refactored it a bit more and cleaned up the old moin code. Committed to moin/1.8, see above.
I didn't implement the 1-tuple code, because
- the default media choice is unclear, could be 'screen' (as you did), but 'all' would also make sense
- thus, explicit is better than implicit in that case
- many users will fail to define a 1-tuple, as it is not (x), but (x,)
- the code was much easier without that
-- ThomasWaldmann 2008-11-29 21:54:22