{- Copyright (C) 2013 John Lenz This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . -} {-# LANGUAGE RecordWildCards, ScopedTypeVariables #-} module Handler.View( threadWidget , threadHeader , getThreadR , getMessagePartR ) where import Import import FilterHtml import StaticFiles import Handler.Tags import Blaze.ByteString.Builder (fromByteString) import Network.HTTP.Types (status200) import Network.Wai (Response(..)) import Text.Blaze.Html.Renderer.Text (renderHtml) import Text.Markdown import qualified Data.ByteString as B import qualified Data.CaseInsensitive as CI import qualified Data.Conduit.List as C import qualified Data.Conduit.Text as C import qualified Data.Map as M import qualified Data.Text as T import qualified Data.Text.Encoding as T import qualified Data.Text.Lazy as TL import qualified Data.Tree as TR #ifdef USE_ICU import qualified Data.Text.ICU.Convert as ICU #endif decodePart :: (MonadLogger m, MonadHandler m) => Maybe (CI.CI T.Text) -> Source (ResourceT IO) B.ByteString -> m TL.Text decodePart charset src = case charset of Just "ISO-8859-1" -> decodeConduit C.iso8859_1 Just "UTF-8" -> decodeConduit C.utf8 #ifdef USE_ICU Just x -> decodeICU $ CI.original x #endif _ -> decodeConduit C.utf8 where decodeConduit c = TL.fromChunks <$> liftResourceT (src $= C.decode c $$ C.consume) #ifdef USE_ICU decodeICU x = do $(logInfo) ("Decoding using ICU: " `T.append` x) raw <- liftResourceT (src $$ C.consume) c <- liftIO $ ICU.open (T.unpack x) (Just True) return $ TL.fromStrict $ ICU.toUnicode c $ B.concat raw #endif messagePart :: MessageID -> Bool -> MessagePart -> Widget messagePart mid _ p@(MessagePart {partContentType = "text/html"}) = do let ((_ :: IO MessagePart), src) = notmuchMessagePart mid $ partID p html <- TL.toStrict <$> decodePart (partContentCharset p) src [whamlet|
#{preEscapedToMarkup $ filterHtml html} |] messagePart mid _ m@(MessagePart {partContent = ContentText ""}) = [whamlet|

$case partContentFilename m $of Just f #{f} $of Nothing No filename (#{CI.original $ partContentType m}) |] -- Text which is part of an alternative messagePart _ True (MessagePart {partContent = ContentText txt}) = [whamlet|

    #{txt}
|]

-- Text not part of an alternative
messagePart _ False (MessagePart {partContent = ContentText txt}) = do
    let html = TL.toStrict $ renderHtml $ markdown markdownSettings $ TL.fromStrict txt
    htmlId <- newIdent
    txtId <- newIdent
    [whamlet|