This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
forked from ilius/starcal
/
export.py
203 lines (191 loc) · 7.51 KB
/
export.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# -*- coding: utf-8 -*-
#
# Copyright (C) Saeed Rasooli <saeed.gnu@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/gpl.txt>.
# Also avalable in /usr/share/common-licenses/GPL on Debian systems
# or /usr/share/licenses/common/GPL3/license.txt on ArchLinux
from scal3.cal_types import calTypes
from scal3 import locale_man
from scal3.locale_man import tr as _
from scal3 import core
from scal3 import ui
from scal3.monthcal import getMonthDesc
rgbToHtml = lambda r, g, b, a=None: '#%.2x%.2x%.2x'%(r, g, b) ## What to do with alpha ??
def colorComposite(front, back):
if len(back)==3:
r0, g0, b0 = back
a0 = 1.0
elif len(back)==4:
r0, g0, b0 = back[:3]
a0 = back[3]/255.0
else:
raise ValueError
if len(front)==3:
r1, g1, b1 = front
a1 = 1.0
elif len(front)==4:
r1, g1, b1 = front[:3]
a1 = front[3]/255.0
else:
raise ValueError
return (a1*r1 + (1-a1)*a0*r0,
a1*g1 + (1-a1)*a0*g0,
a1*b1 + (1-a1)*a0*b0)
def colorComposite3(front, middle, back):## FIXME
c1 = colorComposite(colorComposite(front, middle), back)
c2 = colorComposite(front, colorComposite(middle, back))
assert c1 == c2
return c1
def exportToHtml(fpath, monthsStatus, title=''):
##################### Options:
format = ((2, 'SUB'), (0, None), (1, 'SUB')) ## (dateMode, htmlTag)
sizeMap = lambda size: size*0.25 - 0.5 ## ???????????????
sep = ' '
pluginsTextSep = ' <B>ـ</B> '
pluginsTextPerLine = True ## description of each day in one line
#####################
bgColor = rgbToHtml(*ui.bgColor)
inactiveColor = rgbToHtml(*colorComposite(ui.inactiveColor, ui.bgColor))
borderColor= rgbToHtml(*colorComposite(ui.borderColor, ui.bgColor))
borderTextColor = rgbToHtml(*ui.borderTextColor)
textColor = rgbToHtml(*ui.textColor)
holidayColor = rgbToHtml(*ui.holidayColor)
colors = [rgbToHtml(*x['color']) for x in ui.mcalTypeParams]
if locale_man.rtl:
DIR = 'RTL'
else:
DIR = 'LRT'
text = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
<TITLE>%s</TITLE>
</HEAD>
<BODY LANG="%s" DIR="%s" BGCOLOR="%s">\n'''%(title, locale_man.langSh, DIR, bgColor)
for status in monthsStatus:
text += ' <P>\n'
for i, line in enumerate(getMonthDesc(status).split('\n')):
try:
color = colors[i]
except IndexError:
color = textColor
text += ' <FONT COLOR="%s">%s</FONT>\n <BR>\n'%(color, line)
text += ' </P>\n'
text += ''' <TABLE WIDTH=100%% BGCOLOR="%s" BORDER=%s BORDERCOLOR="#000000"
CELLPADDING=4 CELLSPACING=0>
<TR VALIGN=TOP>\n'''\
%(bgColor, int(ui.mcalGrid))
text += ''' <TD WIDTH=9%% BGCOLOR="%s">
<P ALIGN=CENTER></P>
</TD>\n'''%borderColor## what text????????
for j in range(7):
text += ''' <TD WIDTH=13%% BGCOLOR="%s">
<P ALIGN=CENTER>
<FONT COLOR="%s"><B>%s</B></FONT>
</P>
</TD>\n'''%(borderColor, borderTextColor, core.getWeekDayN(j))
pluginsText = '<P><FONT COLOR="%s">\n'%colors[0]
text += ' </TR>\n'
for i in range(6):
text += ''' <TR VALIGN=TOP>
<TD WIDTH=9%% BGCOLOR="%s">
<P ALIGN=CENTER>
<FONT COLOR="%s"><B>%s</B></FONT>
</P>
</TD>\n'''%(borderColor, borderTextColor, _(status.weekNum[i]))
for j in range(7):
cell = status[i][j]
text += ' <TD WIDTH=13%>\n <P DIR="LTR" ALIGN=CENTER>\n'
for (ind, tag) in format:
try:
mode = calTypes.active[ind]
except IndexError:
continue
try:
params = ui.mcalTypeParams[ind]
except IndexError:
continue
day = _(cell.dates[mode][2], mode)## , 2
font = params['font']
face = font[0]
if font[1]:
face += ' Bold'
if font[2]:
face += ' Underline'
size = str(sizeMap(font[3]))
if cell.month != status.month:
if ind==0:
text += ' '
if tag:
text += '<%s>'%tag
text += '<FONT COLOR="%s" FACE="%s" SIZE="%s">%s</FONT>'%(
inactiveColor,
face,
size,
day,
)
if tag:
text += '</%s>'%tag
text += '\n'
break
else:
continue
text += ' '
if tag:
text += '<%s>'%tag
if ind==0 and cell.holiday:
color = holidayColor
else:
color = colors[ind]
text += '<FONT COLOR="%s" FACE="%s" SIZE="%s">%s</FONT>'%(color, face, size, day)
if tag:
text += '</%s>'%tag
text += '\n'
#text += sep##???????????
text += ' </P>\n </TD>\n'
if cell.month == status.month:
if cell.holiday:
color = holidayColor
else:
color = colors[0]
t = cell.pluginsText.replace('\n', pluginsTextSep)
if t:
pluginsText += '<B><FONT COLOR="%s">%s</FONT>:</B> <SMALL>%s</SMALL>'%(
color,
_(cell.dates[calTypes.primary][2]),
t,
)
if pluginsTextPerLine:
pluginsText += '<BR>\n'
else:
pluginsText += ' \n'
text += ' </TR>\n'
pluginsText += ' </FONT></P>\n'
text += ' </TABLE>\n'
text += pluginsText
text += '\n<P STYLE="border-bottom: 5pt double %s"></P>\n'%colors[0]
text += ''' <P>
<FONT COLOR="%s">%s <A HREF="%s">%s</A> %s %s</FONT>
</P>
</BODY>
</HTML>'''%(
colors[0],
_('Generated by'),
core.homePage,
core.APP_DESC,
_('version'),
core.VERSION,
)
open(fpath, 'w').write(text)