forked from msp-strath/MSPweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneOhOne.hs
316 lines (288 loc) · 17.2 KB
/
OneOhOne.hs
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
module OneOhOne where
import Data.Time
import Data.List
import Data.Ord
import Control.Arrow
import Control.Monad
import Data.Array((!))
import Text.Regex.PCRE -- cabal install regex-pcre
-- HTML utils
nl2br :: String -> String
nl2br [] = []
nl2br ('\n':xs) = "<br/>\n" ++ nl2br xs
nl2br (x:xs) = x:(nl2br xs)
createLink :: String -> String -> String
createLink [] name = name
createLink url name = "<a href='" ++ url ++ "'>" ++ name ++ "</a>"
bracket :: String -> String
bracket str = if null str then "" else " (" ++ str ++ ")"
wordwrap maxlen div = (wrap_ 0) . words where
wrap_ _ [] = ""
wrap_ pos (w:ws)
-- at line start: put down the word no matter what
| pos == 0 = w ++ wrap_ (pos + lw) ws
| pos + lw + 1 > maxlen = div ++ wrap_ 0 (w:ws)
| otherwise = " " ++ w ++ wrap_ (pos + lw + 1) ws
where lw = length w
-- Text.Regex.PCRE does not implement subRegex, so we copy
-- the code from Text.Regex (which does not handle non-greedy matches)
-- (a module system for Haskell, anyone?)
subRegex :: Regex -- ^ Search pattern
-> String -- ^ Input string
-> String -- ^ Replacement text
-> String -- ^ Output string
subRegex _ "" _ = ""
subRegex regexp inp repl =
let compile _i str [] = \ _m -> (str++)
compile i str (("\\",(off,len)):rest) =
let i' = off+len
pre = take (off-i) str
str' = drop (i'-i) str
in if null str' then \ _m -> (pre ++) . ('\\':)
else \ m -> (pre ++) . ('\\' :) . compile i' str' rest m
compile i str ((xstr,(off,len)):rest) =
let i' = off+len
pre = take (off-i) str
str' = drop (i'-i) str
x = read xstr
in if null str' then \ m -> (pre++) . ((fst (m!x))++)
else \ m -> (pre++) . ((fst (m!x))++) . compile i' str' rest m
compiled :: MatchText String -> String -> String
compiled = compile 0 repl findrefs where
-- bre matches a backslash then capture either a backslash or some digits
bre = makeRegex "\\\\(\\\\|[0-9]+)" :: Regex
findrefs = map (\m -> (fst (m!1),snd (m!0))) (matchAllText bre repl)
go _i str [] = str
go i str (m:ms) =
let (_,(off,len)) = m!0
i' = off+len
pre = take (off-i) str
str' = drop (i'-i) str
in if null str' then pre ++ (compiled m "")
else pre ++ (compiled m (go i' str' ms))
in go 0 inp (matchAllText regexp inp)
-- quick and dirty regexp translation of some HTML to its textual representation
html2text :: String -> String
html2text s = foldl' (\ t (p , r) -> subRegex (makeRegex p) t r) s
[("<em>(.*?)</em>", "*\\1*"), -- bold
("<strong>(.*?)</strong>", "*\\1*"),
("<b>(.*?)</b>", "*\\1*"),
("<i>(.*?)</i>", "/\\1/"), -- italic
("<br/>|<br>", "\n"), -- line breaks
("<p>(.*?)</p>", "\\1\n\n"), -- paragraph breaks
("<a\\s+href\\s*=\\s*[\"'](.*?)[\"']>\\1</a>", "\\1"), -- links
("<a\\s+href\\s*=\\s*[\"'](.*?)[\"']>(.*?)</a>", "\\2 (\\1)"),
("<ul>", "\n"), -- lists
("</ul>", ""),
("<li>(.*?)</li>", "* \\1\n"),
(" ", " "), -- escape characters
("–", "--"),
("—", "--"),
("²", "^2"),
("³", "^3"),
("½", "1/2"),
("ö", "ö"),
("ä", "ä"),
("å", "å"),
("<", "<"),
(">", ">"),
("&", "&"),
("¬", "not"),
("→", "->"),
("←", "<-"),
("↔", "<->"),
("⇒", "=>"),
("⇐", "<="),
("⇔", "<=>")
]
-- Generate web pages, calendars and a RSS feed for MSP 101 from data in
-- a text file
-- Extra material, such as slides, source code, ...
data Material = Link { address :: String,
linkDescription :: String }
| PDF { slideName :: FilePath }
| Whiteboard { dirName :: FilePath }
| File { path :: FilePath,
fileDescription :: String }
deriving (Show, Read, Eq)
data Talk = Talk {
date :: UTCTime,
speaker :: String,
institute :: String,
speakerurl :: String,
insturl :: String,
title :: String,
abstract :: String,
location :: String,
material :: [Material]
}
| SpecialEvent {
date :: UTCTime,
title :: String,
url :: String,
location :: String,
locationurl :: String,
description :: String
}
| DepartmentalSeminar {
date :: UTCTime,
speaker :: String,
institute :: String,
speakerurl :: String,
insturl :: String,
title :: String,
abstract :: String,
location :: String
}
deriving (Show, Read, Eq)
generateRSS :: [(Int,Talk)]
-> FilePath -- ^ Output path
-> IO ()
generateRSS ts out = do
let content = concatMap processEntry ts
header = unlines ["<?xml version='1.0' encoding='ISO-8859-1'?>",
"<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>",
" <channel>",
" <atom:link href='http://msp.cis.strath.ac.uk/msp101.rss' rel='self' type='application/rss+xml' />",
" <title>MSP101</title>",
" <link>http://msp.cis.strath.ac.uk/msp101.html</link>",
" <description>MSP101 is an ongoing series of informal talks given on Wednesday mornings by visiting academics or members of the MSP group.</description>",
" <language>en-gb</language>"]
footer = unlines [" </channel>", "</rss>"]
writeFile out (header ++ content ++ footer)
where processEntry (i,(Talk date speaker inst speakerurl insturl title abstract location material))
= let rsstitle = (showGregorian $ utctDay date) ++ ": " ++ speaker ++ bracket inst
abstr = if (null abstract) then "" else "<p><b>Abstract</b><br/><br/>" ++ (nl2br abstract) ++ "</p>"
desc = unlines ["<h2>" ++ (createLink speakerurl speaker) ++ (bracket (createLink insturl inst)) ++ "</h2>",
"<h2>" ++ title ++ "</h2>",
abstr,
"<b>" ++ (show date) ++ "<br/>" ++ location ++ "</b><br/>"]
in
unlines [" <item>",
" <title>" ++ rsstitle ++ "</title>",
" <description><![CDATA[" ++ desc ++ "]]></description>",
" <guid isPermaLink='true'>http://msp.cis.strath.ac.uk/msp101.html#" ++ (show i) ++ "</guid>",
" </item>"]
processEntry (i,(DepartmentalSeminar date speaker inst speakerurl insturl title abstract location))
= let rsstitle = (showGregorian $ utctDay date) ++ " Departmental seminar " ++ ": " ++ speaker ++ bracket inst
abstr = if (null abstract) then "" else "<p><b>Abstract</b><br/><br/>" ++ (nl2br abstract) ++ "</p>"
desc = unlines ["<h2>" ++ (createLink speakerurl speaker) ++ (bracket (createLink insturl inst)) ++ "</h2>",
"<h2>" ++ title ++ "</h2>",
abstr,
"<b>" ++ (show date) ++ "<br/>" ++ location ++ "</b><br/>"]
in
unlines [" <item>",
" <title>" ++ rsstitle ++ "</title>",
" <description><![CDATA[" ++ desc ++ "]]></description>",
" <guid isPermaLink='true'>http://msp.cis.strath.ac.uk/msp101.html#" ++ (show i) ++ "</guid>",
" </item>"]
processEntry (i,(SpecialEvent date title url location locationurl description))
= let rsstitle = (showGregorian $ utctDay date) ++ ": " ++ title
abstr = if (null description) then "" else "<p>" ++ (nl2br description) ++ "</p>"
desc = unlines ["<h2>" ++ (createLink url title) ++ (bracket location) ++ "</h2>",
"<h2>" ++ title ++ "</h2>",
abstr,
"<b>" ++ (show date) ++ "<br/>" ++ (createLink locationurl location) ++ "</b><br/>"]
in
unlines [" <item>",
" <title>" ++ rsstitle ++ "</title>",
" <description><![CDATA[" ++ desc ++ "]]></description>",
" <guid isPermaLink='true'>http://msp.cis.strath.ac.uk/msp101.html#" ++ (show i) ++ "</guid>",
" </item>"]
generateICS :: [(Int,Talk)]
-> FilePath -- ^ Output path
-> IO ()
generateICS ts out = do
now <- getZonedTime
let content = concatMap (processEntry now) ts
header = unlines ["BEGIN:VCALENDAR", "VERSION:2.0", "PRODID:-//MSP//MSP101 v1.0//EN",
"X-WR-CALNAME: MSP101",
"X-WR-CALDESC: MSP101 seminar series"]
footer = unlines ["END:VCALENDAR"]
writeFile out (header ++ content ++ footer)
where gatherData (Talk date speaker inst speakerurl insturl title abstract location material)
= let desc = escape $ html2text $ unlines ["Speaker: " ++ speaker ++ " " ++ (bracket inst), "Title: " ++ title ++ "\n", abstract]
end = addUTCTime (60*60::NominalDiffTime) date
in (desc, end, date, location, title, "")
gatherData (DepartmentalSeminar date speaker inst speakerurl insturl title abstract location)
= let desc = escape $ html2text $ unlines ["Speaker: " ++ speaker ++ " " ++ (bracket inst), "Title: " ++ title ++ "\n", abstract]
end = addUTCTime (60*60::NominalDiffTime) date
in (desc, end, date, location, title, "Departmental seminar: ")
gatherData (SpecialEvent date title url location locationurl description)
= let desc = escape $ html2text $ description
end = addUTCTime (60*60::NominalDiffTime) date
in (desc, end, date, location, title, "Event: ")
escape :: String -> String
escape [] = []
escape ('\\':xs) = "\\\\" ++ (escape xs)
escape ('\n':xs) = "\\n" ++ (escape xs)
escape (';':' ':xs) = "\\; " ++ (escape xs)
escape (',':' ':xs) = "\\, " ++ (escape xs)
escape (x:xs) = x:(escape xs)
processEntry now (i,x)
= let (desc, end, date, location, title, kindEvent) = gatherData x
in
unlines ["BEGIN:VEVENT",
"DTSTAMP;TZID=Europe/London:" ++ (formatTime defaultTimeLocale "%Y%m%dT%H%M%S" now),
"DTSTART;TZID=Europe/London:" ++ (formatTime defaultTimeLocale "%Y%m%dT%H%M%S" date),
"DTEND;TZID=Europe/London:" ++ (formatTime defaultTimeLocale "%Y%m%dT%H%M%S" $ end),
"LOCATION:" ++ location,
wordwrap 73 "\n " $ "SUMMARY:" ++ kindEvent ++ title,
wordwrap 73 "\n " $ "DESCRIPTION:" ++ desc,
"UID:" ++ (show i),
"END:VEVENT"]
generateHTML :: [(Int,Talk)]
-> FilePath -- ^ Output path
-> IO ()
generateHTML ts out = do
now <- fmap zonedTimeToUTC getZonedTime --getCurrentTime
let (previousTalks, upcomingTalks) = sortBy (flip $ comparing $ date . snd) *** sortBy (comparing $ date . snd) $ partition (\(i,x) -> date x < now) ts
-- Some talk statistics for stdout
when (not $ null upcomingTalks) $ putStrLn "\n==============\nUpcoming talks\n=============="
mapM putStrLn (map (\ x -> show (date x) ++ ": " ++ (title x) ++ (bracket $ (case x of SpecialEvent{} -> "" ; _ -> speaker x))) $ map snd upcomingTalks)
putStrLn $ "\n(" ++ (show $ length previousTalks) ++ " previous talks.)\n"
let upcoming = if null upcomingTalks then "" else unlines ["<h2>Upcoming talks</h2>",
"<dl>", concatMap processEntry upcomingTalks, "</dl>"]
previous = if null previousTalks then "" else unlines ["<h2>List of previous talks</h2>",
"<dl>", concatMap processEntry previousTalks, "</dl>"]
header = unlines ["### default.html(section.msp101=current,headtags=<link rel='alternate' type='application/rss+xml' title='MSP101 seminars RSS feed' href='/msp101.rss'/>)",
"<!-- DO NOT EDIT THIS FILE DIRECTLY — EDIT OneOhOneTalks.hs AND RUN Generate101.hs INSTEAD -->",
"<h2>MSP101</h2>",
"<p>MSP101 is an ongoing series of informal talks by visiting academics or members of the MSP group. The talks are usually Thursday mornings 11am in room LT1310 in Livingstone Tower. They are usually announced on the <a href='https://lists.cis.strath.ac.uk/mailman/listinfo/msp-interest'>msp-interest</a> mailing-list. The list of talks is also available as a <a type='application/rss+xml' href='/msp101.rss'><img src='/images/feed-icon-14x14.png' alt='feed icon'>RSS feed</a> and as a <a href='msp101.ics'>calendar file</a>.</p>"]
writeFile out (header ++ upcoming ++ previous)
where processEntry (i,(Talk date speaker inst speakerurl insturl title abstract location material))
= let time = if utctDayTime date == timeOfDayToTime (TimeOfDay 11 0 0) then (showGregorian $ utctDay date) else (formatTime defaultTimeLocale "%Y-%m-%d, %H:%M" date)
place = if location == "LT1310" then "" else (bracket location)
person = if null inst then (createLink speakerurl speaker)
else (createLink speakerurl speaker) ++ ", " ++ (createLink insturl inst)
dt = time ++ place ++ ": " ++ title ++ (bracket person)
pMat (Link url desc) = createLink url desc
pMat (PDF file) = createLink ("101/slides/" ++ file) "Slides"
pMat (File file desc) = createLink ("101/" ++ file) desc
pMat (Whiteboard dir) = createLink ("101/wb/" ++ dir) "Whiteboard photos"
mat = if null material then ""
else
(if null abstract then "" else "\n\n") ++
"<b>Material</b><ul>" ++
(concatMap (\ x -> "<li>" ++ (pMat x) ++ "</li>")
material) ++ "</ul>"
in
unlines [" <dt id='" ++ (show i) ++ "'>" ++ dt ++ "</dt>",
" <dd>" ++ (nl2br abstract)
++ (nl2br mat) ++ "</dd>"]
processEntry (i,(DepartmentalSeminar date speaker inst speakerurl insturl title abstract location))
= let time = formatTime defaultTimeLocale "%Y-%m-%d, %H:%M" date
place = bracket location
person = if null inst then (createLink speakerurl speaker)
else (createLink speakerurl speaker) ++ ", " ++ (createLink insturl inst)
dt = time ++ " " ++ (createLink "https://personal.cis.strath.ac.uk/clemens.kupke/CISeminar.html" "Departmental seminar") ++ " " ++ place ++ ": " ++ title ++ (bracket person)
in
unlines [" <dt id='" ++ (show i) ++ "'>" ++ dt ++ "</dt>",
" <dd>" ++ (nl2br abstract) ++ "</dd>"]
processEntry (i,(SpecialEvent date title url location locationurl description))
= let time = formatTime defaultTimeLocale "%Y-%m-%d" date
dt = time ++ ": " ++ (createLink url title)
++ (bracket (createLink locationurl location))
in
unlines [" <dt id='" ++ (show i) ++ "'>" ++ dt ++ "</dt>",
" <dd>" ++ (nl2br description) ++ "</dd>"]