Add byExtension
compiler
This commit is contained in:
parent
730eebe989
commit
c89cfdb456
2 changed files with 34 additions and 0 deletions
|
@ -103,6 +103,7 @@ module Hakyll.Core.Compiler
|
||||||
, unsafeCompiler
|
, unsafeCompiler
|
||||||
, mapCompiler
|
, mapCompiler
|
||||||
, timedCompiler
|
, timedCompiler
|
||||||
|
, byExtension
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Prelude hiding ((.), id)
|
import Prelude hiding ((.), id)
|
||||||
|
@ -112,6 +113,7 @@ import Control.Monad.Reader (ask)
|
||||||
import Control.Monad.Trans (liftIO)
|
import Control.Monad.Trans (liftIO)
|
||||||
import Control.Category (Category, (.), id)
|
import Control.Category (Category, (.), id)
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
|
import System.FilePath (takeExtension)
|
||||||
|
|
||||||
import Data.Binary (Binary)
|
import Data.Binary (Binary)
|
||||||
import Data.Typeable (Typeable)
|
import Data.Typeable (Typeable)
|
||||||
|
@ -289,3 +291,34 @@ timedCompiler :: String -- ^ Message
|
||||||
timedCompiler msg (Compiler d j) = Compiler d $ \x -> CompilerM $ do
|
timedCompiler msg (Compiler d j) = Compiler d $ \x -> CompilerM $ do
|
||||||
logger <- compilerLogger <$> ask
|
logger <- compilerLogger <$> ask
|
||||||
timed logger msg $ unCompilerM $ j x
|
timed logger msg $ unCompilerM $ j x
|
||||||
|
|
||||||
|
-- | Choose a compiler by extension
|
||||||
|
--
|
||||||
|
-- Example:
|
||||||
|
--
|
||||||
|
-- > route "css/*" $ setExtension "css"
|
||||||
|
-- > compile "css/*" $ byExtension (error "Not a (S)CSS file")
|
||||||
|
-- > [ (".css", compressCssCompiler)
|
||||||
|
-- > , (".scss", sass)
|
||||||
|
-- > ]
|
||||||
|
--
|
||||||
|
-- This piece of code will select the @compressCssCompiler@ for @.css@ files,
|
||||||
|
-- and the @sass@ compiler (defined elsewhere) for @.scss@ files.
|
||||||
|
--
|
||||||
|
byExtension :: Compiler a b -- ^ Default compiler
|
||||||
|
-> [(String, Compiler a b)] -- ^ Choices
|
||||||
|
-> Compiler a b -- ^ Resulting compiler
|
||||||
|
byExtension defaultCompiler choices = Compiler deps job
|
||||||
|
where
|
||||||
|
-- Lookup the compiler, give an error when it is not found
|
||||||
|
lookup' identifier =
|
||||||
|
let extension = takeExtension $ toFilePath identifier
|
||||||
|
in fromMaybe defaultCompiler $ lookup extension choices
|
||||||
|
-- Collect the dependencies of the choice
|
||||||
|
deps = do
|
||||||
|
identifier <- dependencyIdentifier <$> ask
|
||||||
|
compilerDependencies $ lookup' identifier
|
||||||
|
-- Collect the job of the choice
|
||||||
|
job x = CompilerM $ do
|
||||||
|
identifier <- compilerIdentifier <$> ask
|
||||||
|
unCompilerM $ compilerJob (lookup' identifier) x
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
module Hakyll.Core.Compiler.Internal
|
module Hakyll.Core.Compiler.Internal
|
||||||
( Dependencies
|
( Dependencies
|
||||||
|
, DependencyEnvironment (..)
|
||||||
, CompilerEnvironment (..)
|
, CompilerEnvironment (..)
|
||||||
, CompilerM (..)
|
, CompilerM (..)
|
||||||
, Compiler (..)
|
, Compiler (..)
|
||||||
|
|
Loading…
Reference in a new issue