module Shikensu.Contrib
( clearMetadata
, clearMetadataDef
, clone
, copyPropsToMetadata
, copyPropsToMetadataDef
, exclude
, insertMetadata
, insertMetadataDef
, permalink
, permalinkDef
, prefixDirname
, prefixDirnameDef
, rename
, renameDef
, renameExt
, renameExtDef
, renderContent
, renderContentDef
, replaceMetadata
, replaceMetadataDef
, setContent
, setContentDef
, transformContent
, transformContentDef
) where
import Data.ByteString (ByteString)
import Data.Monoid ((<>))
import Shikensu (forkDefinition)
import Shikensu.Internal.Types
import Shikensu.Internal.Utilities (compileParentPath, compilePathToRoot)
import Shikensu.Metadata (transposeToMetadata)
import System.FilePath (FilePath, combine)
import qualified Data.HashMap.Strict as HashMap (empty, union)
clearMetadata :: Dictionary -> Dictionary
clearMetadata =
fmap clearMetadataDef
clearMetadataDef :: Definition -> Definition
clearMetadataDef def =
def { metadata = HashMap.empty }
clone :: FilePath -> FilePath -> Dictionary -> Dictionary
clone existingPath newPath dict =
let
makeNew = \def acc ->
if localPath def == existingPath
then acc <> [forkDefinition newPath def]
else acc
in
dict <> foldr makeNew [] dict
copyPropsToMetadata :: Dictionary -> Dictionary
copyPropsToMetadata =
fmap copyPropsToMetadataDef
copyPropsToMetadataDef :: Definition -> Definition
copyPropsToMetadataDef def =
def
{ metadata = HashMap.union (transposeToMetadata def) (metadata def) }
exclude :: FilePath -> Dictionary -> Dictionary
exclude path =
filter (\def -> localPath def /= path)
insertMetadata :: Metadata -> Dictionary -> Dictionary
insertMetadata a =
fmap (insertMetadataDef a)
insertMetadataDef :: Metadata -> Definition -> Definition
insertMetadataDef given def =
def { metadata = HashMap.union given (metadata def) }
permalink :: String -> Dictionary -> Dictionary
permalink a =
fmap (permalinkDef a)
permalinkDef :: String -> Definition -> Definition
permalinkDef newBasename def =
if basename def /= newBasename then
let
newDirname = combine (dirname def) (basename def)
in
def
{ basename = newBasename
, dirname = newDirname
, parentPath = compileParentPath newDirname
, pathToRoot = compilePathToRoot newDirname
}
else
def
prefixDirname :: String -> Dictionary -> Dictionary
prefixDirname prefix = fmap (prefixDirnameDef prefix)
prefixDirnameDef :: String -> Definition -> Definition
prefixDirnameDef prefix def =
let
newDirname = prefix <> dirname def
in
def
{ dirname = newDirname
, parentPath = compileParentPath newDirname
, pathToRoot = compilePathToRoot newDirname
}
rename :: FilePath -> FilePath -> Dictionary -> Dictionary
rename a b = fmap (renameDef a b)
renameDef :: FilePath -> FilePath -> Definition -> Definition
renameDef oldPath newPath def =
if localPath def == oldPath
then forkDefinition newPath def
else def
renameExt :: String -> String -> Dictionary -> Dictionary
renameExt a b =
fmap (renameExtDef a b)
renameExtDef :: String -> String -> Definition -> Definition
renameExtDef oldExtname newExtname def =
if extname def == oldExtname
then def { extname = newExtname }
else def
renderContent :: (Definition -> Maybe ByteString) -> Dictionary -> Dictionary
renderContent a =
fmap (renderContentDef a)
renderContentDef :: (Definition -> Maybe ByteString) -> Definition -> Definition
renderContentDef renderer def =
def { content = renderer def }
replaceMetadata :: Metadata -> Dictionary -> Dictionary
replaceMetadata a =
fmap (replaceMetadataDef a)
replaceMetadataDef :: Metadata -> Definition -> Definition
replaceMetadataDef given def =
def { metadata = given }
setContent :: ByteString -> Dictionary -> Dictionary
setContent content =
fmap (setContentDef content)
setContentDef :: ByteString -> Definition -> Definition
setContentDef content def =
def { content = Just content }
transformContent :: (Definition -> Maybe ByteString) -> Dictionary -> Dictionary
transformContent = renderContent
transformContentDef :: (Definition -> Maybe ByteString) -> Definition -> Definition
transformContentDef =
renderContentDef