summaryrefslogtreecommitdiff
path: root/web.py
blob: 35f2e897ead1aa5a53253e5b09643b3064daac76 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import data

from ll.xist import xsc, parse
from ll.xist.ns import html, xml, meta
from flask import Flask, Response, url_for

import difflib


class View:

    @staticmethod
    def template(title, body):
        return xsc.Frag(
            html.DocTypeXHTML10transitional(),
            "\n",
            html.html(
                html.head(
                    html.meta(charset='utf-8'),
                    html.title(title),
                    html.style("""\
.diff_add {
color: green;
}

table.diff {font-family:Courier; border:medium;}
.diff_header {background-color:#e0e0e0}
td.diff_header {text-align:right}
.diff_next {background-color:#c0c0c0}
.diff_sub {background-color:#ffaaaa}

tr td:nth-child(3) .diff_chg {
    color: blue;
}
tr td:nth-child(6) .diff_chg {
    color: orangered;
}""")
                ),
                html.body(body)))

    @staticmethod
    def index(feeds):
        return View.template("Index", html.ul(
            list(map(lambda feed:
                     html.li(
                         html.a(feed.title or feed.url, href=url_for('feed', id=feed.id))),
                     feeds))))

    @staticmethod
    def feed(feed, items):
        return View.template("Feed {}".format(feed.title or feed.url),
                             html.div(
                                 html.a("Back", href=url_for('index')),
            html.h1(feed.title),
            *list(map(lambda item: [
                html.h2(html.a(item.title, href=url_for('item', id=item.id))),
                html.ul(
                    *map(lambda version:
                         html.li("{} {}".format(version.created_date.strftime("%x %X"),
                                                version.title)),
                         item.versions)
                )
            ], items))))

    @staticmethod
    def format_version(a, b):
        temp = """\
Title: {0.title}
Authors: {0.authors}
Url: {0.url}
Text:   
{0.text}"""
        if a == None:
            adata = []
            fromdate = ""
        else:
            adata = temp.format(a).split("\n")
            fromdate = a.created_date.strftime("%x %X")
        todate = b.created_date.strftime("%x %X")

        bdata = temp.format(b).split("\n")
        table = difflib.HtmlDiff(wrapcolumn=60) \
            .make_table(adata, bdata,
                        fromdesc=fromdate, todesc=todate)

        table = table.encode('utf-8')
        node = parse.tree(table, parse.Expat(), parse.NS(
            html), parse.Node(pool=xsc.Pool(html)))
        return html.div(node)

    @staticmethod
    def item(item, versions):
        versionsA = versions
        versionsB = [None] + versions[:-1]
        versions = list(zip(versionsA, versionsB))
        return View.template("Item: {}".format(item.title),
                             html.div(
            html.a("Back to {}".format(item.feed.title),
                   href=url_for('feed', id=item.feed.id)),
            html.h1(item.title),
            *list(map(lambda versionAB:
                      View.format_version(versionAB[1], versionAB[0]),
                      versions))
        ))


def run():
    app = Flask(__name__)

    @app.route('/')
    def index():
        return View.index(data.Feed.select()).string("utf-8")

    @app.route('/feed/<id>')
    def feed(id):
        feed = data.Feed.get(data.Feed.id == id)
        items = data.Item.select() \
            .where(data.Item.feed == feed) \
            .order_by(data.Item.created_date.desc())
        return View.feed(feed, items).string("utf-8")

    @app.route('/item/<id>')
    def item(id):
        item = data.Item.get(data.Item.id == id)
        versions = data.Version.select() \
            .where(data.Version.item == item) \
            .order_by(data.Version.created_date)
        return View.item(item, list(versions)).string("utf-8")

    app.run()