X-Git-Url: https://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Rakka%2FWiki%2FFormatter.hs;h=c1e63f5c05138ab1a4caa4b4de7f84b3dd8a49fe;hb=42f51754dea02201aececaacbf194d714cd58aaf;hp=b81c510594f75253678f9dfc7995899e627615c5;hpb=3c5211253dc61c31196a47486c538b64c32d8c5e;p=Rakka.git diff --git a/Rakka/Wiki/Formatter.hs b/Rakka/Wiki/Formatter.hs index b81c510..c1e63f5 100644 --- a/Rakka/Wiki/Formatter.hs +++ b/Rakka/Wiki/Formatter.hs @@ -1,40 +1,57 @@ +{-# LANGUAGE + Arrows + , OverloadedStrings + , TypeOperators + , UnicodeSyntax + , ViewPatterns + #-} module Rakka.Wiki.Formatter ( formatWikiBlocks ) where - -import Control.Arrow -import Control.Arrow.ArrowList -import Control.Arrow.ArrowTree -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 - +import Control.Arrow +import Control.Arrow.ArrowIf +import Control.Arrow.ArrowList +import Control.Arrow.ArrowTree +import Control.Arrow.Unicode +import qualified Data.CaseInsensitive as CS +import Data.Maybe +import Data.Monoid.Unicode +import Data.Text (Text) +import qualified Data.Text as T +import Network.URI hiding (fragment) +import Prelude.Unicode +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 tree <- formatBlock -< (baseURI, block) - attachXHtmlNs -< tree + attachXHtmlNS -< tree + +formatElement :: (ArrowXml a, ArrowChoice a) => a (URI, Element) XmlTree +formatElement + = proc (baseURI, e) + -> case e of + Block b -> formatBlock -< (baseURI, b) + Inline i -> formatInline -< (baseURI, i) -formatBlock :: (ArrowXml a, ArrowChoice a) => a (URI, BlockElement) XmlTree +formatBlock ∷ (ArrowXml (⇝), ArrowChoice (⇝)) ⇒ (URI, BlockElement) ⇝ XmlTree formatBlock = proc (baseURI, block) - -> case block of + → case block of Heading level text - -> formatHeading -< (level, text) + → formatHeading ⤙ (level, text) HorizontalLine -> eelem "hr" -< () - List list + list@(List _ _) -> formatListElement -< (baseURI, list) DefinitionList list @@ -51,10 +68,12 @@ formatBlock EmptyBlock -> none -< () + + _ -> arr (error . ("formatBlock: unsupported BlockElement: " ++) . show) -< block 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 @@ -64,20 +83,20 @@ formatBlock ) += ( (arr fst &&& arrL (snd . snd)) >>> - formatBlock + formatElement ) ) -< (baseURI, (attrs, contents)) -formatHeading :: ArrowXml a => a (Int, String) XmlTree +formatHeading ∷ ArrowXml (⇝) ⇒ (Int, Text) ⇝ XmlTree formatHeading - = proc (level, text) - -> mkelem ("h" ++ show level) + = proc (level, T.unpack → text) + -> mkelem ("h" ⊕ show level) [ sattr "id" text ] - [ txt 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 @@ -97,16 +116,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 @@ -152,13 +164,12 @@ formatParagraph formatInline ) - -formatInline :: (ArrowXml a, ArrowChoice a) => a (URI, InlineElement) XmlTree +formatInline ∷ (ArrowXml (⇝), ArrowChoice (⇝)) ⇒ (URI, InlineElement) ⇝ XmlTree formatInline = proc (baseURI, i) - -> case i of + → case i of Text text - -> mkText -< text + → mkText ⤙ T.unpack text Italic contents -> formatElem "i" -< (baseURI, [], contents) @@ -166,6 +177,9 @@ formatInline Bold contents -> formatElem "b" -< (baseURI, [], contents) + link@(ObjectLink _ _) + -> formatObjectLink -< (baseURI, link) + link@(PageLink _ _ _) -> formatPageLink -< (baseURI, link) @@ -178,72 +192,92 @@ formatInline 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) + Input attrs + -> formatElem "input" -< (baseURI, attrs, []) + EmptyInline -> none -< () + + _ -> arr (error . ("formatInline: unsupported InlineElement: " ++) . show) -< i where - formatElem :: (ArrowXml a, ArrowChoice a) => + formatElem ∷ (ArrowXml (⇝), ArrowChoice (⇝)) ⇒ String - -> a (URI, [Attribute], [InlineElement]) XmlTree + → (URI, [Attribute], [InlineElement]) ⇝ XmlTree formatElem name = proc (baseURI, attrs, contents) - -> ( eelem name - += ( arrL (fst . snd) - >>> - attrFromPair + → ( eelem name + += ( arrL (fst ∘ snd) + ⋙ + attrFromPair ) += ( (arr fst &&& arrL (snd . snd)) - >>> + ⋙ formatInline ) - ) -< (baseURI, (attrs, contents)) - + ) ⤙ (baseURI, (attrs, contents)) -attrFromPair :: (ArrowXml a) => a (String, String) XmlTree -attrFromPair = proc (name, value) - -> attr name (txt value) -<< () +attrFromPair ∷ ArrowXml (⇝) ⇒ Attribute ⇝ XmlTree +attrFromPair = proc (T.unpack ∘ CS.original → name, T.unpack → value) + → attr name (txt value) ⤛ () +formatObjectLink ∷ ArrowXml (⇝) ⇒ (URI, InlineElement) ⇝ XmlTree +formatObjectLink + = proc (baseURI, ObjectLink page text) + → let uri = mkObjectURI baseURI page + label = fromMaybe ("{" ⊕ page ⊕ "}") text + in + mkAnchor ⤙ (uri, label) -formatPageLink :: (ArrowXml a) => a (URI, InlineElement) XmlTree +formatPageLink ∷ ArrowXml (⇝) ⇒ (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) - - -formatExternalLink :: (ArrowXml a) => a InlineElement XmlTree + → let uri = case (page, fragment) of + (Just x, Just y) → mkPageFragmentURI baseURI x y + (Just x, Nothing) → mkPageURI baseURI x + (Nothing, Just y) → mkFragmentURI y + _ → (⊥) + dLabel = fromMaybe (∅) page ⊕ maybe (∅) (T.cons '#') fragment + label = fromMaybe dLabel text + in + mkAnchor ⤙ (uri, label) + +formatImage ∷ ArrowXml (⇝) ⇒ (URI, InlineElement) ⇝ XmlTree +formatImage = proc (baseURI, Image src alt) + → let uri = case src of + Left u → u + Right name → mkObjectURI baseURI name + href = uriToString id uri "" + in + ( eelem "img" + += sattr "src" href + += ( case alt of + Just x → sattr "alt" (T.unpack x) + Nothing → none + ) + ) ⤛ () + + +formatExternalLink ∷ ArrowXml (⇝) ⇒ InlineElement ⇝ XmlTree formatExternalLink = proc (ExternalLink uri text) - -> let href = uriToString id uri "" - label = fromMaybe href text - in - ( eelem "a" - += attr "href" (arr fst >>> mkText) - += (arr snd >>> mkText) - ) -< (href, label) - - -attachXHtmlNs :: ArrowXml a => a XmlTree XmlTree -attachXHtmlNs = processBottomUp (changeQName attach') + → let href = uriToString id uri "" + label = fromMaybe (T.pack href) text + in + mkAnchor -< (uri, label) + +mkAnchor ∷ ArrowXml (⇝) ⇒ (URI, Text) ⇝ XmlTree +mkAnchor = eelem "a" + += attr "href" (arr (flip (uriToString id) "" ∘ fst) ⋙ mkText) + += (arr (T.unpack ∘ snd) ⋙ mkText) + +attachXHtmlNS ∷ ArrowXml (⇝) ⇒ XmlTree ⇝ XmlTree +attachXHtmlNS = processTopDown (changeQName attach `when` isElem) where - attach' :: QName -> QName - attach' qn = qn { - namePrefix = "xhtml" - , namespaceUri = "http://www.w3.org/1999/xhtml" - } + attach ∷ QName → QName + attach = setNamespaceUri' (newXName "http://www.w3.org/1999/xhtml")