X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Rakka%2FWiki%2FFormatter.hs;h=969c228a756ad93d269ff8d69d219466ece5bd99;hb=656fdb2772ab4de5cd083cbe9e7c1610cccef73b;hp=95dd9ffb021a8f55e40653c0b5208336202dd465;hpb=c2cc1241c50c8ff3843002526011574e77f669aa;p=Rakka.git diff --git a/Rakka/Wiki/Formatter.hs b/Rakka/Wiki/Formatter.hs index 95dd9ff..969c228 100644 --- a/Rakka/Wiki/Formatter.hs +++ b/Rakka/Wiki/Formatter.hs @@ -18,8 +18,17 @@ 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) + -> do block <- arrL id -< blocks + tree <- formatBlock -< (baseURI, block) + returnA -< tree + + +formatElement :: (ArrowXml a, ArrowChoice a) => a (URI, Element) XmlTree +formatElement + = proc (baseURI, elem) + -> case elem of + Block b -> formatBlock -< (baseURI, b) + Inline i -> formatInline -< (baseURI, i) formatBlock :: (ArrowXml a, ArrowChoice a) => a (URI, BlockElement) XmlTree @@ -32,7 +41,7 @@ formatBlock HorizontalLine -> eelem "hr" -< () - List list + list@(List _ _) -> formatListElement -< (baseURI, list) DefinitionList list @@ -44,14 +53,38 @@ formatBlock Paragraph inlines -> formatParagraph -< (baseURI, inlines) + Div attrs contents + -> formatElem "div" -< (baseURI, attrs, contents) + + EmptyBlock + -> none -< () + where + formatElem :: (ArrowXml a, ArrowChoice a) => + String + -> a (URI, [Attribute], [Element]) XmlTree + formatElem name + = proc (baseURI, attrs, contents) + -> ( eelem name + += ( arrL (fst . snd) + >>> + attrFromPair + ) + += ( (arr fst &&& arrL (snd . snd)) + >>> + formatElement + ) + ) -< (baseURI, (attrs, contents)) + formatHeading :: ArrowXml a => a (Int, String) XmlTree formatHeading = proc (level, text) - -> selem ("h" ++ show level) [txt text] -<< () + -> mkelem ("h" ++ show level) + [ sattr "id" text ] + [ txt text ] -<< () -formatListElement :: (ArrowXml a, ArrowChoice a) => a (URI, ListElement) XmlTree +formatListElement :: (ArrowXml a, ArrowChoice a) => a (URI, BlockElement) XmlTree formatListElement = proc (baseURI, list) -> let tag = case listType list of @@ -71,16 +104,9 @@ formatListElement -> eelem "li" += ( (arr fst &&& arrL snd) >>> - formatListItem' + formatElement ) -< (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 @@ -134,39 +160,109 @@ formatInline Text text -> mkText -< text - Italic inlines - -> ( eelem "i" - += ( (arr fst &&& arrL snd) - >>> - formatInline - ) - ) -< (baseURI, inlines) + Italic contents + -> formatElem "i" -< (baseURI, [], contents) - Bold inlines - -> ( eelem "b" - += ( (arr fst &&& arrL snd) - >>> - formatInline - ) - ) -< (baseURI, inlines) + Bold contents + -> formatElem "b" -< (baseURI, [], contents) + + link@(ObjectLink _ _) + -> formatObjectLink -< (baseURI, link) link@(PageLink _ _ _) -> formatPageLink -< (baseURI, link) + link@(ExternalLink _ _) + -> formatExternalLink -< link + + LineBreak attrs + -> formatElem "br" -< (baseURI, attrs, []) + + Span attrs contents + -> formatElem "span" -< (baseURI, attrs, contents) + + img@(Image _ _) + -> formatImage -< (baseURI, img) + + Anchor attrs contents + -> formatElem "a" -< (baseURI, attrs, contents) + + Input attrs + -> formatElem "input" -< (baseURI, attrs, []) + + EmptyInline + -> none -< () + where + formatElem :: (ArrowXml a, ArrowChoice a) => + String + -> a (URI, [Attribute], [InlineElement]) XmlTree + formatElem name + = proc (baseURI, attrs, contents) + -> ( eelem name + += ( arrL (fst . snd) + >>> + attrFromPair + ) + += ( (arr fst &&& arrL (snd . snd)) + >>> + formatInline + ) + ) -< (baseURI, (attrs, contents)) + + +attrFromPair :: (ArrowXml a) => a (String, String) XmlTree +attrFromPair = proc (name, value) + -> attr name (txt value) -<< () + + +formatObjectLink :: (ArrowXml a) => a (URI, InlineElement) XmlTree +formatObjectLink + = proc (baseURI, ObjectLink page text) + -> let uri = mkObjectURI baseURI page + href = uriToString id uri "" + label = fromMaybe ("{" ++ page ++ "}") text + in + mkAnchor -< (href, label) + 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) + (Just x, Just y) -> mkPageFragmentURI baseURI x y + (Just x, Nothing) -> mkPageURI baseURI x + (Nothing, Just y) -> mkFragmentURI y href = uriToString id uri "" dLabel = fromMaybe "" page ++ fromMaybe "" (fmap ('#':) fragment) label = fromMaybe dLabel text in - ( eelem "a" + mkAnchor -< (href, label) + + +formatImage :: (ArrowXml a) => a (URI, InlineElement) XmlTree +formatImage = proc (baseURI, Image src alt) + -> let uri = mkObjectURI baseURI src + href = uriToString id uri "" + in + ( eelem "img" + += sattr "src" href + += ( case alt of + Just x -> sattr "alt" x + Nothing -> none + ) + ) -<< () + + +formatExternalLink :: (ArrowXml a) => a InlineElement XmlTree +formatExternalLink + = proc (ExternalLink uri text) + -> let href = uriToString id uri "" + label = fromMaybe href text + in + mkAnchor -< (href, label) + + +mkAnchor :: (ArrowXml a) => a (String, String) XmlTree +mkAnchor = eelem "a" += attr "href" (arr fst >>> mkText) += (arr snd >>> mkText) - ) -< (href, label)