def codit(self): """ Parse tempalte to python code buffer """ multi_code = False for lineno, line in enumerate(self.template.splitlines(True)): line = touni(line, self.encoding) sline = line.strip() # begin with {% if sline.startswith(self.multi_code_begin): multi_code = True sline = sline[2:].strip() if multi_code or \ (sline and sline.startswith(self.single_line_code)): # end with %} if sline.endswith(self.multi_code_end): multi_code = False line = sline[:-2] # begin with %% elif sline.startswith(self.single_line_code): line = sline[2:].strip() # line following the %% # within {% ... %} else: line = sline #if not line: continue cmd = re.split(r'[^a-zA-Z0-9_]', line)[0] self.flush() if cmd in self.blocks or self.multiline: cmd = self.multiline or cmd dedent = cmd in self.dedent_blocks if dedent and not self.oneline and not self.multiline: cmd = self.stack.pop() self.code(line) # a single line oneline = not line.endswith(':') multiline = cmd if line.endswith('\\') else False if not oneline and not multiline: self.stack.append(cmd) elif cmd.startswith('end') and self.stack: self.code('#end(%s) %s' % (self.stack.pop(), line.strip()[3:])) else: self.code(line) else: self.ptrbuffer.append(self.yield_tokens(line)) self.flush() return '\n' .join(self.codebuffer) + '\n'
def prepare(self, noescape=False, escape_func=html_escape): self._str = lambda x: touni(x, self.encoding) self._escape = escape_func if noescape: self._str, self._escape = self._escape, self._str