on master help sign in
Moved some modules around for fun and profit.
commitd25b0b683410211530c977625685349b11b8ff72 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateThu 11 Mar 2010, 11:03 (17 years ago) parent35e8246
14 files changed+111−106
hakyll.cabal+2−1 src/Text/Hakyll.hs+1−1 src/Text/Hakyll/Context.hs+2−83 src/Text/Hakyll/ContextManipulations.hs+84 src/Text/Hakyll/Feed.hs+3−2 src/Text/Hakyll/File.hs+1−1 src/Text/Hakyll/HakyllAction.hs+1−1 src/Text/Hakyll/Hakyll.hs → src/Text/Hakyll/HakyllMonad.hs+6−9 src/Text/Hakyll/Internal/Cache.hs+3−2 src/Text/Hakyll/Internal/Page.hs+1−1 src/Text/Hakyll/Internal/Template.hs+1−1 src/Text/Hakyll/Paginate.hs+1−1 src/Text/Hakyll/Render.hs+2−1 src/Text/Hakyll/Tags.hs+3−2
hakyll.cabal+2−1
@@ -44,8 +44,9 @@ library
4444 exposed-modules: Network.Hakyll.SimpleServer
4545 Text.Hakyll
4646 Text.Hakyll.Context
47+ Text.Hakyll.ContextManipulations
4748 Text.Hakyll.File
48− Text.Hakyll.Hakyll
49+ Text.Hakyll.HakyllMonad
4950 Text.Hakyll.Regex
5051 Text.Hakyll.Render
5152 Text.Hakyll.HakyllAction
src/Text/Hakyll.hs+1−1
@@ -22,7 +22,7 @@ import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
2222import System.Time (getClockTime)
2323
2424import Network.Hakyll.SimpleServer (simpleServer)
25−import Text.Hakyll.Hakyll
25+import Text.Hakyll.HakyllMonad
2626import Text.Hakyll.File
2727
2828-- | The default hakyll configuration.
src/Text/Hakyll/Context.hs+2−83
@@ -1,88 +1,7 @@
1−-- | Module containing various functions to manipulate contexts.
21module Text.Hakyll.Context
32 ( Context
4− , ContextManipulation
5− , renderValue
6− , changeValue
7− , copyValue
8− , renderDate
9− , changeExtension
103 ) where
114
12−import Control.Arrow (arr)
13−import System.Locale (defaultTimeLocale)
14−import System.FilePath (takeFileName, addExtension, dropExtension)
15−import Data.Time.Format (parseTime, formatTime)
16−import Data.Time.Clock (UTCTime)
17−import Data.Maybe (fromMaybe)
18−import qualified Data.Map as M
5+import Data.Map (Map)
196
20−import Text.Hakyll.Regex (substituteRegex)
21−import Text.Hakyll.HakyllAction (HakyllAction)
22−import Text.Hakyll.Hakyll (Context)
23−
24−-- | Type for context manipulating functions.
25−type ContextManipulation = Context -> Context
26−
27−-- | Do something with a value in a @Context@, but keep the old value as well.
28−-- This is probably the most common function to construct a
29−-- @ContextManipulation@.
30−renderValue :: String -- ^ Key of which the value should be copied.
31− -> String -- ^ Key the value should be copied to.
32− -> (String -> String) -- ^ Function to apply on the value.
33− -> HakyllAction Context Context
34−renderValue source destination f = arr $ \context ->
35− case M.lookup source context of
36− Nothing -> context
37− (Just value) -> M.insert destination (f value) context
38−
39−-- | Change a value in a @Context@.
40−--
41−-- > import Data.Char (toUpper)
42−-- > changeValue "title" (map toUpper)
43−--
44−-- Will put the title in UPPERCASE.
45−changeValue :: String -- ^ Key to change.
46− -> (String -> String) -- ^ Function to apply on the value.
47− -> HakyllAction Context Context
48−changeValue key = renderValue key key
49−
50−-- | Copy a value from one key to another in a @Context@.
51−copyValue :: String -- ^ Source key.
52− -> String -- ^ Destination key.
53− -> HakyllAction Context Context
54−copyValue source destination = renderValue source destination id
55−
56−-- | When the context has a key called @path@ in a @yyyy-mm-dd-title.extension@
57−-- format (default for pages), this function can render the date.
58−--
59−-- > renderDate "date" "%B %e, %Y" "Date unknown"
60−--
61−-- Will render something like @January 32, 2010@.
62−renderDate :: String -- ^ Key in which the rendered date should be placed.
63− -> String -- ^ Format to use on the date.
64− -> String -- ^ Default key, in case the date cannot be parsed.
65− -> HakyllAction Context Context
66−renderDate key format defaultValue = renderValue "path" key renderDate'
67− where
68− renderDate' filePath = fromMaybe defaultValue $ do
69− let dateString = substituteRegex "^([0-9]*-[0-9]*-[0-9]*).*" "\\1"
70− (takeFileName filePath)
71− time <- parseTime defaultTimeLocale
72− "%Y-%m-%d"
73− dateString :: Maybe UTCTime
74− return $ formatTime defaultTimeLocale format time
75−
76−-- | Change the extension of a file. This is only needed when you want to
77−-- render, for example, mardown to @.php@ files instead of @.html@ files.
78−--
79−-- > renderChainWith (changeExtension "php")
80−-- > ["templates/default.html"]
81−-- > (createPagePath "test.markdown")
82−--
83−-- Will render to @test.php@ instead of @test.html@.
84−changeExtension :: String -- ^ Extension to change to.
85− -> HakyllAction Context Context
86−changeExtension extension = changeValue "url" changeExtension'
87− where
88− changeExtension' = flip addExtension extension . dropExtension
7+type Context = Map String String
src/Text/Hakyll/ContextManipulations.hsnew file+84
@@ -0,0 +1,84 @@
1+-- | This module exports a number of functions that produce @HakyllAction@s to
2+-- manipulate @Context@s.
3+module Text.Hakyll.ContextManipulations
4+ ( renderValue
5+ , changeValue
6+ , copyValue
7+ , renderDate
8+ , changeExtension
9+ ) where
10+
11+import Control.Arrow (arr)
12+import System.Locale (defaultTimeLocale)
13+import System.FilePath (takeFileName, addExtension, dropExtension)
14+import Data.Time.Format (parseTime, formatTime)
15+import Data.Time.Clock (UTCTime)
16+import Data.Maybe (fromMaybe)
17+import qualified Data.Map as M
18+
19+import Text.Hakyll.Regex (substituteRegex)
20+import Text.Hakyll.HakyllAction (HakyllAction)
21+import Text.Hakyll.Context (Context)
22+
23+-- | Do something with a value in a @Context@, but keep the old value as well.
24+-- This is probably the most common function to construct a
25+-- @ContextManipulation@.
26+renderValue :: String -- ^ Key of which the value should be copied.
27+ -> String -- ^ Key the value should be copied to.
28+ -> (String -> String) -- ^ Function to apply on the value.
29+ -> HakyllAction Context Context
30+renderValue source destination f = arr $ \context ->
31+ case M.lookup source context of
32+ Nothing -> context
33+ (Just value) -> M.insert destination (f value) context
34+
35+-- | Change a value in a @Context@.
36+--
37+-- > import Data.Char (toUpper)
38+-- > changeValue "title" (map toUpper)
39+--
40+-- Will put the title in UPPERCASE.
41+changeValue :: String -- ^ Key to change.
42+ -> (String -> String) -- ^ Function to apply on the value.
43+ -> HakyllAction Context Context
44+changeValue key = renderValue key key
45+
46+-- | Copy a value from one key to another in a @Context@.
47+copyValue :: String -- ^ Source key.
48+ -> String -- ^ Destination key.
49+ -> HakyllAction Context Context
50+copyValue source destination = renderValue source destination id
51+
52+-- | When the context has a key called @path@ in a @yyyy-mm-dd-title.extension@
53+-- format (default for pages), this function can render the date.
54+--
55+-- > renderDate "date" "%B %e, %Y" "Date unknown"
56+--
57+-- Will render something like @January 32, 2010@.
58+renderDate :: String -- ^ Key in which the rendered date should be placed.
59+ -> String -- ^ Format to use on the date.
60+ -> String -- ^ Default key, in case the date cannot be parsed.
61+ -> HakyllAction Context Context
62+renderDate key format defaultValue = renderValue "path" key renderDate'
63+ where
64+ renderDate' filePath = fromMaybe defaultValue $ do
65+ let dateString = substituteRegex "^([0-9]*-[0-9]*-[0-9]*).*" "\\1"
66+ (takeFileName filePath)
67+ time <- parseTime defaultTimeLocale
68+ "%Y-%m-%d"
69+ dateString :: Maybe UTCTime
70+ return $ formatTime defaultTimeLocale format time
71+
72+-- | Change the extension of a file. This is only needed when you want to
73+-- render, for example, mardown to @.php@ files instead of @.html@ files.
74+--
75+-- > renderChainWith (changeExtension "php")
76+-- > ["templates/default.html"]
77+-- > (createPagePath "test.markdown")
78+--
79+-- Will render to @test.php@ instead of @test.html@.
80+changeExtension :: String -- ^ Extension to change to.
81+ -> HakyllAction Context Context
82+changeExtension extension = changeValue "url" changeExtension'
83+ where
84+ changeExtension' = flip addExtension extension . dropExtension
src/Text/Hakyll/Feed.hs+3−2
@@ -28,8 +28,9 @@ import Control.Monad.Reader (liftIO)
2828import Data.Maybe (fromMaybe)
2929import qualified Data.Map as M
3030
31−import Text.Hakyll.Context (renderDate)
32−import Text.Hakyll.Hakyll (Hakyll, Context)
31+import Text.Hakyll.Context (Context)
32+import Text.Hakyll.ContextManipulations (renderDate)
33+import Text.Hakyll.HakyllMonad (Hakyll)
3334import Text.Hakyll.Render (render, renderChain)
3435import Text.Hakyll.Renderables (createListing)
3536import Text.Hakyll.HakyllAction
src/Text/Hakyll/File.hs+1−1
@@ -23,7 +23,7 @@ import Data.List (isPrefixOf, sortBy)
2323import Data.Ord (comparing)
2424import Control.Monad.Reader (liftIO)
2525
26−import Text.Hakyll.Hakyll
26+import Text.Hakyll.HakyllMonad
2727import Text.Hakyll.Internal.FileType (isRenderableFile)
2828
2929-- | Auxiliary function to remove pathSeparators form the start. We don't deal
src/Text/Hakyll/HakyllAction.hs+1−1
@@ -17,7 +17,7 @@ import Prelude hiding ((.), id)
1717import System.IO (hPutStrLn, stderr)
1818
1919import Text.Hakyll.File (toDestination, isFileMoreRecent)
20−import Text.Hakyll.Hakyll
20+import Text.Hakyll.HakyllMonad
2121
2222-- | Type used for rendering computations that carry along dependencies.
2323data HakyllAction a b = HakyllAction
src/Text/Hakyll/Hakyll.hs → src/Text/Hakyll/HakyllMonad.hsrenamed+6−9
@@ -1,7 +1,6 @@
11-- | Module describing the Hakyll monad stack.
2−module Text.Hakyll.Hakyll
3− ( Context
4− , HakyllConfiguration (..)
2+module Text.Hakyll.HakyllMonad
3+ ( HakyllConfiguration (..)
54 , Hakyll
65 , askHakyll
76 , getAdditionalContext
@@ -9,11 +8,12 @@ module Text.Hakyll.Hakyll
98
109import Control.Monad.Reader (ReaderT, ask)
1110import Control.Monad (liftM)
12−import Data.Map (Map)
1311import qualified Data.Map as M
1412
15−-- | Type for a context.
16−type Context = Map String String
13+import Text.Hakyll.Context (Context)
14+
15+-- | Our custom monad stack.
16+type Hakyll = ReaderT HakyllConfiguration IO
1717
1818-- | Hakyll global configuration type.
1919data HakyllConfiguration = HakyllConfiguration
@@ -32,9 +32,6 @@ data HakyllConfiguration = HakyllConfiguration
3232 previewPollDelay :: Int
3333 }
3434
35−-- | Our custom monad stack.
36−type Hakyll = ReaderT HakyllConfiguration IO
37−
3835-- | Simplified @ask@ function for the Hakyll monad stack.
3936--
4037-- Usage would typically be something like:
src/Text/Hakyll/Internal/Cache.hs+3−2
@@ -6,10 +6,11 @@ module Text.Hakyll.Internal.Cache
66
77import Control.Monad ((<=<))
88import Control.Monad.Reader (liftIO)
9−import Text.Hakyll.Hakyll (Hakyll)
10−import Text.Hakyll.File
119import Data.Binary
1210
11+import Text.Hakyll.File
12+import Text.Hakyll.HakyllMonad (Hakyll)
13+
1314-- | We can store all datatypes instantiating @Binary@ to the cache. The cache
1415-- directory is specified by the @HakyllConfiguration@, usually @_cache@.
1516storeInCache :: (Binary a) => a -> FilePath -> Hakyll ()
src/Text/Hakyll/Internal/Page.hs+1−1
@@ -13,7 +13,7 @@ import Text.Pandoc
1313
1414import Text.Hakyll.Context (Context)
1515import Text.Hakyll.File
16−import Text.Hakyll.Hakyll
16+import Text.Hakyll.HakyllMonad
1717import Text.Hakyll.Regex (substituteRegex, matchesRegex)
1818import Text.Hakyll.Util (trim)
1919import Text.Hakyll.Internal.Cache
src/Text/Hakyll/Internal/Template.hs+1−1
@@ -19,8 +19,8 @@ import Control.Monad.Reader (liftIO)
1919
2020import Test.QuickCheck
2121
22−import Text.Hakyll.Hakyll (Hakyll)
2322import Text.Hakyll.Context (Context)
23+import Text.Hakyll.HakyllMonad (Hakyll)
2424import Text.Hakyll.Internal.Cache
2525
2626-- | Datatype used for template substitutions.
src/Text/Hakyll/Paginate.hs+1−1
@@ -7,7 +7,7 @@ module Text.Hakyll.Paginate
77
88import Control.Applicative ((<$>))
99
10−import Text.Hakyll.Hakyll (Context)
10+import Text.Hakyll.Context (Context)
1111import Text.Hakyll.Renderables
1212import Text.Hakyll.HakyllAction
1313import Text.Hakyll.Util (link)
src/Text/Hakyll/Render.hs+2−1
@@ -14,7 +14,8 @@ import System.Directory (copyFile)
1414import Data.Maybe (fromMaybe)
1515import qualified Data.Map as M
1616
17−import Text.Hakyll.Hakyll (Hakyll, Context, askHakyll, getAdditionalContext)
17+import Text.Hakyll.Context (Context)
18+import Text.Hakyll.HakyllMonad (Hakyll, askHakyll, getAdditionalContext)
1819import Text.Hakyll.File
1920import Text.Hakyll.HakyllAction
2021import Text.Hakyll.Internal.CompressCss
src/Text/Hakyll/Tags.hs+3−2
@@ -44,8 +44,9 @@ import Control.Arrow (second, (>>>))
4444import Control.Applicative ((<$>))
4545import System.FilePath
4646
47−import Text.Hakyll.Context
48−import Text.Hakyll.Hakyll (Hakyll)
47+import Text.Hakyll.Context (Context)
48+import Text.Hakyll.ContextManipulations (changeValue)
49+import Text.Hakyll.HakyllMonad (Hakyll)
4950import Text.Hakyll.Regex
5051import Text.Hakyll.Renderables
5152import Text.Hakyll.HakyllAction