Dolda2000 GitWeb
/
wrw.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Added a SP formatter function to output mere fragments of documents.
[wrw.git]
/
wrw
/
sp
/
util.py
diff --git
a/wrw/sp/util.py
b/wrw/sp/util.py
index
96e9880
..
0b31c65
100644
(file)
--- a/
wrw/sp/util.py
+++ b/
wrw/sp/util.py
@@
-44,6
+44,9
@@
class formatter(object):
def text(self, el):
self.quotewrite(el)
def text(self, el):
self.quotewrite(el)
+ def rawcode(self, el):
+ self.write(el)
+
def attrval(self, buf):
qc, qt = (u"'", u"'") if u'"' in buf else (u'"', u""")
self.write(qc)
def attrval(self, buf):
qc, qt = (u"'", u"'") if u'"' in buf else (u'"', u""")
self.write(qc)
@@
-112,6
+115,8
@@
class formatter(object):
self.element(el)
elif isinstance(el, cons.text):
self.text(el)
self.element(el)
elif isinstance(el, cons.text):
self.text(el)
+ elif isinstance(el, cons.raw):
+ self.rawcode(el)
else:
raise Exception("Unknown object in element tree: " + el)
else:
raise Exception("Unknown object in element tree: " + el)
@@
-135,6
+140,10
@@
class formatter(object):
def output(cls, out, el, *args, **kw):
cls(out=out, root=el, *args, **kw).start()
def output(cls, out, el, *args, **kw):
cls(out=out, root=el, *args, **kw).start()
+ @classmethod
+ def fragment(cls, out, el, *args, **kw):
+ cls(out=out, root=el, *args, **kw).element(el)
+
def update(self, **ch):
ret = type(self).__new__(type(self))
ret.__dict__.update(self.__dict__)
def update(self, **ch):
ret = type(self).__new__(type(self))
ret.__dict__.update(self.__dict__)
@@
-183,18
+192,21
@@
class indenter(formatter):
reind = False
if not self.simple(el):
sub = self.update(curind=self.curind + self.indent)
reind = False
if not self.simple(el):
sub = self.update(curind=self.curind + self.indent)
- sub.
out.indent(sub.curind
)
+ sub.
reindent(
)
reind = True
for ch in el.children:
sub.node(ch)
if reind:
reind = True
for ch in el.children:
sub.node(ch)
if reind:
- self.
out.indent(self.curind
)
+ self.
reindent(
)
self.endtag(el)
def element(self, el, **extra):
super(indenter, self).element(el, **extra)
if self.out.col > 80 and self.simple(el):
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)
+ self.reindent()
+
+ def reindent(self):
+ self.out.indent(self.curind.encode(self.charset))
def start(self):
super(indenter, self).start()
def start(self):
super(indenter, self).start()