Browse Source

Initial import from old hakyll-heist package

master
Peter J. Jones 6 years ago
commit
044da2a4fe
6 changed files with 224 additions and 0 deletions
  1. 22
    0
      GNUmakefile
  2. 30
    0
      LICENSE
  3. 0
    0
      README.md
  4. 2
    0
      Setup.hs
  5. 27
    0
      devalot-hakyll.cabal
  6. 143
    0
      src/Hakyll/Web/Heist.hs

+ 22
- 0
GNUmakefile View File

@@ -0,0 +1,22 @@
################################################################################
SITE = dist/build/site/site

################################################################################
.PHONEY: all clean test

################################################################################
all:
cabal configure && cabal build
(cd example && \
cabal configure && cabal build && $(SITE) build)

################################################################################
clean:
cabal clean
(cd example && test -r $(SITE) && $(SITE) clean || echo)
(cd example && cabal clean)

################################################################################
test:
cabal configure --enable-tests && \
cabal build && cabal test

+ 30
- 0
LICENSE View File

@@ -0,0 +1,30 @@
Copyright (c) 2013 Peter Jones <pjones@pmade.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Peter Jones nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 0
- 0
README.md View File


+ 2
- 0
Setup.hs View File

@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

+ 27
- 0
devalot-hakyll.cabal View File

@@ -0,0 +1,27 @@
name: devalot-hakyll
version: 0.1.1.0
synopsis: Hakyll utilities for Devalot.com
license: BSD3
license-file: LICENSE
author: Peter Jones <pjones@pmade.com>
maintainer: Peter Jones <pjones@pmade.com>
copyright: Copyright (C) 2013 Peter Jones
category: Web
build-type: Simple
cabal-version: >=1.8

library
exposed-modules: Hakyll.Web.Heist
hs-source-dirs: src
ghc-options: -Wall
build-depends: base == 4.5.*
, hakyll == 4.1.*
, heist == 0.11.*
, blaze-builder == 0.3.*
, bytestring == 0.9.*
, errors == 1.3.*
, mtl == 2.1.*
, text == 0.11.*
, transformers == 0.3.*
, utf8-string == 0.3.*
, xmlhtml == 0.2.*

+ 143
- 0
src/Hakyll/Web/Heist.hs View File

@@ -0,0 +1,143 @@
{-# LANGUAGE OverloadedStrings #-}

{-

This file is part of the package hakyll-heist. It is subject to the
license terms in the LICENSE file found in the top-level directory of
this distribution and at git://pmade.com/hakyll-heist/LICENSE. No
part of hakyll-heist package, including this file, may be copied,
modified, propagated, or distributed except according to the terms
contained in the LICENSE file.

-}

--------------------------------------------------------------------------------
module Hakyll.Web.Heist
( loadDefaultHeist
, loadHeist
, applyTemplate
, applyTemplateList
, applyJoinTemplateList
, Content
, SpliceT
, State
) where

--------------------------------------------------------------------------------
import Blaze.ByteString.Builder (toByteString)
import Control.Error (runEitherT)
import Control.Monad (liftM)
import Control.Monad.Reader (ReaderT(..), ask)
import Control.Monad.Trans (lift)
import Data.ByteString (ByteString)
import Data.Monoid ((<>))
import Text.XmlHtml (elementAttrs)
import Data.ByteString.UTF8 (toString)
import Data.List (intersperse)
import qualified Data.Text as T

--------------------------------------------------------------------------------
import Hakyll.Core.Compiler
import Hakyll.Core.Item
import Hakyll.Web.Template.Context

--------------------------------------------------------------------------------
import Heist
import qualified Heist.Compiled as C

--------------------------------------------------------------------------------
type Content a = (Context a, Item a)
type SpliceT a = ReaderT (Content a) Compiler
type State a = HeistState (SpliceT a)

--------------------------------------------------------------------------------
-- | Load all of the templates from the given directory and return an
-- initialized 'HeistState' using the given splices (in addition to
-- the default splices and the @hakyll@ splice).
loadHeist :: FilePath
-- ^ Directory containing the templates.
-> [(T.Text, C.Splice (SpliceT a))]
-- ^ List of compiled Heist slices.
-> [(T.Text, AttrSplice (SpliceT a))]
-- ^ List of Heist attribute slices.
-> IO (HeistState (SpliceT a))
loadHeist baseDir a b = do
tState <- runEitherT $ do
templates <- loadTemplates baseDir
let splices' = [("hakyll", hakyllSplice)] ++ a
attrs = [("url", urlAttrSplice)] ++ b
hc = HeistConfig [] defaultLoadTimeSplices splices' attrs templates
initHeist hc
either (error . concat) return tState

--------------------------------------------------------------------------------
-- | Load all of the templates from the given directory and return an
-- initialized 'HeistState' with the default splices.
loadDefaultHeist :: FilePath -> IO (HeistState (SpliceT a))
loadDefaultHeist baseDir = loadHeist baseDir [] []

--------------------------------------------------------------------------------
-- | Apply a Heist template to a Hakyll 'Item'. You need a
-- 'HeistState' from either the 'loadDefaultHeist' function or the
-- 'loadHeist' function.
applyTemplate :: HeistState (SpliceT a) -- ^ HeistState
-> ByteString -- ^ Template name
-> Context a -- ^ Context
-> Item a -- ^ Page
-> Compiler (Item String) -- ^ Resulting item
applyTemplate state name context item = do
case C.renderTemplate state name of
Nothing -> fail badTplError
Just (r,_) -> do builder <- runReaderT r (context, item)
let body = toString $ toByteString builder
return $ itemSetBody body item
where badTplError = "failed to render template: " ++ toString name

--------------------------------------------------------------------------------
-- | Render the given list of 'Item's with the given Heist template
-- and return everything concatenated together.
applyTemplateList :: HeistState (SpliceT a) -- ^ HeistState
-> ByteString
-> Context a
-> [Item a]
-> Compiler String
applyTemplateList = applyJoinTemplateList ""

--------------------------------------------------------------------------------
-- | Render the given list of 'Item's with the given Heist template.
-- The content of the items is joined together using the given string
-- delimiter.
applyJoinTemplateList :: String
-> HeistState (SpliceT a) -- ^ HeistState
-> ByteString
-> Context a
-> [Item a]
-> Compiler String
applyJoinTemplateList delimiter state name context items = do
items' <- mapM (applyTemplate state name context) items
return $ concat $ intersperse delimiter $ map itemBody items'

--------------------------------------------------------------------------------
-- Internal function to render the @hakyll@ splice given fields inside
-- of a 'Context'.
hakyllSplice :: C.Splice (SpliceT a)
hakyllSplice = do
node <- getParamNode
return $ C.yieldRuntimeText $ do
(context, item) <- lift ask
let context' f = unContext (context <> missingField) f item
case lookup "field" $ elementAttrs node of
Nothing -> fail fieldError
Just f -> liftStr $ context' $ T.unpack f
where fieldError = "The `hakyll' splice is missing the `field' attribute"
liftStr s = lift $ lift $ liftM T.pack s

--------------------------------------------------------------------------------
-- Attribute splice: changes a bare @url@ attribute to a complete
-- @href@ attribute using the URL from the current 'Context'.
urlAttrSplice :: AttrSplice (SpliceT a)
urlAttrSplice _ = do
(context, item) <- lift ask
let url = unContext (context <> missingField) "url" item
val <- lift $ lift $ liftM T.pack url
return $ ("href", val) : []

Loading…
Cancel
Save