- sub.node(ch)
- if reind:
- self.out.indent(self.curind)
- self.endtag(el)
-
- def element(self, el, **extra):
- super(indenter, self).element(el, **extra)
- if self.out.col > 80 and self.simple(el):
- self.out.indent(self.curind)
-
- def start(self):
- super(indenter, self).start()
- self.write('\n')
+ if isinstance(ch, cons.text):
+ return True
+ return False
+
+ def push(self, el):
+ self.stack.append((el, self.curind, self.inline))
+
+ def pop(self):
+ el, self.curind, self.inline = self.stack.pop()
+ return el
+
+ def starttag(self, el):
+ if not self.inline:
+ self.br()
+ self.push(el)
+ self.inline = self.inline or self.inlinep(el)
+ self.curind += self.indent
+ super().starttag(el)
+
+ def shorttag(self, el):
+ if not self.inline:
+ self.br()
+ super().shorttag(el)
+
+ def endtag(self, el):
+ il = self.inline
+ self.pop()
+ if not il:
+ self.br()
+ super().endtag(el)
+
+ def start(self, el):
+ super().start(el)
+ self.atbreak = True
+
+ def end(self, el):
+ self.br()
+
+class textindenter(indenter):
+ maxcol = 70
+
+ def text(self, el):
+ left = str(el)
+ while True:
+ if len(left) + self.col > self.maxcol:
+ bp = max(self.maxcol - self.col, 0)
+ for i in range(bp, -1, -1):
+ if left[i].isspace():
+ while i > 0 and left[i - 1].isspace(): i -= 1
+ break
+ else:
+ for i in range(bp + 1, len(left)):
+ if left[i].isspace():
+ break
+ else:
+ i = None
+ if i is None:
+ self.quotewrite(left)
+ break
+ else:
+ self.quotewrite(left[:i])
+ self.br()
+ left = left[i + 1:].lstrip()
+ else:
+ self.quotewrite(left)
+ break
+
+class response(dispatch.restart):
+ charset = "utf-8"
+ doctype = None
+ formatter = indenter
+
+ def __init__(self, root):
+ super().__init__()
+ self.root = root
+
+ @property
+ def ctype(self):
+ raise Exception("a subclass of wrw.sp.util.response must override ctype")
+
+ def handle(self, req):
+ ret = self.formatter.format(self.root, doctype=self.doctype, charset=self.charset)
+ req.ohead["Content-Type"] = self.ctype
+ req.ohead["Content-Length"] = len(ret)
+ return [ret]