Attachment 'taskpaper.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - TaskPaper Parser
   4     @copyright: 2007 David O'Callaghan <david.ocallaghan@cs.tcd.ie>
   5     @license: GNU GPL
   6     @version: 20071112
   7 
   8     based on:
   9 
  10     MoinMoin - ToDo Parser
  11      by Terry Brown <terry_n_brown@yahoo.com>
  12 
  13     MoinMoin wiki parser
  14 """
  15 
  16 import MoinMoin.parser.wiki as wiki
  17 import re
  18 
  19 Dependencies = []
  20 
  21 class Parser:
  22     """
  23         Format TaskPaper items
  24     """
  25 
  26     extensions = '*'
  27     Dependencies = []
  28 
  29     def __init__(self, raw, request, **kw):
  30         self.raw = raw
  31         self.request = request
  32         self.form = request.form
  33         self._ = request.getText
  34         self.list_indents = []
  35         self.list_types = []
  36         self.in_li = 0
  37 
  38     # Adapted from wiki.py
  39     def _indent_level(self):
  40         """Return current char-wise indent level."""
  41         return len(self.list_indents) and self.list_indents[-1]
  42 
  43     # Adapted from wiki.py
  44     def _indent_to(self, new_level, list_type="ul"):
  45         """Close and open lists."""
  46         open = []   # don't make one out of these two statements!
  47         close = []
  48 
  49         #self.request.write("<!-- old: %d, new: %d -->\n" %(self._indent_level(), new_level))
  50 
  51         # Close lists while char-wise indent is greater than the current one
  52         while ((self._indent_level() > new_level) or
  53                ( new_level and
  54                 (self._indent_level() == new_level) and
  55                 (self.list_types[-1]) != list_type)):
  56             self._close_item(close)
  57             tag = self.formatter.bullet_list(0)
  58             close.append(tag)
  59 
  60             del(self.list_indents[-1])
  61             del(self.list_types[-1])
  62             
  63             if self.list_types: # we are still in a list
  64                 self.in_li = 1
  65         # Open new list, if necessary
  66         if self._indent_level() < new_level:
  67                     
  68             self.list_indents.append(new_level)
  69             self.list_types.append(list_type)
  70 
  71             if self.formatter.in_p:
  72                close.append(self.formatter.paragraph(0))
  73             
  74             tag = self.formatter.bullet_list(1)
  75             open.append(tag)
  76 
  77             self.first_list_item = 1
  78             self.in_li = 0
  79 
  80         return ''.join(close) + ''.join(open)
  81 
  82 
  83     # Adapted from wiki.py
  84     def _undent(self):
  85         """Close all open lists."""
  86         result = []
  87         #result.append("<!-- _undent start -->\n")
  88         self._close_item(result)
  89         for type in self.list_types:
  90                 result.append(self.formatter.bullet_list(0))
  91         #result.append("<!-- _undent end -->\n")
  92         self.list_indents = []
  93         self.list_types = []
  94         return ''.join(result)
  95 
  96     # Adapted from wiki.py
  97     def _close_item(self, result):
  98         #result.append("<!-- close item begin -->\n")
  99         if self.in_li:
 100             self.in_li = 0
 101             if self.formatter.in_p:
 102                 result.append(self.formatter.paragraph(0))
 103             result.append(self.formatter.listitem(0))
 104         #result.append("<!-- close item end -->\n")
 105 
 106     def format(self, formatter):
 107         """ Send the text. """
 108 
 109         _ = self._
 110 
 111         def emit(x): self.request.write(x)
 112         def wemit(x):
 113             """emit something using wiki parser"""
 114             wk = wiki.Parser(x, self.request)
 115             wk.format(formatter)
 116 
 117         f = formatter
 118         self.formatter = formatter
 119 
 120         indent_re = re.compile("^\s*", re.UNICODE)
 121 
 122         for line in self.raw.split('\n'):
 123 
 124             if line.startswith('#'): continue  # a comment
 125 
 126             if line.endswith(':'): # heading
 127                 emit(self._undent())
 128                 emit(f.heading(1,1))
 129                 emit(line)
 130                 emit(f.heading(0,1))
 131 
 132             elif line.lstrip().startswith('-'): # task
 133                 indent = indent_re.match(line)
 134                 indlen = len(indent.group(0))
 135                 emit(self._indent_to(indlen+1))
 136 
 137                 done = line.find('@done') != -1
 138                 if(done):
 139                         emit(f.listitem(1, style="list-style-type: disc; text-decoration: line-through; filter:alpha(opacity=60); opacity:.60"))
 140                 else:
 141                         emit(f.listitem(1, style="list-style-type: circle"))
 142                 
 143                 #find tags
 144                 #tagre = re.compile("@[a-zA-Z0-9]+(?:\\([-a-zA-Z0-9]\\))??")
 145                 tagre = re.compile("@[a-zA-Z0-9]+")
 146                 tags =  tagre.findall(line)
 147                 task = re.sub(tagre, "", line)
 148 
 149                 emit(task) 
 150 
 151                 for tag in tags:
 152                         emit(f.small(1))
 153                         emit(f.emphasis(1))
 154                         emit(tag)
 155                         emit(f.emphasis(0))
 156                         emit(f.small(0))
 157 
 158                 emit(f.listitem(0))
 159 #            try:
 160 #                done = '2611' if int(item['percent']) == 100 else '2610'
 161 #            except:  # int() raised something
 162 #                done = '2610'
 163 #                if k == 'title': txt = '&#x%s; %s' % (done, txt)
 164 #                wemit(txt)
 165 
 166 #            if line.startswith((' ','\t')) or line == '':  # extra material
 167 #                continue
 168             else:
 169                 wemit(line)
 170 
 171         emit(self._undent())

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] (2007-11-12 13:04:02, 15.5 KB) [[attachment:taskpaper-moin.png]]
  • [get | view] (2007-11-12 12:54:29, 5.1 KB) [[attachment:taskpaper.py]]
 All files | Selected Files: delete move to page copy to page

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