Permalink
Browse files

Simple rendering of messages

With proper rendering of links and making <A> anchors from user (@)
and label (#) references in the message.
  • Loading branch information...
1 parent 7c0520d commit 7866469653cb57d9979dda5ceffe88c327348838 @akoprow akoprow committed Mar 29, 2011
Showing with 20 additions and 3 deletions.
  1. +3 −0 src/label.opa
  2. +14 −3 src/msg.opa
  3. +3 −0 src/user.opa
View
@@ -12,4 +12,7 @@ Label = {{
mk_ref(label : string) : Label.ref =
@wrap(label)
+ to_anchor(label : Label.ref) : xhtml =
+ <A href="/label/{@unwrap(label)}">#{label}</>
+
}}
View
@@ -10,25 +10,36 @@ type Msg.t = private(string)
type Msg.segment =
{ text : string }
/ { label : Label.ref }
- / { url : Url.url }
+ / { url : Uri.uri }
/ { user : User.ref }
Msg = {{
create(s : string) : Msg.t =
@wrap(s)
- parse(msg : Msg.t) : list(Msg.segment) =
+ @private parse(msg : Msg.t) : list(Msg.segment) =
word = parser
/* FIXME we probably want to extend the character set
below. */
| word=([a-zA-Z0-9_\-]*) -> Text.to_string(word)
segment_parser = parser
| [@] user=word -> { user = User.mk_ref(user) }
| [#] label=word -> { label = Label.mk_ref(label) }
- | &"http://" url=UriParser.uri -> ~{ url }
+ | &"http://" url=Uri.uri_parser -> ~{ url }
| txt=((![@#] .)*) -> { text = Text.to_string(txt) }
msg_parser = parser res=segment_parser* -> res
Parser.parse(msg_parser, @unwrap(msg))
+ render(msg : Msg.t) : xhtml =
+ render_segment =
+ | ~{ text } -> <>{text}</>
+ | ~{ url } ->
+ url_string = Uri.to_string(url)
+ <A href={url_string}>{url_string}</>
+ | ~{ label } -> Label.to_anchor(label)
+ | ~{ user } -> User.to_anchor(user)
+ content = List.map(render_segment, parse(msg))
+ <div class="msg">{content}</>
+
}}
View
@@ -12,4 +12,7 @@ User = {{
mk_ref(user : string) : User.ref =
@wrap(user)
+ to_anchor(user : User.ref) : xhtml =
+ <A href="/user/{@unwrap(user)}">@{user}</>
+
}}

0 comments on commit 7866469

Please sign in to comment.