+module Rakka.Wiki.Formatter
+ ( formatWikiElements
+ )
+ where
+
+import Control.Arrow
+import Control.Arrow.ArrowList
+import Data.List
+import Rakka.Wiki
+import Text.XML.HXT.Arrow.XmlArrow
+import Text.XML.HXT.DOM.TypeDefs
+
+
+-- 複數の Inline を一つに纏める
+packParagraph :: [WikiElement] -> [Either BlockElement [InlineElement]]
+packParagraph elems = map pack grp
+ where
+ grp :: [[WikiElement]]
+ grp = groupBy criteria elems
+
+ criteria :: WikiElement -> WikiElement -> Bool
+ criteria (Inline _) (Inline _) = True
+ criteria _ _ = False
+
+ pack :: [WikiElement] -> Either BlockElement [InlineElement]
+ pack (Block b : []) = Left b
+ pack xs = Right [ case x of
+ Inline i -> i | x <- xs ]
+
+
+formatWikiElements :: (ArrowXml a, ArrowChoice a) => a [WikiElement] XmlTree
+formatWikiElements
+ = proc elems
+ -> do chunk <- arrL id -< packParagraph elems
+ case chunk of
+ Left x -> formatBlock -< x
+ Right xs -> formatParagraph -< xs
+
+
+formatBlock :: (ArrowXml a, ArrowChoice a) => a BlockElement XmlTree
+formatBlock
+ = proc b
+ -> do case b of
+ Header level text
+ -> formatHeader -< (level, text)
+ EmptyLine
+ -> none -< ()
+
+
+formatHeader :: ArrowXml a => a (Int, String) XmlTree
+formatHeader
+ = proc (level, text)
+ -> selem ("h" ++ show level) [txt text] -<< ()
+
+
+formatParagraph :: (ArrowXml a, ArrowChoice a) => a [InlineElement] XmlTree
+formatParagraph
+ = proc xs
+ -> do elem <- arrL id -< xs
+ tree <- ( eelem "p"
+ += formatInline ) -< elem
+ returnA -< tree
+
+
+formatInline :: (ArrowXml a, ArrowChoice a) => a InlineElement XmlTree
+formatInline
+ = proc i
+ -> do case i of
+ Text text
+ -> mkText -< text