module Rakka.Wiki.Formatter ( formatWikiBlocks ) where import Control.Arrow import Control.Arrow.ArrowList import Data.Char import Data.List import Data.Maybe import Network.URI import Rakka.Page import Rakka.Wiki import Text.XML.HXT.Arrow.XmlArrow import Text.XML.HXT.DOM.TypeDefs formatWikiBlocks :: (ArrowXml a, ArrowChoice a) => a (URI, [BlockElement]) XmlTree formatWikiBlocks = proc (baseURI, blocks) -> do block <- arrL id -< blocks formatBlock -< (baseURI, block) formatBlock :: (ArrowXml a, ArrowChoice a) => a (URI, BlockElement) XmlTree formatBlock = proc (baseURI, block) -> case block of Heading level text -> formatHeading -< (level, text) HorizontalLine -> eelem "hr" -< () List list -> formatListElement -< (baseURI, list) DefinitionList list -> formatDefinitionList -< (baseURI, list) LeadingSpaced inlines -> formatLeadingSpaced -< (baseURI, inlines) Paragraph inlines -> formatParagraph -< (baseURI, inlines) formatHeading :: ArrowXml a => a (Int, String) XmlTree formatHeading = proc (level, text) -> selem ("h" ++ show level) [txt text] -<< () formatListElement :: (ArrowXml a, ArrowChoice a) => a (URI, ListElement) XmlTree formatListElement = proc (baseURI, list) -> let tag = case listType list of Bullet -> "ul" Numbered -> "ol" in ( eelem tag += ( (constA baseURI &&& constL (listItems list)) >>> formatListItem ) ) -<< () where formatListItem :: (ArrowXml a, ArrowChoice a) => a (URI, ListItem) XmlTree formatListItem = proc (baseURI, item) -> eelem "li" += ( (arr fst &&& arrL snd) >>> formatListItem' ) -< (baseURI, item) formatListItem' :: (ArrowXml a, ArrowChoice a) => a (URI, Either ListElement InlineElement) XmlTree formatListItem' = proc (baseURI, x) -> case x of Left nestedList -> formatListElement -< (baseURI, nestedList) Right inline -> formatInline -< (baseURI, inline ) formatDefinitionList :: (ArrowXml a, ArrowChoice a) => a (URI, [Definition]) XmlTree formatDefinitionList = proc (baseURI, list) -> ( eelem "dl" += ( (arr fst &&& arrL snd) >>> formatDefinition ) ) -< (baseURI, list) where formatDefinition :: (ArrowXml a, ArrowChoice a) => a (URI, Definition) XmlTree formatDefinition = proc (baseURI, def) -> ( eelem "dt" += ( (arr fst &&& arrL (defTerm . snd)) >>> formatInline ) <+> eelem "dd" += ( (arr fst &&& arrL (defDesc . snd)) >>> formatInline ) ) -< (baseURI, def) formatLeadingSpaced :: (ArrowXml a, ArrowChoice a) => a (URI, [InlineElement]) XmlTree formatLeadingSpaced = eelem "pre" += ( (arr fst &&& arrL snd) >>> formatInline ) formatParagraph :: (ArrowXml a, ArrowChoice a) => a (URI, [InlineElement]) XmlTree formatParagraph = eelem "p" += ( (arr fst &&& arrL snd) >>> formatInline ) formatInline :: (ArrowXml a, ArrowChoice a) => a (URI, InlineElement) XmlTree formatInline = proc (baseURI, i) -> case i of Text text -> mkText -< text link@(PageLink _ _ _) -> formatPageLink -< (baseURI, link) formatPageLink :: (ArrowXml a) => a (URI, InlineElement) XmlTree formatPageLink = proc (baseURI, PageLink page fragment text) -> let uri = case (page, fragment) of (Just x, Just y) -> mkPageFragmentURI baseURI (fix x) y (Just x, Nothing) -> mkPageURI baseURI (fix x) (Nothing, Just y) -> nullURI { uriFragment = ('#':y) } fix = (\ (x:xs) -> toUpper x : xs) . map (\ c -> if c == ' ' then '_' else c) href = uriToString id uri "" dLabel = fromMaybe "" page ++ fromMaybe "" (fmap ('#':) fragment) label = fromMaybe dLabel text in ( eelem "a" += attr "href" (arr fst >>> mkText) += (arr snd >>> mkText) ) -< (href, label)