forked from jaspervdj/tweetov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Templates.hs
96 lines (86 loc) · 3.25 KB
/
Templates.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
{-# LANGUAGE OverloadedStrings #-}
module Templates where
import Prelude
import Data.Monoid (mempty, mappend)
import Snap.Types (Snap, modifyResponse, addHeader, writeLBS)
import Text.Blaze.Renderer.Utf8 (renderHtml)
import Text.Blaze.Html5
import qualified Text.Blaze.Html5 as H
import Text.Blaze.Html5.Attributes
import qualified Text.Blaze.Html5.Attributes as A
import Data.Text (Text)
import Twitter
import Twitter.Html (linkTweet)
-- | Send blaze output to snap.
--
setBlaze :: Html -> Snap ()
setBlaze response = do
modifyResponse $ addHeader "Content-Type" "text/html; charset=UTF-8"
addBlaze response
-- | Send blaze output to snap, assuming 'setBlaze' has already been called.
--
addBlaze :: Html -> Snap ()
addBlaze = writeLBS . renderHtml
-- | The root page of the web app.
--
rootTemplate :: Html -- ^ Tweet section contents
-> Html -- ^ User section contents
-> Html
rootTemplate tweetSection' userSection' = docTypeHtml $ do
H.head $ do
H.title "Tweetov: Markov chain tweets"
script ! type_ "text/javascript" ! src "jquery-1.4.2.min.js"
$ mempty
script ! type_ "text/javascript" ! src "jquery.json-2.2.min.js"
$ mempty
script ! type_ "text/javascript" ! src "tweetov.js" $ mempty
link ! rel "stylesheet" ! type_ "text/css" ! href "screen.css"
body $ do
H.div ! A.id "content" $ do
h1 "Tweetov: Markov chain tweets"
inputSection
H.div ! A.id "tweet" $ tweetSection'
H.div ! A.id "user" $ userSection'
H.div ! A.style "clear: both;" $ mempty
H.div ! A.id "footer" $ do
"Tweets of insanity by "
a ! href "http://twitter.com/jaspervdj" $ "@jaspervdj"
" ("
a ! href "http://github.com/jaspervdj/tweetov" $ "source code"
")"
-- | The input section of the root page.
--
inputSection :: Html
inputSection = H.div ! A.id "inputsection" $ H.form
! onsubmit "return submit_username();"
$ do input ! type_ "text" ! name "usernamefield"
! A.id "usernamefield" ! value "DalaiLama"
-- Clear field on click.
! onfocus (preEscapedStringValue
"if(this.value == this.defaultValue) {\
\ $(this).val('');\
\};")
input ! type_ "submit"
! A.id "submitfield"
! value "Submit"
-- | Section containing a tweet.
--
tweetSection :: TweetInfo -> Integer -> Html
tweetSection tweet id' = H.div ! A.id "tweetsection" $ do
H.div ! A.id "tweet" $ linkTweet tweet
H.div ! A.id "tweetlink" $
a ! href (stringValue $ show id') $ "link (expires)"
-- | Produces a script to set the user
--
setUser :: Text -> Html
setUser user = script ! type_ "text/javascript" $
"set_user('" >> text user >> "');"
-- | Section containing the requested twitter user.
--
userSection :: UserInfo -> Html
userSection userInfo = H.div ! A.id "usersection" $ do
img ! src (textValue $ userImageUrl userInfo) ! alt "User image."
a ! href ("http://twitter.com/" `mappend` textValue (userName userInfo))
! A.id "username"
$ "@" `mappend` text (userName userInfo)
H.div ! A.id "realname" $ text $ userRealName userInfo