X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Rakka%2FWiki%2FFormatter.hs;h=3f0960604887663d1a479719339d0d8a480339bb;hb=e43bb104a7313dd696b8bb8aa3bafff94706a187;hp=7a7efce40bd4e8c010d308cae1ab2e9617e61da8;hpb=7c3065043cdfbd96539a9bf6bff9b1d4281c0b2a;p=Rakka.git diff --git a/Rakka/Wiki/Formatter.hs b/Rakka/Wiki/Formatter.hs index 7a7efce..3f09606 100644 --- a/Rakka/Wiki/Formatter.hs +++ b/Rakka/Wiki/Formatter.hs @@ -5,6 +5,7 @@ module Rakka.Wiki.Formatter import Control.Arrow import Control.Arrow.ArrowList +import Control.Arrow.ArrowTree import Data.Char import Data.List import Data.Maybe @@ -18,8 +19,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) + attachXHtmlNs -< 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 +42,7 @@ formatBlock HorizontalLine -> eelem "hr" -< () - List list + list@(List _ _) -> formatListElement -< (baseURI, list) DefinitionList list @@ -46,10 +56,13 @@ formatBlock Div attrs contents -> formatElem "div" -< (baseURI, attrs, contents) + + EmptyBlock + -> none -< () where formatElem :: (ArrowXml a, ArrowChoice a) => String - -> a (URI, [Attribute], [BlockElement]) XmlTree + -> a (URI, [Attribute], [Element]) XmlTree formatElem name = proc (baseURI, attrs, contents) -> ( eelem name @@ -59,7 +72,7 @@ formatBlock ) += ( (arr fst &&& arrL (snd . snd)) >>> - formatBlock + formatElement ) ) -< (baseURI, (attrs, contents)) @@ -72,7 +85,7 @@ formatHeading [ 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 @@ -92,16 +105,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 @@ -161,20 +167,29 @@ formatInline 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) - Image attrs - -> formatElem "img" -< (baseURI, attrs, []) + img@(Image _ _) + -> formatImage -< (baseURI, img) Anchor attrs contents -> formatElem "a" -< (baseURI, attrs, contents) + + EmptyInline + -> none -< () where formatElem :: (ArrowXml a, ArrowChoice a) => String @@ -198,19 +213,64 @@ 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) + (Just x, Just y) -> mkPageFragmentURI baseURI x y + (Just x, Nothing) -> mkPageURI baseURI 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" + mkAnchor -< (href, label) + + +formatImage :: (ArrowXml a) => a (URI, InlineElement) XmlTree +formatImage = proc (baseURI, Image name alt) + -> let uri = mkObjectURI baseURI name + 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) + + +attachXHtmlNs :: ArrowXml a => a XmlTree XmlTree +attachXHtmlNs = processBottomUp (changeQName attach') + where + attach' :: QName -> QName + attach' qn = qn { + namePrefix = "xhtml" + , namespaceUri = "http://www.w3.org/1999/xhtml" + }