Note that only one regexp can be matched: chain filters together to parse multiple markups shortcuts. Note that the regexp must not be nullable, that is, it must never match an empty string.
Note that the start and end method must be weaver method names, not actual methods.
1: #line 17 "weaver_filters.ipk" 2: from interscript.weavers.multiplexor import multiplexor 3: import re 4: class markup_filter(multiplexor): 5: def __init__(self,pass_frame,regexp,startmethod,endmethod,base=[]): 6: multiplexor.__init__(self,pass_frame,base) 7: self.regexp = re.compile(regexp) 8: self.startmethod = startmethod 9: self.endmethod = endmethod 10: self.protocol = ('regexp filter',regexp) 11: self.name = 'markup filter v1' 12: self.translating = 1 13: 14: def translate(self): 15: self.translating = 1 16: for weaver in self.base: weaver.translate() 17: 18: def raw(self): 19: self.translating = 0 20: for weaver in self.base: weaver.raw() 21: 22: def raw_if(self, tag): 23: self.translating = 0 24: for weaver in base: weaver.raw_if(tag) 25: 26: def write(self,data): 27: if not self.enabled: return 28: if not self.translating: 29: for weaver in self.base: 30: weaver.write(data) 31: else: 32: match = self.regexp.search(data) 33: startpos = 0 34: while match: 35: midpos = match.start(0) 36: endpos = match.end(0) 37: if midpos == endpos: 38: raise 'nullable regexp '+self.regexp.pattern 39: if startpos != midpos: 40: for weaver in self.base: 41: weaver.write(data[startpos:midpos]) 42: for weaver in self.base: 43: exec 'weaver.'+self.startmethod+'()' 44: weaver.write(match.group(1)) 45: exec 'weaver.'+self.endmethod+'()' 46: startpos = endpos 47: match = self.regexp.search(data,startpos) 48: if startpos != len(data): 49: for weaver in self.base: 50: weaver.write(data[startpos:]) 51: 52: def writeline(self,data=''): 53: if not self.enabled: return 54: if data != '': self.write(data) 55: for weaver in self.base: 56: weaver.writeline() 57: 58: def begin_displayed_code(self): 59: self.translating = 0 60: for weaver in self.base: 61: try: weaver.begin_displayed_code() 62: except KeyboardInterrupt: raise 63: except: pass 64: 65: def end_displayed_code(self): 66: self.translating = 0 67: for weaver in self.base: weaver.end_displayed_code() 68: