OSDN Git Service

implement list feature
authorSet <set.minami@gmail.com>
Sun, 26 May 2013 15:25:12 +0000 (00:25 +0900)
committerSet <set.minami@gmail.com>
Sun, 26 May 2013 15:25:12 +0000 (00:25 +0900)
.cache
mdTest/test5.bq
src/main/scala/org/blackquill/engine/BQParser.scala
src/main/scala/org/blackquill/engine/TreeNode.scala
src/main/scala/org/blackquill/main/BlackQuill.scala

diff --git a/.cache b/.cache
index fca90c7..e7eacae 100644 (file)
Binary files a/.cache and b/.cache differ
index 3f33ac9..77edd7d 100644 (file)
@@ -11,13 +11,46 @@ bbbbb<-testccccccc
 コメント
 -->
 *AAA*
-* abc
-  * ABC
-    * DE
-* def
-  * ABC
-  * CDE
-* ghi
+    * abc
+        * ABC
+        * DEFFF
+    * def
+        * ABCCC
+        * CDE
+    * ghi
+
+    - A
+        - B
+            - C
+    - D
+
+    + AA
+        + BB
+            + CC
+        + DD
+
+    * AAA
+        + BBB
+        + CCC
+            - DDD
+            - EEE
+
+    * A
+        * B
+            * C
+            * D
+                * E
+                    * F
+    * G
+
+
+    1. A
+    2. B
+        3. C
+        4. D
+            5. E
+                6. F
+    7. G
 
 <h1>aaaaa</h1>
 あああ
\ No newline at end of file
index 489493f..98411e7 100644 (file)
@@ -7,6 +7,9 @@ package org.blackquill.engine
 import org.apache.commons.logging._
 import scala.collection.immutable.List
 import scala.collection.mutable.LinkedHashMap
+import scala.collection.mutable.Stack
+import scala.collection.mutable.ListMap
+import scala.collection.SortedSet
 import scala.util.matching.Regex
 import scala.xml._
 
@@ -16,97 +19,121 @@ class BQParser {
        private val log:Log = LogFactory.getLog(classOf[BQParser])
 
        private val Syntax = LinkedHashMap(
-       "^(.*?)((\\s*\\*\\s.+?\\\\,)+)(.*?)$$" -> ("ul",surroundByListTAG _),
-       "^(.*?)\\*(.+?)\\*(.*?)$$" -> ("i",surroundByAbstructTAG _),
-       "^(.*?)\\*\\*(.+?)\\*\\*(.*?)$$" -> ("em",surroundByAbstructTAG _),
+       "^(.*?)((\\s+\\d+?\\.\\s.+?\\\\,\\s*?)+)\\\\,\\s*?(.*?)$$" -> ("ol",surroundByListTAG _),
+       "^(.*?)((\\s+[\\*|\\+|\\-]\\s.+?\\\\,\\s*?)+)\\\\,\\s*?(.*?)$$" -> ("ul",surroundByListTAG _),
+       "^(.*?)\\*(.+?)\\*(.*?)$$" -> ("i",surroundByGeneralTAG _),
+       "^(.*?)\\*\\*(.+?)\\*\\*(.*?)$$" -> ("em",surroundByGeneralTAG _),
        "^(.*?)(#+)\\s(.+?)\\\\,(.*?)$$" -> ("h",surroundByHeadTAG _)
        //"^(.*?)(\\\\,.+?\\\\,)(.*?)$$" -> ("p",surroundByAbstructTAG _)
        )
 
-/*
-       private def controlStyle(style:List[String]):String = {
-         if(style.size == 0){
-                 log warn "<ul> style: too many nests found where listed by by *"
-                 System.exit(-1)
-                 return ""
-         }else{return style.head}
+/*     private def ulStyles(index:Int):String =  {
+
+    }
+
+       private def olStyles(index:Int):String = 
        }
 */
        private def surroundByListTAG(doc:String, regex:String, TAG:String):String = {
-               var styles = List[String]()
+               val p = new Regex(regex, "before","elements","element","following")
+               val m = p findFirstMatchIn(doc)
+               var s = ""
+               var bef = ""
+               var fol = ""
+
+               if(m != None){
+                       if(m.get.group("before") != None){bef = m.get.group("before")}else{bef = ""}
+                       if(m.get.group("following") != None){fol = m.get.group("following")}else{fol = ""}
+                       s = m.get.group("elements")
+               }else{
+                 return doc
+               }
+
+               
                var sign = ""
                var sp = ""
-               val ulStyles = List[String]("disc","disc","disc","circle","circle","circle","square","square","square")
-               val olStyles = List[String]("disc","circle","square")     
+               val indentWidth = 4
+               var styles:((Int) => String) = null
+               
                TAG match{
              case "ul"=>
                sp = TAG
-               styles = ulStyles
-               sign = "*"
+               styles = (index:Int) => {
+                         (index/indentWidth)%3 match{
+                               case 1 => "disc"
+                               case 2 => "circle"
+                               case 0 => "square"
+                               case _ => "---"
+                         }
+               }
+               sign = "[\\*|\\+|\\-]"
              case "ol"=>
                sp = TAG
-               styles = olStyles
-               sign = "-"
+               styles = (index:Int) => {
+                         (index/indentWidth)%4 match{
+                           case 1 => "decimal"
+                           case 2 => "decimal-leading-zero"
+                           case 3 => "upper-latin"
+                           case 0 => "lower-latin"                         
+                           case _ => "---"
+                         }
+               }
+               sign = "\\d+?\\."
            }
-               var tree = new TreeNode[String]("root")
+               
+               var docList = List[String]()
+               for(elem <- s"""(\\s+?$sign\\s.+?\\\\,)+?""".r.findAllMatchIn(s)){                               
+                       docList = elem.group(1)::docList
+               }
+
 
-               def _surroundByListTAG(doc:String, regex:String, TAG:String, style:List[String],indent:Int):String = {
-                       if(doc == ""){return ""}
-                       if(style.size == 0){
-                           log warn s"<$sp> style: too many nests or wrong notation found where listing by $sp"
-                           System.exit(-1)
-                           return ""
-                       }
                
-                       val p = new Regex(regex, "before","elements","element","following")
-                       val m = p findFirstMatchIn(doc)
-                       if(m != None){
-                               var bef = ""
-                               var fol = ""
+               def _surroundByListTAG(doc:List[String],TAG:String,indent:Int):TreeNode[String] = {     
+                       var tree = new TreeNode[String]("")
+                   if(doc.isEmpty){return tree}                                                
+               
+                       tree.add(new TreeNode("<" + sp + s""" style=\"list-style-type:${styles(indent)}\">"""))
+                                                                       var i = indent
+                       var list = List.empty[Tuple3[String,Int,String]]
+                       for(elem <- doc){
+                         val m = s"""((\\s+?)$sign\\s(.+?)\\\\,)""".r.findFirstMatchIn(elem)
+                         list = (m.get.group(1),m.get.group(2).size,m.get.group(3))::list
+                       }
 
-                               if(m.get.group("before") != None){bef = m.get.group("before")}else{bef = ""}
-                               if(m.get.group("following") != None){fol = m.get.group("following")}else{fol = ""}
-                               val s = m.get.group("elements")
-                               log info s                      
-                               
-                               var str = ""
-                               var i = indent
-                               var list = List.empty[Tuple3[String,Int,String]]
-                               for(elem <- s"""((\\s*?)\\$sign\\s(.+?)\\\\,)+?""".r.findAllMatchIn(s)){
-                                 list = (elem.group(1),elem.group(2).size,elem.group(3))::list
-                               }
-                               var indents = 0
-                               for(elem <- list.reverse){
-                                 if(elem._2 != indents){indents = elem._2}
-                                 if(indents == i){
-                                       log info elem._1 + ":" + indents + ":" + elem._3
-                                   str += s"<$TAG>" + elem._3 + s"</$TAG>\\,"
-                                   log info "^^^" + elem._1
-                                   tree.add(new TreeNode[String](str))
-                                   //_surroundByUlListTAG(elem.group("following"),regex,TAG,style.tail,i)
-                                 }else if(indents > i){
-                                       str += _surroundByListTAG(elem._1,regex,TAG,style.tail,indents)
-                                       log info "%%%" + str
-                                       tree.add(new TreeNode[String](str))
-                                 }else if(indents < i){
-                                   str += _surroundByListTAG(elem._1,regex,TAG,style,indents)
-                                       log info "&&&" + str
-                                       tree.add(new TreeNode[String](str))
-                                 }
-                                 i = indents
+                       var restStr = List[String]()
+                       if(list.isEmpty){return new TreeNode("")
+                       }else{for(e <- list.reverse.tail){restStr = e._1::restStr}}
+                       
+                       restStr = restStr.reverse
+                       for(elem <- list.reverse){                      
+                               if(elem._2 > i){                           
+                                       tree.add(new TreeNode("<" + sp + s""" style=\"list-style-type:${styles(elem._2)}\">"""))
+                               }else if(elem._2 < i){                                  
+                                       tree.add(new TreeNode[String](s"</$sp>"*((i - elem._2)/indentWidth)))
                                }
+                               tree.add(new TreeNode[String](s"<$TAG>" + elem._3 + s"</$TAG>\\,"))     
                                
-                               val ST:String = style.head
-                               log info "---->"  
-                               for(node <- tree) log info node
-                               return _surroundByListTAG(bef,regex,TAG,style,0) +
-                                               s"""<$sp style=\"list-style-type:$ST\">""" + str + s"""</$sp>\\,""" +
-                                               _surroundByListTAG(fol,regex,TAG,style,0)                                               
-
+                               
+                               if(restStr.isEmpty){
+                                       restStr = List[String]("")                                
+                               }else{
+                                       restStr = restStr.tail
+                               }
+                               i = elem._2
                        }
-                       doc
-                 }
-               _surroundByListTAG(doc,regex,"li",styles,0)
+               tree.add(new TreeNode(s"</$sp>"*((i - indent)/indentWidth + 1)))
+               return tree
+         }
+               val r = s"""^(\\s*)${sign}.*?$$""".r("firstSpace")
+               val wS = r.findFirstMatchIn(s)
+               var str = ""
+                
+               if(wS != None){
+                       for(e <- _surroundByListTAG(docList.reverse,"li",wS.get.group("firstSpace").size)){
+                         str += e.toString()
+               }
+                 surroundByListTAG(bef,regex,TAG) + str + surroundByListTAG(fol,regex,TAG)
+               }else{doc}
        }
        
        private def surroundByHeadTAG(doc:String, regex:String, TAG:String):String = {
@@ -130,7 +157,7 @@ class BQParser {
          doc
        }
        
-       private def surroundByAbstructTAG(doc:String, regex:String, TAG:String):String = {
+       private def surroundByGeneralTAG(doc:String, regex:String, TAG:String):String = {
          if(doc == ""){return doc}
          log debug doc
          val p = new Regex(regex,"before","inTAG","following")
@@ -140,9 +167,9 @@ class BQParser {
              var fol = ""
                  if(m.get.group("before") != None){bef = m.get.group("before")}else{bef = ""}
              if(m.get.group("following") != None){fol = m.get.group("following")}else{fol = ""}
-                 return surroundByAbstructTAG(bef,regex,TAG) + 
+                 return surroundByGeneralTAG(bef,regex,TAG) + 
                                  s"<${TAG}>" + m.get.group("inTAG") + s"</${TAG}>" +
-                         surroundByAbstructTAG(fol,regex,TAG)
+                         surroundByGeneralTAG(fol,regex,TAG)
          }
          doc
        }
index 4bc1b22..7fb45a7 100644 (file)
@@ -1,19 +1,29 @@
 package org.blackquill.engine
 
 import scala.collection.immutable.Traversable
+import scala.collection.immutable.List
 import scala.collection.mutable.ArrayBuffer
 
 class TreeNode[T](val content:T) extends Traversable[T] {
override def toString="<Node %s>".format(content.toString)
      override def toString="%s".format(content.toString)
 
     var parent :Option[TreeNode[T]]=None
-    val children=ArrayBuffer[TreeNode[T]]()
+    val children = ArrayBuffer[TreeNode[T]]()
 
     def add(child :TreeNode[T])={
         child.parent=Some(this)
         children.append(child)
         child
     }
+       
+       def getContents():T = {
+         return content
+       }       
+       
+       def getChildren:List[TreeNode[T]] = {
+         val childrenList = children.toList
+         childrenList
+       }
 
     def foreach[U](f: T => U){
         for(child <- children){
@@ -21,4 +31,9 @@ class TreeNode[T](val content:T) extends Traversable[T] {
             child.foreach(f)
         }
     }
+    
+    override def mkString(sep:String):String = {
+      children.mkString(sep)
+    }
+
 }
\ No newline at end of file
index f99ca1d..238e51a 100644 (file)
@@ -122,9 +122,10 @@ object BlackQuill{
 
   def blackquill(lines:List[String]):List[String] = {
     val str = new HTMLMap htmlTAGFilter lines.mkString("\\,")
+    log info str
     val parsed = new BQParser
     log info parsed.toHTML(str)
-     str split """\\,""" toList
+    str split """\\,""" toList
   }
 
 }