Attachment 'tabs.py'

Download

   1 import StringIO
   2 import re
   3 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
   4 #from nocamelcase import Parser as WikiParser
   5 from MoinMoin.formatter.text_docbook import Formatter as DocBookFormatter
   6 
   7 class Parser:
   8     def __init__(self, raw, request, **kw):
   9         self.raw = raw
  10         self.request = request
  11 
  12     def _docbook(self, formatter):
  13         request = self.request
  14         tabs = self.raw.split('<<Tabs')[1:]
  15 
  16         formatter.table(True, attrs={'role': 'tabs'})
  17         for tab in tabs:
  18             title = "No Title"
  19             m = re.search("\((.*)\)>>", tab)
  20             if m:
  21                 title = m.group(1)
  22             body = tab[tab.find(')>>')+3:]
  23             formatter.table_row(True)
  24             formatter.table_cell(True)
  25             WikiParser('<' + title + '>\n\n' + body, request).format(formatter)
  26             formatter.table_cell(False)
  27             formatter.table_row(False)
  28         formatter.table(False)
  29 
  30     def _wiki(self, formatter):
  31         result = []
  32         request = self.request
  33         if not hasattr(request, 'tabs_cnt'):
  34             request.tabs_cnt = 0
  35         tabs = self.raw.split('<<Tabs')[1:]
  36 
  37         result.append(formatter.div(True, css_class='tabs'))
  38 
  39         result.append(formatter.bullet_list(True))
  40         i = 0
  41         for tab in tabs:
  42             i += 1
  43             title = "No Title"
  44             m = re.search("\((.*)\)>>", tab)
  45             if m:
  46                 title = m.group(1)
  47             result.append(formatter.listitem(True))
  48             result.append(formatter.url(True, '#tabs-%d' % (request.tabs_cnt + i)))
  49             result.append(formatter.text(title))
  50             result.append(formatter.url(False))
  51             result.append(formatter.listitem(False))
  52         result.append(formatter.bullet_list(False))
  53 
  54         i = 0
  55         for tab in tabs:
  56             i += 1
  57             body = tab[tab.find(')>>')+3:]
  58             result.append(formatter.div(True, id='tabs-%d' % (request.tabs_cnt + i)))
  59             strfile = StringIO.StringIO()
  60             request.redirect(strfile)
  61             try:
  62                 WikiParser(body, request).format(formatter)
  63             finally:
  64                 request.redirect()
  65             result.append(strfile.getvalue())
  66             result.append(formatter.div(False))
  67 
  68         result.append(formatter.div(False))
  69 
  70         request.tabs_cnt += len(tabs)
  71 
  72         request.write(''.join(result))
  73 
  74         if not hasattr(request, 'code_for_tabs_parser'):
  75             request.code_for_tabs_parser = True
  76             request.write("""
  77 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  78 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js"></script>
  79 <link rel="stylesheet" type="text/css" charset="utf-8" media="all" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/smoothness/jquery-ui.css">
  80 <script type="text/javascript">
  81 $(function () {
  82     $('.tabs').tabs();
  83 });
  84 </script>
  85 """)
  86 
  87     def format(self, formatter):
  88         if formatter.__class__==DocBookFormatter:
  89             self._docbook(formatter)
  90         else:
  91             self._wiki(formatter)

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-08 11:25:28, 8.7 KB) [[attachment:tabs.png]]
  • [get | view] (2012-02-08 11:25:10, 3.1 KB) [[attachment:tabs.py]]
 All files | Selected Files: delete move to page copy to page

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