match "css/*.scss" $ do
route $ setExtension "css"
compile $ sassCompilerWith scssopts
assetHashRoute :: FileHashes -> Routes
assetHashRoute fileHashes =
customRoute $ \identifier ->
fromMaybe (toFilePath identifier) (Map.lookup identifier fileHashes)
rewriteAssetUrls :: FileHashes -> Item String -> Compiler (Item String)
rewriteAssetUrls hashes item = do
route <- getRoute $ itemIdentifier item
return $ case route of
Nothing -> item
Just r -> fmap rewrite item
where
rewrite = withUrls $ \url ->
maybe url (\hashUrl -> "/" <> hashUrl) (Map.lookup (fromFilePath url) hashes)
main = hakyll $ do
imageHashes <- preprocess (mkFileHashes "images")
match "images/*" $ do
route $ assetHashRoute imageHashes
compile copyFileCompiler
match "pages/*" $ do
route $ gsubRoute "pages/" (const "") `composeRoutes` setExtension "html"
compile $ do
pandocCompiler
>>= loadAndApplyTemplate "templates/page.html" defaultContext
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= rewriteAssetUrls imageHashes
>>= relativizeUrls
loadSass :: String -> IO (Map Identifier (String, String))
loadSass dir = do
files <- getRecursiveContents (\_ -> return False) dir
toCompile <- return $ files >>=
\file -> maybe [] (\opts -> [(dir </> file, opts)]) (sassOpts file)
compileResults <- forM toCompile $
\(file, opt) -> fmap (\result -> (file, result)) $ compileFile file opt
successfullFiles <- return $ compileResults >>=
\result -> case result of
(file, Left _) -> []
(file, Right css) -> [(fromFilePath file, (replaceFileName file (hash css <> ".css"), css))]
return $ Map.fromList successfullFiles
where
sassOpts filepath =
case takeExtensions filepath of
".sass" -> Just def { sassIsIndentedSyntax = True
, sassOutputStyle = SassStyleCompressed
}
".scss" -> Just def { sassIsIndentedSyntax = False
, sassOutputStyle = SassStyleCompressed
}
_ -> Nothing
hash =
BS8.unpack. Base16.encode . SHA256.hash . BS8.pack
writeHashedContent :: Map Identifier (String, String) -> Rules ()
writeHashedContent =
sequence_ . fmap f . Map.toList
where
f (identifier, (hashedPath, css)) =
create [identifier] $ do
route $ customRoute (\identifier -> hashedPath)
compile $ makeItem css
main = hakyll $ do
imageHashes <- preprocess (mkFileHashes "images")
sass <- preprocess (loadSass "css")
match "pages/*" $ do
route $ gsubRoute "pages/" (const "") `composeRoutes` setExtension "html"
compile $ do
pandocCompiler
>>= loadAndApplyTemplate "templates/page.html" defaultContext
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= rewriteAssetUrls imageHashes
>>= rewriteAssetUrls (fmap fst sass)
>>= relativizeUrls