1--------------------------------------------------------------------------------
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4module Hakyll.Core.Compiler
7 , getUnderlyingExtension
18 , Internal.loadSnapshot
20 , Internal.loadSnapshotBody
22 , Internal.loadAllSnapshots
30--------------------------------------------------------------------------------
31import Control.Applicative ((<$>))
32import Data.Binary (Binary)
33import Data.ByteString.Lazy (ByteString)
34import Data.Typeable (Typeable)
35import System.Environment (getProgName)
36import System.FilePath (takeExtension)
39--------------------------------------------------------------------------------
40import Hakyll.Core.Compiler.Internal
41import qualified Hakyll.Core.Compiler.Require as Internal
42import Hakyll.Core.Dependencies
43import Hakyll.Core.Identifier
44import Hakyll.Core.Item
45import Hakyll.Core.Logger as Logger
46import Hakyll.Core.Provider
47import Hakyll.Core.Routes
48import qualified Hakyll.Core.Store as Store
51--------------------------------------------------------------------------------
52-- | Get the underlying identifier.
53getUnderlying :: Compiler Identifier
54getUnderlying = compilerUnderlying <$> compilerAsk
57--------------------------------------------------------------------------------
58-- | Get the extension of the underlying identifier. Returns something like
60getUnderlyingExtension :: Compiler String
61getUnderlyingExtension = takeExtension . toFilePath <$> getUnderlying
64--------------------------------------------------------------------------------
65makeItem :: a -> Compiler (Item a)
67 identifier <- getUnderlying
68 return $ Item identifier x
71--------------------------------------------------------------------------------
72-- | Get the route for a specified item
73getRoute :: Identifier -> Compiler (Maybe FilePath)
74getRoute identifier = do
75 provider <- compilerProvider <$> compilerAsk
76 routes <- compilerRoutes <$> compilerAsk
77 -- Note that this makes us dependend on that identifier: when the metadata
78 -- of that item changes, the route may change, hence we have to recompile
79 compilerTellDependencies [IdentifierDependency identifier]
80 compilerUnsafeIO $ runRoutes routes provider identifier
83--------------------------------------------------------------------------------
84-- | Get the body of the underlying resource
85getResourceBody :: Compiler (Item String)
86getResourceBody = getResourceWith resourceBody
89--------------------------------------------------------------------------------
90-- | Get the resource we are compiling as a string
91getResourceString :: Compiler (Item String)
92getResourceString = getResourceWith resourceString
95--------------------------------------------------------------------------------
96-- | Get the resource we are compiling as a lazy bytestring
97getResourceLBS :: Compiler (Item ByteString)
98getResourceLBS = getResourceWith resourceLBS
101--------------------------------------------------------------------------------
102-- | Overloadable function for 'getResourceString' and 'getResourceLBS'
103getResourceWith :: (Provider -> Identifier -> IO a) -> Compiler (Item a)
104getResourceWith reader = do
105 provider <- compilerProvider <$> compilerAsk
106 id' <- compilerUnderlying <$> compilerAsk
107 let filePath = toFilePath id'
108 if resourceExists provider id'
109 then compilerUnsafeIO $ Item id' <$> reader provider id'
110 else compilerThrow $ error' filePath
112 error' fp = "Hakyll.Core.Compiler.getResourceWith: resource " ++
113 show fp ++ " not found"
116--------------------------------------------------------------------------------
117-- | Save a snapshot of the item. This function returns the same item, which
118-- convenient for building '>>=' chains.
119saveSnapshot :: (Binary a, Typeable a)
120 => Internal.Snapshot -> Item a -> Compiler (Item a)
121saveSnapshot snapshot item = do
122 store <- compilerStore <$> compilerAsk
123 logger <- compilerLogger <$> compilerAsk
124 compilerUnsafeIO $ do
125 Logger.debug logger $ "Storing snapshot: " ++ snapshot
126 Internal.saveSnapshot store snapshot item
130--------------------------------------------------------------------------------
131cached :: (Binary a, Typeable a)
135cached name compiler = do
136 id' <- compilerUnderlying <$> compilerAsk
137 store <- compilerStore <$> compilerAsk
138 provider <- compilerProvider <$> compilerAsk
139 let modified = resourceModified provider id'
143 compilerUnsafeIO $ Store.set store [name, show id'] x
146 compilerTellCacheHits 1
147 x <- compilerUnsafeIO $ Store.get store [name, show id']
148 progName <- compilerUnsafeIO getProgName
149 case x of Store.Found x' -> return x'
150 _ -> compilerThrow (error' progName)
153 "Hakyll.Core.Compiler.cached: Cache corrupt! " ++
154 "Try running: " ++ progName ++ " clean"
157--------------------------------------------------------------------------------
158unsafeCompiler :: IO a -> Compiler a
159unsafeCompiler = compilerUnsafeIO
162--------------------------------------------------------------------------------
163-- | Compiler for debugging purposes
164debugCompiler :: String -> Compiler ()
165debugCompiler msg = do
166 logger <- compilerLogger <$> compilerAsk
167 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