incubator-esme-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vdic...@apache.org
Subject svn commit: r885151 - in /incubator/esme/trunk/server/src/main/scala/org/apache/esme: lib/MsgParser.scala model/Message.scala
Date Sat, 28 Nov 2009 21:33:49 GMT
Author: vdichev
Date: Sat Nov 28 21:33:49 2009
New Revision: 885151

URL: http://svn.apache.org/viewvc?rev=885151&view=rev
Log:
Parser improvements for inline formatting.

Modified:
    incubator/esme/trunk/server/src/main/scala/org/apache/esme/lib/MsgParser.scala
    incubator/esme/trunk/server/src/main/scala/org/apache/esme/model/Message.scala

Modified: incubator/esme/trunk/server/src/main/scala/org/apache/esme/lib/MsgParser.scala
URL: http://svn.apache.org/viewvc/incubator/esme/trunk/server/src/main/scala/org/apache/esme/lib/MsgParser.scala?rev=885151&r1=885150&r2=885151&view=diff
==============================================================================
--- incubator/esme/trunk/server/src/main/scala/org/apache/esme/lib/MsgParser.scala (original)
+++ incubator/esme/trunk/server/src/main/scala/org/apache/esme/lib/MsgParser.scala Sat Nov
28 21:33:49 2009
@@ -228,15 +228,29 @@
     case xs => MsgText(xs.mkString(""))
   }
 
-  lazy val emph: Parser[Emph] = '_' ~> rep1(not(spaceEOF) ~ not('_') ~ not(hashTag) ~
not(atName) ~> anyChar) <~ '_' ^^ {
-    case xs => Emph(xs.mkString)
+  lazy val begOrSpace: Parser[Int] = rep1(' ') ^^ {case lst => lst.length} | beginl ^^^
0
+  lazy val spaceOrEnd: Parser[Int] = EOL ^^^ 0 | rep1(' ') ^^ {case lst => lst.length}
+
+  def peek[T](p: Parser[T]): Parser[T] = Parser { in =>
+    p(in) match {
+      case s @ Success(v, _) => Success(v, in)
+      case e @ Error(msg, _) => Error(msg, in)
+      case f @ Failure(msg, _) => Failure(msg, in)
+    }
+  }
+
+  lazy val beginl = Parser[Unit]{ in =>
+    if(in.pos.column==1) Success((), in) else Failure("", in)
   }
   
-  lazy val strong: Parser[Strong] = 
-  '*' ~> rep1(not(spaceEOF) ~ not('*') ~ not(hashTag) ~ not(atName) ~> anyChar) <~
'*' ^^ {
-    case xs => Strong(xs.mkString)
+  def surrounded(sep: String)(constructor: (String) => MsgInfo): Parser[MsgInfo] =
+    begOrSpace ~ accept(sep) ~> rep1(not(spaceEOF) ~ not(accept(sep)) ~ not(hashTag) ~
not(atName) ~> anyChar) <~ accept(sep) ~ peek(spaceOrEnd) ^^ {
+    case xs => constructor(xs.mkString)
   }
   
+  lazy val emph = surrounded("_"){Emph(_)}
+  lazy val strong = surrounded("*"){Strong(_)}
+
   lazy val EOF: Parser[Elem] = elem("EOF", isEof _)
 
   def perform(in: String): Box[Performances] = _perform(in) match {

Modified: incubator/esme/trunk/server/src/main/scala/org/apache/esme/model/Message.scala
URL: http://svn.apache.org/viewvc/incubator/esme/trunk/server/src/main/scala/org/apache/esme/model/Message.scala?rev=885151&r1=885150&r2=885151&view=diff
==============================================================================
--- incubator/esme/trunk/server/src/main/scala/org/apache/esme/model/Message.scala (original)
+++ incubator/esme/trunk/server/src/main/scala/org/apache/esme/model/Message.scala Sat Nov
28 21:33:49 2009
@@ -316,6 +316,16 @@
               <xml:group> <a href={"/u/"+id}>{url}</a> </xml:group>)).
           getOrElse(Text("") )
 
+        case e: Elem if e.label == "em" =>
+          e.attribute("text").map(text =>
+            <em>{text}</em>).
+          getOrElse(e)
+        
+        case e: Elem if e.label == "strong" =>
+          e.attribute("text").map(text =>
+            <strong>{text}</strong>).
+          getOrElse(e)
+        
         case x => x
       })
   }
@@ -392,6 +402,12 @@
     tags.map(x => x.split(" ").mkString("_")) mkString " "
   }
 
+  /**
+   * Parse and format into XML
+   * Note that the text representation of the XML must be readable
+   * for clients that don't support markup formatting
+   * and is recommended to result in the same XML when parsed
+   */
   def setTextAndTags(in: String, tags: List[Tag], metaData: Box[Elem]): Box[Message] = {
     MsgParser.parseMessage(in).map{
       lst =>
@@ -403,8 +419,8 @@
               case MsgText(text) => Text(text)
               case URL(url) => <url id={url.id.toString}
                   url={url.url.toString} uniqueId={url.uniqueId.is} >{url.url.toString}</url>
-              case Emph(text) => <em>{text}</em>
-              case Strong(text) => <strong>{text}</strong>
+              case Emph(text) => <xml:group> <em text={text}>_{text}_</em>
</xml:group>
+              case Strong(text) => <xml:group> <strong text={text}>*{text}*</strong>
</xml:group>
             }
           }</body>
         <tags>{



Mime
View raw message