on master help sign in
Added copyValueWith.
commit637ab61284802c033372787d9af938282b8f69c5 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateFri 25 Dec 2009, 23:06 (17 years ago) parenta532352
1 file changed+12
src/Text/Hakyll/Page.hs+12
src/Text/Hakyll/Page.hs+12
@@ -1,6 +1,7 @@
11module Text.Hakyll.Page
22 ( Page,
33 fromContext,
4+ copyValueWith,
45 getBody,
56 readPage,
67 writePage
@@ -28,6 +29,17 @@ data Page = Page (M.Map B.ByteString B.ByteString)
2829fromContext :: (M.Map B.ByteString B.ByteString) -> Page
2930fromContext = Page
3031
32+-- | Do something with a value of the page.
33+copyValueWith :: String -- ^ Key of which the value should be copied.
34+ -> String -- ^ Key the value should be copied to.
35+ -> (B.ByteString -> B.ByteString) -- ^ Function to apply on the value.
36+ -> Page -- ^ Page on which to apply this modification.
37+ -> Page -- ^ Result.
38+copyValueWith src dst f p@(Page page) = case M.lookup (B.pack src) page of
39+ Nothing -> p
40+ (Just value) -> Page $ M.insert (B.pack dst) (f value) page
41+
42+
3143-- | Auxiliary function to pack a pair.
3244packPair :: (String, String) -> (B.ByteString, B.ByteString)
3345packPair (a, b) = (B.pack a, B.pack b)