-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.py
154 lines (146 loc) · 3.74 KB
/
post.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from xhpy.pylib import *
country_names = {
'al': 'Albania',
'ba': 'Bosnia and Herzegovina',
'be': 'Belgium',
'ca': 'Canada',
'ch': 'Switzerland',
'de': 'Germany',
'dk': 'Denmark',
'es': 'Spain',
'fr': 'France',
'gb': 'United Kingdom',
'gr': 'Greece',
'hr': 'Croatia',
'it': 'Italy',
'ma': 'Morocco',
'mc': 'Monaco',
'me': 'Montenegro',
'nl': 'Holland',
'pt': 'Portugal',
'si': 'Slovenia',
'tr': 'Turkey'
}
class :ui:page(:x:element):
attribute unicode title
def render(self):
return \
<x:doctype>
<html>
<head>
<meta charset="UTF-8" />
<title>{self.getAttribute('title')}</title>
<link href='https://fonts.googleapis.com/css?family=Lusitana:400,700' rel='stylesheet' type='text/css' />
<link href='/style.css' rel='stylesheet' type='text/css' />
</head>
<body>
{self.getChildren()}
</body>
</html>
</x:doctype>
class :ui:raw(:x:primitive):
category %flow
children pcdata
def stringify(self):
return u''.join(self.getChildren())
class :ui:index-section(:x:element):
attribute string country
def __init__(self, attributes={}, children=[], source=None):
super(:ui:index-section, self).__init__(attributes, children, source)
self._posts = <div class="country-posts" />
def render(self):
country = self.getAttribute('country')
return \
<div class="country" id={country}>
<div class="country-icon">
<img src={'/icons/{0}.png'.format(country)} />
</div>
<div class="country-name">
{country_names[country]}
</div>
{self._posts}
</div>
def addPost(self, data):
self._posts.appendChild(<ui:index-entry data={data} />)
class :ui:index-entry(:x:element):
attribute dict data
def render(self):
data = self.getAttribute('data')
return \
<div class="post">
<div class="post-date">
{data['date'].strftime('%Y-%m-%d')}
</div>
<div class="post-link">
<a href={data['href']}>{data['title']}</a>
</div>
</div>
def _link(href, text):
if href is None:
return <a class="disabled" href="#">{text}</a>
return <a class="nav" href={href}>{text}</a>
def render_index(posts):
countries = []
for data in posts:
if not countries or data['country'] != countries[-1].getAttribute('country'):
countries.append(<ui:index-section country={data['country']} />)
countries[-1].addPost(data)
page = \
<ui:page title={u"Bike To The Earth"}>
<div id="root">
<div id="title">
<h1>Bike to the Earth</h1>
</div>
<div id="content">
{countries}
</div>
</div>
</ui:page>
return unicode(page)
def render_post(data):
country_href = '/index.html#{0}'.format(data['country'])
nav = \
<div class="nav">
<div class="post-link">
{_link(data['prev'].get('href'), 'prev')}
</div>
<div class="post-link">
{_link(data['next'].get('href'), 'next')}
</div>
<div class="post-link">
{_link('/index.html', 'index')}
</div>
</div>
post = \
<div id="root">
{nav}
<div id="title">
<h1>{data['title']}</h1>
</div>
<div id="info">
<div id="country">
<div class="country-icon">
<a href={country_href}>
<img src={'/icons/{0}.png'.format(data['country'])} />
</a>
</div>
<div class="country-name">
{country_names[data['country']]}
</div>
</div>
<div class="post-date">
{data['date'].strftime('%Y-%m-%d')}
</div>
</div>
<div id="content">
<ui:raw>
{data['content']}
</ui:raw>
</div>
{nav}
</div>
page = \
<ui:page title={data['title']}>
{post}
</ui:page>
return unicode(page)