on master help sign in
Added "teasers" support to be used in posts index.

Just add "<!-- teaser_end -->" to separate the teaser and the rest of the article and use "$teaser$" key in the template! Closes issue #35.

commit2651627189ba8b35949580b9a31cc1dde4911f88 authorIvan N. Veselov <veselov@gmail.com> dateSat 4 May 2013, 17:24 (14 years ago) parentca80171
2 files changed+33−1
src/Hakyll/Core/Util/String.hs+20 src/Hakyll/Web/Template/Context.hs+13−1
src/Hakyll/Core/Util/String.hs+20
@@ -4,9 +4,11 @@ module Hakyll.Core.Util.String
44 ( trim
55 , replaceAll
66 , splitAll
7+ , needlePrefix
78 ) where
89
910import Data.Char (isSpace)
11+import Data.List (isPrefixOf)
1012import Data.Maybe (listToMaybe)
1113
1214import Text.Regex.TDFA ((=~~))
@@ -46,3 +48,21 @@ splitAll pattern = filter (not . null) . splitAll'
4648 Just (o, l) ->
4749 let (before, tmp) = splitAt o src
4850 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
77 , functionField
88
99 , defaultContext
10+ , teaserContext
1011 , bodyField
1112 , metadataField
1213 , urlField
@@ -40,7 +41,7 @@ import Hakyll.Core.Identifier
4041import Hakyll.Core.Item
4142import Hakyll.Core.Metadata
4243import Hakyll.Core.Provider
43−import Hakyll.Core.Util.String (splitAll)
44+import Hakyll.Core.Util.String (splitAll, needlePrefix)
4445import Hakyll.Web.Html
4546
4647
@@ -90,6 +91,17 @@ defaultContext =
9091 titleField "title" `mappend`
9192 missingField
9293
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+
93105
94106--------------------------------------------------------------------------------
95107bodyField :: String -> Context String