on master help sign in
Added "teasers" support to be used in posts index.
2651627Ivan N. Veselov
src/Hakyll/Core/Util/String.hs+20
@@ -4,9 +4,11 @@ module Hakyll.Core.Util.String
4 ( trim
4 ( trim
5 , replaceAll
5 , replaceAll
6 , splitAll
6 , splitAll
7+ , needlePrefix
7 ) where
8 ) where
8
9
9import Data.Char (isSpace)
10import Data.Char (isSpace)
11+import Data.List (isPrefixOf)
10import Data.Maybe (listToMaybe)
12import Data.Maybe (listToMaybe)
11
13
12import Text.Regex.TDFA ((=~~))
14import Text.Regex.TDFA ((=~~))
@@ -46,3 +48,21 @@ splitAll pattern = filter (not . null) . splitAll'
46 Just (o, l) ->
48 Just (o, l) ->
47 let (before, tmp) = splitAt o src
49 let (before, tmp) = splitAt o src
48 in before : splitAll' (drop l tmp)
50 in before : splitAll' (drop l tmp)
51+
52+
53+-- | Find the first instance of needle (must be non-empty) in
54+-- haystack. We return the prefix of haystack before needle is
55+-- matched.
56+--
57+-- Examples:
58+-- needlePrefix "cd" "abcde" = "ab"
59+-- needlePrefix "ab" "abc" = ""
60+-- needlePrefix "ab" "xxab" = "xx"
61+-- needlePrefix "a" "xx" = "xx"
62+--
63+needlePrefix :: String -> String -> String
64+needlePrefix needle haystack = go haystack
65+ where
66+ go [] = []
67+ go xss@(x:xs) | needle `isPrefixOf` xss = []
68+ | otherwise = x : go xs
src/Hakyll/Web/Template/Context.hs+13−1
@@ -7,6 +7,7 @@ module Hakyll.Web.Template.Context
7 , functionField
7 , functionField
8
8
9 , defaultContext
9 , defaultContext
10+ , teaserContext
10 , bodyField
11 , bodyField
11 , metadataField
12 , metadataField
12 , urlField
13 , urlField
@@ -40,7 +41,7 @@ import Hakyll.Core.Identifier
40import Hakyll.Core.Item
41import Hakyll.Core.Item
41import Hakyll.Core.Metadata
42import Hakyll.Core.Metadata
42import Hakyll.Core.Provider
43import Hakyll.Core.Provider
43−import Hakyll.Core.Util.String (splitAll)
44+import Hakyll.Core.Util.String (splitAll, needlePrefix)
44import Hakyll.Web.Html
45import Hakyll.Web.Html
45
46
46
47
@@ -90,6 +91,17 @@ defaultContext =
90 titleField "title" `mappend`
91 titleField "title" `mappend`
91 missingField
92 missingField
92
93
94+--------------------------------------------------------------------------------
95+teaserContext :: Snapshot -> Context String
96+teaserContext snapshot = field "teaser" $ \item ->
97+ (needlePrefix teaserSeparator . itemBody) <$>
98+ loadSnapshot (itemIdentifier item) snapshot
99+
100+
101+--------------------------------------------------------------------------------
102+teaserSeparator :: String
103+teaserSeparator = "<!-- teaser_end -->"
104+
93
105
94--------------------------------------------------------------------------------
106--------------------------------------------------------------------------------
95bodyField :: String -> Context String
107bodyField :: String -> Context String