Attachment 'modernized_mobile.py'

Download

   1 from MoinMoin.theme.modernized import Theme as ThemeBase
   2 from MoinMoin import wikiutil, config
   3 from MoinMoin.Page import Page
   4 
   5 class Theme(ThemeBase):
   6 
   7     name = "modernized" # we tell that we are 'modernized', so we use its static data
   8 
   9     def __init__(self, request):
  10         ThemeBase.__init__(self, request)
  11 
  12         import re
  13         self.is_mobile = re.search(r"android.+mobile|iphone|ipod", request.http_user_agent, re.I|re.M) is not None
  14 
  15     def html_head(self, d):
  16         if not self.is_mobile:
  17             return ThemeBase.html_head(self, d)
  18 
  19         html = [
  20             u'<title>%(title)s - %(sitename)s</title>' % {
  21                 'title': wikiutil.escape(d['title']),
  22                 'sitename': wikiutil.escape(d['sitename']),
  23             },
  24             self.html_stylesheets(d),
  25             ]
  26         return '\n'.join(html)
  27 
  28     def guiworks(self, page):
  29         if not self.is_mobile:
  30             return ThemeBase.guiworks(self, page)
  31 
  32         return False
  33 
  34     def header(self, d, **kw):
  35         if not self.is_mobile:
  36             return ThemeBase.header(self, d, **kw)
  37 
  38         html = [
  39             u'<div id="header">',
  40             self.logo(),
  41             self.username(d),
  42             u'<h1 id="locationline">',
  43             self.title_with_separators(d),
  44             u'</h1>',
  45             u'<div id="pageline"><hr style="display:none;"></div>',
  46             self.msg(d),
  47             self.editbar(d),
  48             u'</div>',
  49         ]
  50         return u'\n'.join(html)
  51 
  52     def editorheader(self, d, **kw):
  53         if not self.is_mobile:
  54             return ThemeBase.editorheader(self, d, **kw)
  55 
  56         html = [
  57             u'<div id="header">',
  58             u'<h1 id="locationline">',
  59             self.title_with_separators(d),
  60             u'</h1>',
  61             self.msg(d),
  62             u'</div>',
  63         ]
  64         return u'\n'.join(html)
  65 
  66     def footer(self, d, **keywords):
  67         if not self.is_mobile:
  68             return ThemeBase.footer(self, d, **keywords)
  69 
  70         page = d['page']
  71         html = [
  72             self.pageinfo(page),
  73             u'<div id="footer">',
  74             self.credits(d),
  75             self.showversion(d, **keywords),
  76             u'</div>',
  77             ]
  78         return u'\n'.join(html)
  79 
  80     def send_title(self, text, **keywords):
  81         if not self.is_mobile:
  82             return ThemeBase.send_title(self, text, **keywords)
  83 
  84         request = self.request
  85 
  86         if keywords.has_key('page'):
  87             page = keywords['page']
  88             pagename = page.page_name
  89         else:
  90             pagename = keywords.get('pagename', '')
  91             page = Page(request, pagename)
  92 
  93         request.content_type = "text/html; charset=%s" % (config.charset, )
  94 
  95         user_head = []
  96         user_head.append('''<meta http-equiv="Content-Type" content="%s;charset=%s">\n''' % (page.output_mimetype, page.output_charset))
  97         user_head.append('<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/>')
  98 
  99         output = []
 100         output.append("""\
 101 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 102 <html>
 103 <head>
 104 %s
 105 %s
 106 %s
 107 """ % (
 108             ''.join(user_head),
 109             self.html_head({
 110                 'page': page,
 111                 'title': text,
 112                 'sitename': request.cfg.html_pagetitle or request.cfg.sitename,
 113                 'print_mode': keywords.get('print_mode', False),
 114                 'media': keywords.get('media', 'screen'),
 115             }),
 116             keywords.get('html_head', ''),
 117         ))
 118         output.append("</head>\n")
 119 
 120         bodyattr = []
 121         output.append('\n<body%s>\n' % ''.join(bodyattr))
 122 
 123         d = {
 124             'theme': self.name,
 125             'title_text': text,
 126             'page': page,
 127             'page_name': pagename or '',
 128             'user_name': request.user.name,
 129             'user_valid': request.user.valid,
 130             'msg': self._status,
 131             'editor_mode': keywords.get('editor_mode', 0),
 132         }
 133         request.themedict = d
 134         if keywords.get('editor_mode', 0):
 135             output.append(self.editorheader(d))
 136         else:
 137             output.append(self.header(d))
 138 
 139         request.write(''.join(output))
 140         self._send_title_called = True
 141 
 142     def send_footer(self, pagename, **keywords):
 143         if not self.is_mobile:
 144             return ThemeBase.send_footer(self, pagename, **keywords)
 145 
 146         request = self.request
 147         d = request.themedict
 148         request.write(self.footer(d, **keywords))
 149 
 150     def send_closing_html(self):
 151         if not self.is_mobile:
 152             return ThemeBase.send_closing_html(self)
 153 
 154         request = self.request
 155         d = request.themedict
 156         if d['editor_mode']:
 157             request.write('''
 158 <script>
 159 document.getElementById('editor-help').style.display = "none";
 160 (function() {
 161     // auto grow text area
 162     var textarea = document.getElementById('editor-textarea'),
 163         timer;
 164     textarea.onfocus = function() {
 165         timer = setInterval(function() {
 166             var scrollHeight = textarea.scrollHeight,
 167                 clientHeight = textarea.clientHeight;
 168             if(clientHeight < scrollHeight) {
 169                 textarea.style.height = scrollHeight + 50 + 'px';
 170             }
 171         }, 100);
 172     };
 173     textarea.onblur = function() {
 174         clearInterval(timer);
 175     };
 176 })();
 177 </script>
 178 ''')
 179         request.write('</body>\n</html>\n\n')
 180 
 181 def execute(request):
 182     return Theme(request)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2012-02-12 14:16:45, 63.3 KB) [[attachment:modernized_mobile.png]]
  • [get | view] (2012-02-12 14:15:29, 5.4 KB) [[attachment:modernized_mobile.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.