1--------------------------------------------------------------------------------
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4module Hakyll.Core.Compiler
22--------------------------------------------------------------------------------
23import Control.Applicative ((<$>))
24import Data.Binary (Binary)
25import Data.ByteString.Lazy (ByteString)
26import Data.Typeable (Typeable)
27import Prelude hiding (id, (.))
28import System.Environment (getProgName)
31--------------------------------------------------------------------------------
32import Hakyll.Core.Compiler.Internal
33import Hakyll.Core.Compiler.Require
34import Hakyll.Core.Dependencies
35import Hakyll.Core.Identifier
36import Hakyll.Core.Item
37import Hakyll.Core.Logger as Logger
38import Hakyll.Core.Metadata
39import Hakyll.Core.Provider
40import Hakyll.Core.Routes
41import qualified Hakyll.Core.Store as Store
44--------------------------------------------------------------------------------
45-- | Get the underlying identifier. Only use this if you know what you're doing.
46getUnderlying :: Compiler Identifier
47getUnderlying = compilerUnderlying <$> compilerAsk
50--------------------------------------------------------------------------------
51makeItem :: a -> Compiler (Item a)
53 identifier <- getUnderlying
54 return $ Item identifier x
57--------------------------------------------------------------------------------
58-- | Get the route for a specified item
59getRoute :: Identifier -> Compiler (Maybe FilePath)
60getRoute identifier = do
61 routes <- compilerRoutes <$> compilerAsk
62 return $ runRoutes routes identifier
66--------------------------------------------------------------------------------
67getMetadata :: Identifier -> Compiler Metadata
68getMetadata identifier = do
69 provider <- compilerProvider <$> compilerAsk
70 compilerTellDependencies [IdentifierDependency identifier]
71 compilerUnsafeIO $ resourceMetadata provider identifier
74--------------------------------------------------------------------------------
75-- | Get the body of the underlying resource
76getResourceBody :: Compiler (Item String)
77getResourceBody = getResourceWith resourceBody
80--------------------------------------------------------------------------------
81-- | Get the resource we are compiling as a string
82getResourceString :: Compiler (Item String)
83getResourceString = getResourceWith $ const resourceString
86--------------------------------------------------------------------------------
87-- | Get the resource we are compiling as a lazy bytestring
88getResourceLBS :: Compiler (Item ByteString)
89getResourceLBS = getResourceWith $ const resourceLBS
92--------------------------------------------------------------------------------
93-- | Overloadable function for 'getResourceString' and 'getResourceLBS'
94getResourceWith :: (Provider -> Identifier -> IO a) -> Compiler (Item a)
95getResourceWith reader = do
96 provider <- compilerProvider <$> compilerAsk
97 id' <- compilerUnderlying <$> compilerAsk
98 let filePath = toFilePath id'
99 if resourceExists provider id'
100 then compilerUnsafeIO $ Item id' <$> reader provider id'
101 else compilerThrow $ error' filePath
103 error' fp = "Hakyll.Core.Compiler.getResourceWith: resource " ++
104 show fp ++ " not found"
107--------------------------------------------------------------------------------
108cached :: (Binary a, Typeable a)
112cached name compiler = do
113 id' <- compilerUnderlying <$> compilerAsk
114 store <- compilerStore <$> compilerAsk
115 provider <- compilerProvider <$> compilerAsk
116 modified <- compilerUnsafeIO $ resourceModified provider id'
120 compilerUnsafeIO $ Store.set store [name, show id'] x
123 compilerTellCacheHits 1
124 x <- compilerUnsafeIO $ Store.get store [name, show id']
125 progName <- compilerUnsafeIO getProgName
126 case x of Store.Found x' -> return x'
127 _ -> compilerThrow (error' progName)
130 "Hakyll.Core.Compiler.cached: Cache corrupt! " ++
131 "Try running: " ++ progName ++ " clean"
134--------------------------------------------------------------------------------
135unsafeCompiler :: IO a -> Compiler a
136unsafeCompiler = compilerUnsafeIO
139--------------------------------------------------------------------------------
140-- | Compiler for debugging purposes
141debugCompiler :: String -> Compiler ()
142debugCompiler msg = do
143 logger <- compilerLogger <$> compilerAsk
144 compilerUnsafeIO $ Logger.debug logger msg
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list