{-# LANGUAGE Safe #-}

{-
  This module is part of Chatty.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Antisplice with everyone you like.

  Chatty 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.

  Chatty 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 Chatty. If not, see <http://www.gnu.org/licenses/>.
-}

-- | Provides an 'ChExtendedPrinter' that handles colours using HTML output.
module Text.Chatty.Extended.HTML where

import Text.Chatty.Printer
import Text.Chatty.Expansion
import Text.Chatty.Extended.Printer
import Control.Applicative
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.IO.Class

-- | An 'ChExtendedPrinter' for HTML output.
newtype HtmlPrinterT m a = HtmlPrinter { HtmlPrinterT m a -> m a
runHtmlPrinterT :: m a }

instance Monad m => Monad (HtmlPrinterT m) where
  return :: a -> HtmlPrinterT m a
return = m a -> HtmlPrinterT m a
forall (m :: * -> *) a. m a -> HtmlPrinterT m a
HtmlPrinter (m a -> HtmlPrinterT m a) -> (a -> m a) -> a -> HtmlPrinterT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
  (HtmlPrinter m a
p) >>= :: HtmlPrinterT m a -> (a -> HtmlPrinterT m b) -> HtmlPrinterT m b
>>= a -> HtmlPrinterT m b
f = m b -> HtmlPrinterT m b
forall (m :: * -> *) a. m a -> HtmlPrinterT m a
HtmlPrinter (m b -> HtmlPrinterT m b) -> m b -> HtmlPrinterT m b
forall a b. (a -> b) -> a -> b
$ do a
p' <- m a
p; HtmlPrinterT m b -> m b
forall (m :: * -> *) a. HtmlPrinterT m a -> m a
runHtmlPrinterT (a -> HtmlPrinterT m b
f a
p')

instance MonadTrans HtmlPrinterT where
  lift :: m a -> HtmlPrinterT m a
lift = m a -> HtmlPrinterT m a
forall (m :: * -> *) a. m a -> HtmlPrinterT m a
HtmlPrinter

instance Functor m => Functor (HtmlPrinterT m) where
  fmap :: (a -> b) -> HtmlPrinterT m a -> HtmlPrinterT m b
fmap a -> b
f (HtmlPrinter m a
p) = m b -> HtmlPrinterT m b
forall (m :: * -> *) a. m a -> HtmlPrinterT m a
HtmlPrinter (m b -> HtmlPrinterT m b) -> m b -> HtmlPrinterT m b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f m a
p

instance (Functor m, Monad m) => Applicative (HtmlPrinterT m) where
  <*> :: HtmlPrinterT m (a -> b) -> HtmlPrinterT m a -> HtmlPrinterT m b
(<*>) = HtmlPrinterT m (a -> b) -> HtmlPrinterT m a -> HtmlPrinterT m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
  pure :: a -> HtmlPrinterT m a
pure = a -> HtmlPrinterT m a
forall (m :: * -> *) a. Monad m => a -> m a
return

instance MonadIO m => MonadIO (HtmlPrinterT m) where
  liftIO :: IO a -> HtmlPrinterT m a
liftIO = m a -> HtmlPrinterT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> HtmlPrinterT m a)
-> (IO a -> m a) -> IO a -> HtmlPrinterT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

instance ChPrinter m => ChPrinter (HtmlPrinterT m) where
  mprint :: String -> HtmlPrinterT m ()
mprint = m () -> HtmlPrinterT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> HtmlPrinterT m ())
-> (String -> m ()) -> String -> HtmlPrinterT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mprint (String -> m ()) -> (String -> String) -> String -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> String) -> String -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Char -> String
maskHtml
  mnoecho :: String -> HtmlPrinterT m ()
mnoecho = m () -> HtmlPrinterT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> HtmlPrinterT m ())
-> (String -> m ()) -> String -> HtmlPrinterT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mnoecho (String -> m ()) -> (String -> String) -> String -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> String) -> String -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Char -> String
maskHtml
  mflush :: HtmlPrinterT m ()
mflush = m () -> HtmlPrinterT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m ()
forall (m :: * -> *). ChPrinter m => m ()
mflush
  mnomask :: String -> HtmlPrinterT m ()
mnomask = m () -> HtmlPrinterT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> HtmlPrinterT m ())
-> (String -> m ()) -> String -> HtmlPrinterT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mnomask

