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.
This commit is contained in:
parent
ca80171caf
commit
2651627189
2 changed files with 33 additions and 1 deletions
|
@ -4,9 +4,11 @@ module Hakyll.Core.Util.String
|
||||||
( trim
|
( trim
|
||||||
, replaceAll
|
, replaceAll
|
||||||
, splitAll
|
, splitAll
|
||||||
|
, needlePrefix
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Char (isSpace)
|
import Data.Char (isSpace)
|
||||||
|
import Data.List (isPrefixOf)
|
||||||
import Data.Maybe (listToMaybe)
|
import Data.Maybe (listToMaybe)
|
||||||
|
|
||||||
import Text.Regex.TDFA ((=~~))
|
import Text.Regex.TDFA ((=~~))
|
||||||
|
@ -46,3 +48,21 @@ splitAll pattern = filter (not . null) . splitAll'
|
||||||
Just (o, l) ->
|
Just (o, l) ->
|
||||||
let (before, tmp) = splitAt o src
|
let (before, tmp) = splitAt o src
|
||||||
in before : splitAll' (drop l tmp)
|
in before : splitAll' (drop l tmp)
|
||||||
|
|
||||||
|
|
||||||
|
-- | Find the first instance of needle (must be non-empty) in
|
||||||
|
-- haystack. We return the prefix of haystack before needle is
|
||||||
|
-- matched.
|
||||||
|
--
|
||||||
|
-- Examples:
|
||||||
|
-- needlePrefix "cd" "abcde" = "ab"
|
||||||
|
-- needlePrefix "ab" "abc" = ""
|
||||||
|
-- needlePrefix "ab" "xxab" = "xx"
|
||||||
|
-- needlePrefix "a" "xx" = "xx"
|
||||||
|
--
|
||||||
|
needlePrefix :: String -> String -> String
|
||||||
|
needlePrefix needle haystack = go haystack
|
||||||
|
where
|
||||||
|
go [] = []
|
||||||
|
go xss@(x:xs) | needle `isPrefixOf` xss = []
|
||||||
|
| otherwise = x : go xs
|
||||||
|
|
|
@ -7,6 +7,7 @@ module Hakyll.Web.Template.Context
|
||||||
, functionField
|
, functionField
|
||||||
|
|
||||||
, defaultContext
|
, defaultContext
|
||||||
|
, teaserContext
|
||||||
, bodyField
|
, bodyField
|
||||||
, metadataField
|
, metadataField
|
||||||
, urlField
|
, urlField
|
||||||
|
@ -40,7 +41,7 @@ import Hakyll.Core.Identifier
|
||||||
import Hakyll.Core.Item
|
import Hakyll.Core.Item
|
||||||
import Hakyll.Core.Metadata
|
import Hakyll.Core.Metadata
|
||||||
import Hakyll.Core.Provider
|
import Hakyll.Core.Provider
|
||||||
import Hakyll.Core.Util.String (splitAll)
|
import Hakyll.Core.Util.String (splitAll, needlePrefix)
|
||||||
import Hakyll.Web.Html
|
import Hakyll.Web.Html
|
||||||
|
|
||||||
|
|
||||||
|
@ -90,6 +91,17 @@ defaultContext =
|
||||||
titleField "title" `mappend`
|
titleField "title" `mappend`
|
||||||
missingField
|
missingField
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
teaserContext :: Snapshot -> Context String
|
||||||
|
teaserContext snapshot = field "teaser" $ \item ->
|
||||||
|
(needlePrefix teaserSeparator . itemBody) <$>
|
||||||
|
loadSnapshot (itemIdentifier item) snapshot
|
||||||
|
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
teaserSeparator :: String
|
||||||
|
teaserSeparator = "<!-- teaser_end -->"
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
bodyField :: String -> Context String
|
bodyField :: String -> Context String
|
||||||
|
|
Loading…
Reference in a new issue