1 module Rakka.Wiki.Formatter
7 import Control.Arrow.ArrowList
14 import Text.XML.HXT.Arrow.XmlArrow
15 import Text.XML.HXT.DOM.TypeDefs
18 formatWikiBlocks :: (ArrowXml a, ArrowChoice a) => a (URI, [BlockElement]) XmlTree
20 = proc (baseURI, blocks)
21 -> do block <- arrL id -< blocks
22 formatBlock -< (baseURI, block)
25 formatBlock :: (ArrowXml a, ArrowChoice a) => a (URI, BlockElement) XmlTree
27 = proc (baseURI, block)
30 -> formatHeading -< (level, text)
36 -> formatListElement -< (baseURI, list)
39 -> formatDefinitionList -< (baseURI, list)
42 -> formatPreformatted -< (baseURI, inlines)
45 -> formatParagraph -< (baseURI, inlines)
48 formatHeading :: ArrowXml a => a (Int, String) XmlTree
51 -> selem ("h" ++ show level) [txt text] -<< ()
54 formatListElement :: (ArrowXml a, ArrowChoice a) => a (URI, ListElement) XmlTree
56 = proc (baseURI, list)
57 -> let tag = case listType list of
62 += ( (constA baseURI &&& constL (listItems list))
68 formatListItem :: (ArrowXml a, ArrowChoice a) => a (URI, ListItem) XmlTree
70 = proc (baseURI, item)
72 += ( (arr fst &&& arrL snd)
77 formatListItem' :: (ArrowXml a, ArrowChoice a) => a (URI, Either ListElement InlineElement) XmlTree
81 Left nestedList -> formatListElement -< (baseURI, nestedList)
82 Right inline -> formatInline -< (baseURI, inline )
85 formatDefinitionList :: (ArrowXml a, ArrowChoice a) => a (URI, [Definition]) XmlTree
87 = proc (baseURI, list)
89 += ( (arr fst &&& arrL snd)
95 formatDefinition :: (ArrowXml a, ArrowChoice a) => a (URI, Definition) XmlTree
99 += ( (arr fst &&& arrL (defTerm . snd))
105 += ( (arr fst &&& arrL (defDesc . snd))
112 formatPreformatted :: (ArrowXml a, ArrowChoice a) => a (URI, [InlineElement]) XmlTree
115 += ( (arr fst &&& arrL snd)
121 formatParagraph :: (ArrowXml a, ArrowChoice a) => a (URI, [InlineElement]) XmlTree
124 += ( (arr fst &&& arrL snd)
130 formatInline :: (ArrowXml a, ArrowChoice a) => a (URI, InlineElement) XmlTree
139 += ( (arr fst &&& arrL snd)
143 ) -< (baseURI, inlines)
147 += ( (arr fst &&& arrL snd)
151 ) -< (baseURI, inlines)
153 link@(PageLink _ _ _)
154 -> formatPageLink -< (baseURI, link)
158 += (arrL id >>> attrFromPair)
162 attrFromPair :: (ArrowXml a) => a (String, String) XmlTree
163 attrFromPair = proc (name, value)
164 -> attr name (txt value) -<< ()
167 formatPageLink :: (ArrowXml a) => a (URI, InlineElement) XmlTree
169 = proc (baseURI, PageLink page fragment text)
170 -> let uri = case (page, fragment) of
171 (Just x, Just y) -> mkPageFragmentURI baseURI (fix x) y
172 (Just x, Nothing) -> mkPageURI baseURI (fix x)
173 (Nothing, Just y) -> nullURI { uriFragment = ('#':y) }
174 fix = (\ (x:xs) -> toUpper x : xs) . map (\ c -> if c == ' ' then '_' else c)
175 href = uriToString id uri ""
176 dLabel = fromMaybe "" page ++ fromMaybe "" (fmap ('#':) fragment)
177 label = fromMaybe dLabel text
180 += attr "href" (arr fst >>> mkText)
181 += (arr snd >>> mkText)