~bkidwell/zim/pyzim-win-installer

« back to all changes in this revision

Viewing changes to zim/templates.py

  • Committer: Jaap Karssenberg
  • Date: 2010-06-30 20:35:51 UTC
  • Revision ID: jaap@no-spoon-20100630203551-w44d6mv1d9f9alsi
Create Note plugin now supports templates

Show diffs side-by-side

added added

removed removed

Lines of Context:
224
224
                                                raise TemplateSyntaxError, "unmatched '[%'"
225
225
                                        append_text(pre)
226
226
                                        if cmd.startswith('-'): # '[%-'
227
 
                                                if isinstance(self.stack[-1][-1], basestring):
228
 
                                                        self.stack[-1][-1] = self.stack[-1][-1].rstrip()
 
227
                                                try:
 
228
                                                        if isinstance(self.stack[-1][-1], basestring):
 
229
                                                                self.stack[-1][-1] = self.stack[-1][-1].rstrip()
 
230
                                                except IndexError:
 
231
                                                        pass
229
232
                                        if cmd.endswith('-'): # '-%]'
230
233
                                                line = line.lstrip()
231
234
                                        cmd = cmd.strip('-')
279
282
                                notebook, page,
280
283
                                self.format, self.linker, options),
281
284
                        'pages': pages,
282
 
                        'strftime': TemplateFunction(self.strftime),
 
285
                        'strftime': StrftimeFunction(),
283
286
                        'url': TemplateFunction(self.url),
284
287
                        'options': options
285
288
                }
286
289
 
287
290
                if self.linker:
288
 
                        self.linker.set_path(page) 
289
 
                        # this is later reset in body() but we need it here for 
 
291
                        self.linker.set_path(page)
 
292
                        # this is later reset in body() but we need it here for
290
293
                        # first part of the template
291
294
 
292
295
                output = GenericTemplate.process(self, dict)
304
307
                return self.format.Parser().parse(lines)
305
308
 
306
309
        @staticmethod
307
 
        def strftime(dict, format, timestamp=None):
308
 
                '''Static method callable from the template, returns a string'''
309
 
                if timestamp is None:
310
 
                        return strftime(format)
311
 
                elif isinstance(timestamp, basestring):
312
 
                        # TODO generalize this - now hardcoded for Calendar plugin
313
 
                        match = re.search(r'\d{4}:\d{2}:\d{2}', timestamp)
314
 
                        if match:
315
 
                                timestamp = strptime(match.group(0), '%Y:%m:%d')
316
 
                                return strftime(format, timestamp)
317
 
                        else:
318
 
                                return None
319
 
                else:
320
 
                        return strftime(format, timestamp)
321
 
 
322
 
        @staticmethod
323
310
        def url(dict, link):
324
311
                '''Static method callable from the template, returns a string'''
325
312
                if link is None:
466
453
 
467
454
                return output
468
455
 
 
456
 
469
457
class TemplateLiteral(unicode):
470
458
 
471
459
        def evaluate(self, dict):
549
537
                return self.function(*args)
550
538
 
551
539
 
 
540
class StrftimeFunction(TemplateFunction):
 
541
        '''Template function wrapper for strftime'''
 
542
 
 
543
        def __init__(self):
 
544
                pass
 
545
 
 
546
        def __call__(self, dict, format, timestamp=None):
 
547
                if timestamp is None:
 
548
                        return strftime(format)
 
549
                elif isinstance(timestamp, basestring):
 
550
                        # TODO generalize this - now hardcoded for Calendar plugin
 
551
                        match = re.search(r'\d{4}:\d{2}:\d{2}', timestamp)
 
552
                        if match:
 
553
                                timestamp = strptime(match.group(0), '%Y:%m:%d')
 
554
                                return strftime(format, timestamp)
 
555
                        else:
 
556
                                return None
 
557
                else:
 
558
                        return strftime(format, timestamp)
 
559
 
 
560
 
552
561
class TemplateDict(object):
553
562
        '''Object behaving like a dict for storing values of template parameters.
554
563
        It is initialized with a nested structure of dicts and objects which