/usr/lib/python2.7/dist-packages/pyth/plugins/plaintext/writer.py is in python-pyth 0.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | """
Render documents as plaintext.
"""
from pyth import document
from pyth.format import PythWriter
from cStringIO import StringIO
class PlaintextWriter(PythWriter):
@classmethod
def write(klass, document, target=None, newline="\n"):
if target is None:
target = StringIO()
writer = PlaintextWriter(document, target, newline)
return writer.go()
def __init__(self, doc, target, newline):
self.document = doc
self.target = target
self.newline = newline
self.indent = -1
self.paragraphDispatch = {
document.List: self.list,
document.Paragraph: self.paragraph
}
def go(self):
for (i, paragraph) in enumerate(self.document.content):
handler = self.paragraphDispatch[paragraph.__class__]
handler(paragraph)
self.target.write("\n")
# Heh heh, remove final paragraph spacing
self.target.seek(-2, 1)
self.target.truncate()
self.target.seek(0)
return self.target
def paragraph(self, paragraph, prefix=""):
content = []
for text in paragraph.content:
content.append(u"".join(text.content))
content = u"".join(content).encode("utf-8")
for line in content.split("\n"):
self.target.write(" " * self.indent)
self.target.write(prefix)
self.target.write(line)
self.target.write("\n")
if prefix: prefix = " "
def list(self, list, prefix=None):
self.indent += 1
for (i, entry) in enumerate(list.content):
for (j, paragraph) in enumerate(entry.content):
prefix = "* " if j == 0 else " "
handler = self.paragraphDispatch[paragraph.__class__]
handler(paragraph, prefix)
self.indent -= 1
|