{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-missing-fields #-}
module Text.Cassius
(
Css
, CssUrl
, ToCss (..)
, renderCss
, renderCssUrl
, cassius
, cassiusFile
, cassiusFileDebug
, cassiusFileReload
, cassiusMixin
, Mixin
, Color (..)
, colorRed
, colorBlack
, mkSize
, AbsoluteUnit (..)
, AbsoluteSize (..)
, absoluteSize
, EmSize (..)
, ExSize (..)
, PercentageSize (..)
, percentageSize
, PixelSize (..)
, cassiusUsedIdentifiers
) where
import Text.Internal.Css
import Text.Shakespeare.Base
import Text.Shakespeare (VarType)
import Language.Haskell.TH.Quote (QuasiQuoter (..))
import Language.Haskell.TH.Syntax
import qualified Data.Text.Lazy as TL
import Text.Internal.CssCommon
import Text.Lucius (lucius)
import qualified Text.Lucius
import Text.IndentToBrace (i2b)
cassius :: QuasiQuoter
cassius = QuasiQuoter { quoteExp = quoteExp lucius . i2b }
cassiusFile :: FilePath -> Q Exp
cassiusFile fp = do
contents <- readFileRecompileQ fp
quoteExp cassius contents
cassiusFileDebug, cassiusFileReload :: FilePath -> Q Exp
cassiusFileDebug = cssFileDebug True [|Text.Lucius.parseTopLevels|] Text.Lucius.parseTopLevels
cassiusFileReload = cassiusFileDebug
cassiusUsedIdentifiers :: String -> [(Deref, VarType)]
cassiusUsedIdentifiers = cssUsedIdentifiers True Text.Lucius.parseTopLevels
cassiusMixin :: QuasiQuoter
cassiusMixin = QuasiQuoter
{ quoteExp = quoteExp Text.Lucius.luciusMixin . i2bMixin
}
i2bMixin :: String -> String
i2bMixin s' =
TL.unpack
$ stripEnd "}"
$ stripFront "mixin {"
$ TL.strip
$ TL.pack
$ i2b
$ unlines
$ "mixin" : (map (" " ++) $ lines s')
where
stripFront x y =
case TL.stripPrefix x y of
Nothing -> y
Just z -> z
stripEnd x y =
case TL.stripSuffix x y of
Nothing -> y
Just z -> z