1--------------------------------------------------------------------------------
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4module Hakyll.Core.Compiler
17 , Internal.loadSnapshot
19 , Internal.loadSnapshotBody
21 , Internal.loadAllSnapshots
29--------------------------------------------------------------------------------
30import Control.Applicative ((<$>))
31import Data.Binary (Binary)
32import Data.ByteString.Lazy (ByteString)
33import Data.Typeable (Typeable)
34import Prelude hiding (id, (.))
35import System.Environment (getProgName)
38--------------------------------------------------------------------------------
39import Hakyll.Core.Compiler.Internal
40import qualified Hakyll.Core.Compiler.Require as Internal
41import Hakyll.Core.Identifier
42import Hakyll.Core.Item
43import Hakyll.Core.Logger as Logger
44import Hakyll.Core.Provider
45import Hakyll.Core.Routes
46import qualified Hakyll.Core.Store as Store
49--------------------------------------------------------------------------------
50-- | Get the underlying identifier. Only use this if you know what you're doing.
51getUnderlying :: Compiler Identifier
52getUnderlying = compilerUnderlying <$> compilerAsk
55--------------------------------------------------------------------------------
56makeItem :: a -> Compiler (Item a)
58 identifier <- getUnderlying
59 return $ Item identifier x
62--------------------------------------------------------------------------------
63-- | Get the route for a specified item
64getRoute :: Identifier -> Compiler (Maybe FilePath)
65getRoute identifier = do
66 routes <- compilerRoutes <$> compilerAsk
67 return $ runRoutes routes identifier
70--------------------------------------------------------------------------------
71-- | Get the body of the underlying resource
72getResourceBody :: Compiler (Item String)
73getResourceBody = getResourceWith resourceBody
76--------------------------------------------------------------------------------
77-- | Get the resource we are compiling as a string
78getResourceString :: Compiler (Item String)
79getResourceString = getResourceWith resourceString
82--------------------------------------------------------------------------------
83-- | Get the resource we are compiling as a lazy bytestring
84getResourceLBS :: Compiler (Item ByteString)
85getResourceLBS = getResourceWith resourceLBS
88--------------------------------------------------------------------------------
89-- | Overloadable function for 'getResourceString' and 'getResourceLBS'
90getResourceWith :: (Provider -> Identifier -> IO a) -> Compiler (Item a)
91getResourceWith reader = do
92 provider <- compilerProvider <$> compilerAsk
93 id' <- compilerUnderlying <$> compilerAsk
94 let filePath = toFilePath id'
95 if resourceExists provider id'
96 then compilerUnsafeIO $ Item id' <$> reader provider id'
97 else compilerThrow $ error' filePath
99 error' fp = "Hakyll.Core.Compiler.getResourceWith: resource " ++
100 show fp ++ " not found"
103--------------------------------------------------------------------------------
104saveSnapshot :: (Binary a, Typeable a)
105 => Internal.Snapshot -> Item a -> Compiler ()
106saveSnapshot snapshot item = do
107 store <- compilerStore <$> compilerAsk
108 compilerUnsafeIO $ Internal.saveSnapshot store snapshot item
111--------------------------------------------------------------------------------
112cached :: (Binary a, Typeable a)
116cached name compiler = do
117 id' <- compilerUnderlying <$> compilerAsk
118 store <- compilerStore <$> compilerAsk
119 provider <- compilerProvider <$> compilerAsk
120 modified <- compilerUnsafeIO $ resourceModified provider id'
124 compilerUnsafeIO $ Store.set store [name, show id'] x
127 compilerTellCacheHits 1
128 x <- compilerUnsafeIO $ Store.get store [name, show id']
129 progName <- compilerUnsafeIO getProgName
130 case x of Store.Found x' -> return x'
131 _ -> compilerThrow (error' progName)
134 "Hakyll.Core.Compiler.cached: Cache corrupt! " ++
135 "Try running: " ++ progName ++ " clean"
138--------------------------------------------------------------------------------
139unsafeCompiler :: IO a -> Compiler a
140unsafeCompiler = compilerUnsafeIO
143--------------------------------------------------------------------------------
144-- | Compiler for debugging purposes
145debugCompiler :: String -> Compiler ()
146debugCompiler msg = do
147 logger <- compilerLogger <$> compilerAsk
148 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