Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 154 lines (140 sloc) 4.235 kB
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
1 /*
2 * Twopenny. (C) MLstate - 2011
3 * @author Adam Koprowski
4 **/
5
6 package mlstate.twopenny
7
12d1dcb @akoprow Improved message visuals.
akoprow authored
8 import widgets.dateprinter
9
b8da3ab @akoprow First (non-working) attempt at dynamic messages update.
akoprow authored
10 type Msg.t = private(
11 { author: User.ref
12 ; content : string
13 ; created_at : Date.date
14 })
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
15
16 type Msg.segment =
17 { text : string }
18 / { label : Label.ref }
7866469 @akoprow Simple rendering of messages
akoprow authored
19 / { url : Uri.uri }
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
20 / { user : User.ref }
21
56af1a0 @akoprow Further visual improvements.
akoprow authored
22 @both_implem Msg = {{
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
23
b8da3ab @akoprow First (non-working) attempt at dynamic messages update.
akoprow authored
24 create(author : User.ref, content : string) : Msg.t =
25 msg = { ~author ~content created_at=Date.now() }
26 @wrap(msg)
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
27
12d1dcb @akoprow Improved message visuals.
akoprow authored
28 get_creation_date(msg : Msg.t) : Date.date =
29 @unwrap(msg).created_at
30
31 get_author(msg : Msg.t) : User.ref =
32 @unwrap(msg).author
33
34 get_content(msg : Msg.t) : string =
35 @unwrap(msg).content
36
aee5d31 @akoprow Introducing MsgFactory.
akoprow authored
37 parse(msg : Msg.t) : list(Msg.segment) =
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
38 word = parser
39 /* FIXME we probably want to extend the character set
40 below. */
13f37dc @akoprow Fixing a subtle error in message parsing.
akoprow authored
41 | word=([a-zA-Z0-9_\-]*) -> Text.to_string(word)
0cf1c12 @akoprow Fixed parsing of URLs in messages.
akoprow authored
42 user_prefix = parser "@" -> void
43 label_prefix = parser "#" -> void
44 url_prefix = parser "http://" -> void
45 special_prefix = parser user_prefix | label_prefix | url_prefix;
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
46 segment_parser = parser
0cf1c12 @akoprow Fixed parsing of URLs in messages.
akoprow authored
47 | user_prefix user=word -> { user = User.mk_ref(user) }
48 | label_prefix label=word -> { label = Label.mk_ref(label) }
49 /* careful here, Uri.uri_parser too liberal, as it parses things like
50 hey.ho as valid URLs; so we use "http://" prefix to recognize URLs */
51 | &url_prefix url=Uri.uri_parser -> ~{ url }
52 /* below we eat a complete [word] or a single non-word character; the
53 latter case alone may not be enough as we don't want:
54 sthhttp://sth to pass for an URL. */
13f37dc @akoprow Fixing a subtle error in message parsing.
akoprow authored
55 | txt=((!special_prefix (. word))+) -> // Warning! + not * ; otherwise it will loop
12a06c2 @akoprow (For now very primitive) message submission.
akoprow authored
56 { text = Text.to_string(txt) }
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
57 msg_parser = parser res=segment_parser* -> res
12d1dcb @akoprow Improved message visuals.
akoprow authored
58 Parser.parse(msg_parser, Msg.get_content(msg))
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
59
a2ca3ab @akoprow Slowly fading in new messages.
akoprow authored
60 render(msg : Msg.t, mode : {preview} / {final} / {new}) : xhtml =
12d1dcb @akoprow Improved message visuals.
akoprow authored
61 render_segment =
7866469 @akoprow Simple rendering of messages
akoprow authored
62 | ~{ text } -> <>{text}</>
63 | ~{ url } ->
0cf1c12 @akoprow Fixed parsing of URLs in messages.
akoprow authored
64 // we add spaces around URLs to avoid collapsing them together with adjacent text
65 <> <a href={url}>{Uri.to_string(url)}</> </>
7866469 @akoprow Simple rendering of messages
akoprow authored
66 | ~{ label } -> Label.to_anchor(label)
67 | ~{ user } -> User.to_anchor(user)
68 content = List.map(render_segment, parse(msg))
12d1dcb @akoprow Improved message visuals.
akoprow authored
69 date = WDatePrinter.html(WDatePrinter.default_config,
70 uniq(), Msg.get_creation_date(msg))
56af1a0 @akoprow Further visual improvements.
akoprow authored
71 author = Msg.get_author(msg)
a2ca3ab @akoprow Slowly fading in new messages.
akoprow authored
72 id = uniq()
73 classes = ["msg" | if mode == {new} then ["hidden"] else []]
74 ready(_) = match mode with
75 | {new} ->
76 _ = Dom.Effect.fade_in()
77 |> Dom.Effect.with_duration({millisec=1500}, _)
78 |> Dom.transition(#{id}, _)
79 void
80 | _ -> void
81 <div id={id} class={classes} onready={ready}>
56af1a0 @akoprow Further visual improvements.
akoprow authored
82 {User.show_photo({size_px=48}, author)}
12d1dcb @akoprow Improved message visuals.
akoprow authored
83 <div class="content">
56af1a0 @akoprow Further visual improvements.
akoprow authored
84 <div class="user">{User.to_anchor(author)}</>
12d1dcb @akoprow Improved message visuals.
akoprow authored
85 <div class="text">{content}</>
86 {match mode with
a2ca3ab @akoprow Slowly fading in new messages.
akoprow authored
87 | {final}
88 | {new} -> <div class="date">{date}</>
12d1dcb @akoprow Improved message visuals.
akoprow authored
89 | {preview} -> <></> // don't display date in preview
90 }
91 </>
0e9d908 @akoprow Styles & animations for the message box.
akoprow authored
92 {match mode with
93 | {preview} -> <span class="preview">Preview</>
94 | {final}
95 | {new} -> <></>
96 }
12d1dcb @akoprow Improved message visuals.
akoprow authored
97 </>
7866469 @akoprow Simple rendering of messages
akoprow authored
98
12d1dcb @akoprow Improved message visuals.
akoprow authored
99 // FIXME WDatePrinter -> "now ago" -> "now"
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
100 }}
12d1dcb @akoprow Improved message visuals.
akoprow authored
101
cad668a @akoprow Further improvements to site CSS.
akoprow authored
102 msg_css = css
103 div{} // FIXME, work-around for CSS parsing
12d1dcb @akoprow Improved message visuals.
akoprow authored
104 .msg {
0e9d908 @akoprow Styles & animations for the message box.
akoprow authored
105 position: relative;
12d1dcb @akoprow Improved message visuals.
akoprow authored
106 width: 540px;
107 min-height: 48px;
69d9c5d @akoprow Re-styling messages + textarea gets different style when focused.
akoprow authored
108 font-size: 14px;
109 font-family: Verdana;
110 background: #E8EDFF;
111 padding: 10px;
112 border-radius: 10px;
113 border: 1px solid #C0CAED;
a2ca3ab @akoprow Slowly fading in new messages.
akoprow authored
114 margin-bottom: 10px;
115 }
116 .msg.new {
117 background: #FF0000;
12d1dcb @akoprow Improved message visuals.
akoprow authored
118 }
119 .msg:hover {
69d9c5d @akoprow Re-styling messages + textarea gets different style when focused.
akoprow authored
120 background: #D0DAFD;
12d1dcb @akoprow Improved message visuals.
akoprow authored
121 }
122 .msg .image {
123 width: 48px;
124 height: 48px;
125 margin-left: 3px;
126 float: left;
127 }
128 .msg .content {
129 margin-left: 60px;
130 }
131 .msg .user {
132 line-height: 15px;
133 color: #333;
134 font-weight: bold;
135 }
136 .msg .text {
137 line-height: 19px;
138 color: #444;
139 }
140 .msg .date {
69d9c5d @akoprow Re-styling messages + textarea gets different style when focused.
akoprow authored
141 font-size: 12px;
12d1dcb @akoprow Improved message visuals.
akoprow authored
142 color: #999;
143 }
0e9d908 @akoprow Styles & animations for the message box.
akoprow authored
144 .msg .preview {
145 position: absolute;
146 top: -13px;
147 right: -27px;
148 border: 1px solid #C0CAED;
149 background-color: #D0DAFD;
150 padding: 3px;
151 border-radius: 5px;
152 font-variant: small-caps;
153 }
Something went wrong with that request. Please try again.