wimap

Check-in [0d75320618]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Deal with quoting -> blockquote Factor all HTML-ification into its own module
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:0d75320618d02249e3c7febd563dde03d23da192abdbfc9bc20d97bc302dcda5
User & Date: ajv-899-334-8894@vsta.org 2016-11-28 14:44:42
Context
2016-11-30
22:50
Wrestle with some horrible edge cases from eBay check-in: b9f983566b user: ajv-899-334-8894@vsta.org tags: master, trunk
2016-11-28
14:44
Deal with quoting -> blockquote Factor all HTML-ification into its own module check-in: 0d75320618 user: ajv-899-334-8894@vsta.org tags: master, trunk
14:41
Firewall against spinning check-in: b0dcd65343 user: ajv-899-334-8894@vsta.org tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to get.py.

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
...
233
234
235
236
237
238
239
240
241
242
243
#  /
#	Main UI.  Top part is playlist, bottom is file/dir browser
#  /media/<prefix>/...
#	For each prefix of files, its contents is served by
#	way of its path under here.
import sys, urllib, cgi, time
from email.utils import parseaddr
import imap

# How many messages to list at a time
NMSG = 20

# The GET part of our handling
class GET_mixin(object):

................................................................................
                ourpath,
                startidx+NMSG )

        buf = self.build_tailer(buf)

        return True,self.send_result(buf, "text/html")

    # Return straight text, with blank lines replaced with <p>
    # This'll make reflowable-friendly HTML.
    def para_body(self, body):
        while "\n\n" in body:
            idx = body.index("\n\n")
            body = body[:idx] + "\n<p>\n" + body[idx+2:]
        return body

    # Message number under folder, render contents
    def read_msg(self):
        # /<folder>/<msg#>
        pp = self.paths
        if len(pp) != 2:
            return False,None

................................................................................
        # Headers and text of body
        fields,body = srv.msg_text(msgidx)

        # Render to page
        who = self.get_who(fields)
        when = self.get_when(fields)
        buf = self.build_header("%s %s" % (when, who))
        buf += self.para_body(body)

        buf = self.build_tailer(buf)
        return True,self.send_result(buf, "text/html")







|







 







<
<
<
<
<
<
<
<







 







|



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
188
189
190
191
192
193
194








195
196
197
198
199
200
201
...
225
226
227
228
229
230
231
232
233
234
235
#  /
#	Main UI.  Top part is playlist, bottom is file/dir browser
#  /media/<prefix>/...
#	For each prefix of files, its contents is served by
#	way of its path under here.
import sys, urllib, cgi, time
from email.utils import parseaddr
import imap, text

# How many messages to list at a time
NMSG = 20

# The GET part of our handling
class GET_mixin(object):

................................................................................
                ourpath,
                startidx+NMSG )

        buf = self.build_tailer(buf)

        return True,self.send_result(buf, "text/html")









    # Message number under folder, render contents
    def read_msg(self):
        # /<folder>/<msg#>
        pp = self.paths
        if len(pp) != 2:
            return False,None

................................................................................
        # Headers and text of body
        fields,body = srv.msg_text(msgidx)

        # Render to page
        who = self.get_who(fields)
        when = self.get_when(fields)
        buf = self.build_header("%s %s" % (when, who))
        buf += text.htmlify(body)

        buf = self.build_tailer(buf)
        return True,self.send_result(buf, "text/html")

Added text.py.











































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
73
74
75
76
77
78
79
80
81
82
83
84
85
#
# text.py
#	Make a message as readable as possible
#

# Tell tabulation/quoting depth
# Returns (quoting-depth, prefix-len)
def tabDepth(l):
    idx = 0
    orig = l
    while True:
	if (not l) or (l[0] not in (":", ">")):
	    return (idx, len(orig)-len(l))
	    break
	idx += 1
	l = l[1:]
	while l and l[0].isspace():
	    l = l[1:]

# Return straight text, with blank lines replaced with <p>
# This'll make reflowable-friendly HTML.
def para_body(body):
    while "\n\n" in body:
	idx = body.index("\n\n")
	body = body[:idx] + "\n<p>\n" + body[idx+2:]
    return body

# Take a plain text email body, return it decorated with HTML
#  for display
def htmlify(s):
    # Think of it as lines, with annotation of quotation
    #  depth in parallel
    ls = tuple(l.strip() for l in s.split('\n'))
    indents = tuple(tabDepth(l) for l in ls)

    # Process the message body by walking forward to
    #  successive quotation jumps or blank lines
    curi = 0
    lsidx = 0
    lslen = len(ls)
    res = []
    curblank = False
    while lsidx < lslen:
	l = ls[lsidx]
	i,plen = indents[lsidx]
	lsidx += 1

	# Change in quotation
	while curi < i:
	    res.append("<blockquote>\n")
	    curi += 1
	while curi > i:
	    res.append("</blockquote>\n")
	    curi -= 1

	# Shed quoting
	l = l[plen:]

	# Blank line == paragraph break
	if not l:
	    # Just one at a time
	    if not curblank:
		res.append("<p>\n")
		curblank = True
	else:
	    # Otherwise add to output
	    res.append(l)
	    curblank = False

    # Close off any open quotes
    while curi > i:
	res.append("</blockquote>\n")
	curi -= 1

    # Newline terminated message body in HTML
    return '\n'.join(res) + '\n'

if __name__ == "__main__":
    s = """This is some input text.
> With a quote
And I hope it works!

Hello, world.
"""
    print htmlify(s)