Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 61 lines (52 sloc) 2.005 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
b8da3ab @akoprow First (non-working) attempt at dynamic messages update.
akoprow authored
8 type Msg.t = private(
9 { author: User.ref
10 ; content : string
11 ; created_at : Date.date
12 })
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
13
14 type Msg.segment =
15 { text : string }
16 / { label : Label.ref }
7866469 @akoprow Simple rendering of messages
akoprow authored
17 / { url : Uri.uri }
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
18 / { user : User.ref }
19
29be2af @akoprow Box for typing messages made into a widget.
akoprow authored
20 Msg = {{
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
21
b8da3ab @akoprow First (non-working) attempt at dynamic messages update.
akoprow authored
22 create(author : User.ref, content : string) : Msg.t =
23 msg = { ~author ~content created_at=Date.now() }
24 @wrap(msg)
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
25
aee5d31 @akoprow Introducing MsgFactory.
akoprow authored
26 parse(msg : Msg.t) : list(Msg.segment) =
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
27 word = parser
28 /* FIXME we probably want to extend the character set
29 below. */
13f37dc @akoprow Fixing a subtle error in message parsing.
akoprow authored
30 | word=([a-zA-Z0-9_\-]*) -> Text.to_string(word)
0cf1c12 @akoprow Fixed parsing of URLs in messages.
akoprow authored
31 user_prefix = parser "@" -> void
32 label_prefix = parser "#" -> void
33 url_prefix = parser "http://" -> void
34 special_prefix = parser user_prefix | label_prefix | url_prefix;
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
35 segment_parser = parser
0cf1c12 @akoprow Fixed parsing of URLs in messages.
akoprow authored
36 | user_prefix user=word -> { user = User.mk_ref(user) }
37 | label_prefix label=word -> { label = Label.mk_ref(label) }
38 /* careful here, Uri.uri_parser too liberal, as it parses things like
39 hey.ho as valid URLs; so we use "http://" prefix to recognize URLs */
40 | &url_prefix url=Uri.uri_parser -> ~{ url }
41 /* below we eat a complete [word] or a single non-word character; the
42 latter case alone may not be enough as we don't want:
43 sthhttp://sth to pass for an URL. */
13f37dc @akoprow Fixing a subtle error in message parsing.
akoprow authored
44 | txt=((!special_prefix (. word))+) -> // Warning! + not * ; otherwise it will loop
12a06c2 @akoprow (For now very primitive) message submission.
akoprow authored
45 { text = Text.to_string(txt) }
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
46 msg_parser = parser res=segment_parser* -> res
b8da3ab @akoprow First (non-working) attempt at dynamic messages update.
akoprow authored
47 Parser.parse(msg_parser, @unwrap(msg).content)
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
48
7866469 @akoprow Simple rendering of messages
akoprow authored
49 render(msg : Msg.t) : xhtml =
0cf1c12 @akoprow Fixed parsing of URLs in messages.
akoprow authored
50 render_segment =
7866469 @akoprow Simple rendering of messages
akoprow authored
51 | ~{ text } -> <>{text}</>
52 | ~{ url } ->
0cf1c12 @akoprow Fixed parsing of URLs in messages.
akoprow authored
53 // we add spaces around URLs to avoid collapsing them together with adjacent text
54 <> <a href={url}>{Uri.to_string(url)}</> </>
7866469 @akoprow Simple rendering of messages
akoprow authored
55 | ~{ label } -> Label.to_anchor(label)
56 | ~{ user } -> User.to_anchor(user)
57 content = List.map(render_segment, parse(msg))
58 <div class="msg">{content}</>
59
7c0520d @akoprow Added very simple parser for messages (extracting @/#-tags & URLs)
akoprow authored
60 }}
Something went wrong with that request. Please try again.