instance ChPrinter m => ChExtendedPrinter (HtmlPrinterT m) where
  estart :: Colour -> HtmlPrinterT m ()
estart Colour
c =  m () -> HtmlPrinterT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> HtmlPrinterT m ()) -> m () -> HtmlPrinterT m ()
forall a b. (a -> b) -> a -> b
$ String -> m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mprint (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [String
"<span style=\"color: #", Colour -> String
hexColour Colour
c, String
";\">"]
  efin :: HtmlPrinterT m ()
efin = m () -> HtmlPrinterT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> HtmlPrinterT m ()) -> m () -> HtmlPrinterT m ()
forall a b. (a -> b) -> a -> b
$ String -> m ()
forall (m :: * -> *). ChPrinter m => String -> m ()
mprint String
"</span>"

instance (Functor m,ChExpand m) => ChExpand (HtmlPrinterT m) where
  expand :: String -> HtmlPrinterT m String
expand = m String -> HtmlPrinterT m String
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m String -> HtmlPrinterT m String)
-> (String -> m String) -> String -> HtmlPrinterT m String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> m String
forall (e :: * -> *). ChExpand e => String -> e String
expand (String -> HtmlPrinterT m String)
-> (String -> HtmlPrinterT m String)
-> String
-> HtmlPrinterT m String
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< (((), Replayable) -> String)
-> HtmlPrinterT m ((), Replayable) -> HtmlPrinterT m String
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (Replayable -> String
replay(Replayable -> String)
-> (((), Replayable) -> Replayable) -> ((), Replayable) -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.((), Replayable) -> Replayable
forall a b. (a, b) -> b
snd) (HtmlPrinterT m ((), Replayable) -> HtmlPrinterT m String)
-> (String -> HtmlPrinterT m ((), Replayable))
-> String
-> HtmlPrinterT m String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RecorderT (HtmlPrinterT m) () -> HtmlPrinterT m ((), Replayable)
forall (m :: * -> *) a.
(Functor m, Monad m) =>
RecorderT m a -> m (a, Replayable)
runRecorderT (RecorderT (HtmlPrinterT m) () -> HtmlPrinterT m ((), Replayable))
-> (String -> RecorderT (HtmlPrinterT m) ())
-> String
-> HtmlPrinterT m ((), Replayable)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HtmlPrinterT (RecorderT (HtmlPrinterT m)) ()
-> RecorderT (HtmlPrinterT m) ()
forall (m :: * -> *) a. HtmlPrinterT m a -> m a
runHtmlPrinterT (HtmlPrinterT (RecorderT (HtmlPrinterT m)) ()
 -> RecorderT (HtmlPrinterT m) ())
-> (String -> HtmlPrinterT (RecorderT (HtmlPrinterT m)) ())
-> String
-> RecorderT (HtmlPrinterT m) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> HtmlPrinterT (RecorderT (HtmlPrinterT m)) ()
forall (m :: * -> *). ChExtendedPrinter m => String -> m ()
expandClr

-- | Convert the given character to its HTML representation.
maskHtml :: Char -> String
maskHtml :: Char -> String
maskHtml Char
'&' = String
"&amp;"
maskHtml Char
'<' = String
"&lt;"
maskHtml Char
'>' = String
"&gt;"
--maskHtml ' ' = "&nbsp;"
maskHtml Char
c = [Char
c]

-- | Convert the given colour to its CSS representation.
hexColour :: Colour -> String
hexColour (Dull Tone
Green) = String
"007F00"
hexColour (Vivid Tone
Green) = String
"00FF00"
hexColour (Dull Tone
Red) = String
"7F0000"
hexColour (Vivid Tone
Red) = String
"FF0000"
hexColour (Dull Tone
Yellow) = String
"7F7F00"
hexColour (Vivid Tone
Yellow) = String
"FFFF00"
hexColour (Dull Tone
Blue) = String
"00007F"
hexColour (Vivid Tone
Blue) = String
"0000FF"
hexColour (Dull Tone
Black) = String
"000000"
hexColour (Vivid Tone
Black) = String
"606060"
hexColour (Dull Tone
White) = String
"909090"
hexColour (Vivid Tone
White) = String
"FFFFFF"
hexColour (Dull Tone
Cyan) = String
"007F7F"
hexColour (Vivid Tone
Cyan) = String
"00FFFF"
hexColour (Dull Tone
Magenta) = String
"7F007F"
hexColour (Vivid Tone
Magenta) = String
"FF00FF"