diff options
-rw-r--r-- | hakyll.cabal | 6 | ||||
-rw-r--r-- | src/Hakyll/Web/Feed.hs | 6 | ||||
-rw-r--r-- | src/Hakyll/Web/Template.hs | 49 | ||||
-rw-r--r-- | src/Hakyll/Web/Template/Context.hs | 10 | ||||
-rw-r--r-- | src/Hakyll/Web/Template/Read.hs | 44 | ||||
-rw-r--r-- | src/Hakyll/Web/Template/Read/Hakyll.hs | 35 | ||||
-rw-r--r-- | src/Hakyll/Web/Template/Read/Hamlet.hs | 46 | ||||
-rw-r--r-- | tests/Hakyll/Web/Template/Tests.hs | 13 | ||||
-rw-r--r-- | tests/data/template.html | 1 | ||||
-rw-r--r-- | tests/data/template.html.out (renamed from tests/data/example.md.out) | 1 |
10 files changed, 69 insertions, 142 deletions
diff --git a/hakyll.cabal b/hakyll.cabal index 7ec9de4..23082fd 100644 --- a/hakyll.cabal +++ b/hakyll.cabal @@ -50,8 +50,8 @@ Data-Files: Extra-source-files: tests/data/example.md tests/data/example.md.metadata - tests/data/example.md.out tests/data/template.html + tests/data/template.html.out Source-Repository head Type: git @@ -82,7 +82,6 @@ Library deepseq >= 1.3 && < 1.4, directory >= 1.0 && < 1.3, filepath >= 1.0 && < 1.4, - hamlet >= 1.0 && < 1.2, lrucache >= 1.1.1 && < 1.2, mtl >= 1 && < 2.2, old-locale >= 1.0 && < 1.1, @@ -145,8 +144,6 @@ Library Hakyll.Core.Provider.Modified Hakyll.Core.Rules.Internal Hakyll.Web.Template.Internal - Hakyll.Web.Template.Read.Hakyll - Hakyll.Web.Template.Read.Hamlet Paths_hakyll If flag(previewServer) @@ -191,7 +188,6 @@ Test-suite hakyll-tests deepseq >= 1.3 && < 1.4, directory >= 1.0 && < 1.3, filepath >= 1.0 && < 1.4, - hamlet >= 1.0 && < 1.2, lrucache >= 1.1.1 && < 1.2, mtl >= 1 && < 2.2, old-locale >= 1.0 && < 1.1, diff --git a/src/Hakyll/Web/Feed.hs b/src/Hakyll/Web/Feed.hs index c0a6213..01ba0e1 100644 --- a/src/Hakyll/Web/Feed.hs +++ b/src/Hakyll/Web/Feed.hs @@ -24,8 +24,8 @@ module Hakyll.Web.Feed -------------------------------------------------------------------------------- -import Control.Monad (forM, (<=<)) -import Data.Monoid (mconcat) +import Control.Monad (forM, (<=<)) +import Data.Monoid (mconcat) -------------------------------------------------------------------------------- @@ -34,7 +34,7 @@ import Hakyll.Core.Compiler.Internal import Hakyll.Core.Item import Hakyll.Web.Template import Hakyll.Web.Template.Context -import Hakyll.Web.Template.Read.Hakyll (readTemplate) +import Hakyll.Web.Template.Read -------------------------------------------------------------------------------- diff --git a/src/Hakyll/Web/Template.hs b/src/Hakyll/Web/Template.hs index adaf1aa..a933496 100644 --- a/src/Hakyll/Web/Template.hs +++ b/src/Hakyll/Web/Template.hs @@ -36,32 +36,9 @@ -- -- Because of it's simplicity, these templates can be used for more than HTML: -- you could make, for example, CSS or JS templates as well. --- --- In addition to the native format, Hakyll also supports hamlet templates. For --- more information on hamlet templates, please refer to: --- <http://hackage.haskell.org/package/hamlet>. Internally, hamlet templates are --- converted to hakyll templates -- which means that you can only use variable --- insertion (and not all hamlet's features). --- --- This is an example of a valid hamlet template. You should place them in --- files with a @.hamlet@ extension: --- --- > !!! --- > <html> --- > <head> --- > <meta charset="UTF-8"> --- > <title> MyAweSomeCompany - #{title} --- > <body> --- > <h1> MyAweSomeCompany - #{title} --- > <div id="navigation"> --- > <a href="/index.html"> Home --- > <a href="/about.html"> About --- > <a href="/code.html"> Code --- > #{body} module Hakyll.Web.Template ( Template , templateCompiler - , templateCompilerWith , applyTemplate , applyTemplateWith ) where @@ -70,14 +47,10 @@ module Hakyll.Web.Template -------------------------------------------------------------------------------- import Control.Monad (forM, liftM) import Prelude hiding (id) -import System.FilePath (takeExtension) -import Text.Hamlet (HamletSettings, - defaultHamletSettings) -------------------------------------------------------------------------------- import Hakyll.Core.Compiler -import Hakyll.Core.Identifier import Hakyll.Core.Item import Hakyll.Web.Template.Context import Hakyll.Web.Template.Internal @@ -85,25 +58,11 @@ import Hakyll.Web.Template.Read -------------------------------------------------------------------------------- --- | Read a template. If the extension of the file we're compiling is --- @.hml@ or @.hamlet@, it will be considered as a Hamlet template, and parsed --- as such. +-- | Read a template. templateCompiler :: Compiler (Item Template) -templateCompiler = templateCompilerWith defaultHamletSettings - - --------------------------------------------------------------------------------- --- | Version of 'templateCompiler' that enables custom settings. -templateCompilerWith :: HamletSettings -> Compiler (Item Template) -templateCompilerWith settings = - cached "Hakyll.Web.Template.templateCompilerWith" $ do - identifier <- getUnderlying - item <- getResourceString - if takeExtension (toFilePath identifier) `elem` [".hml", ".hamlet"] - -- Hamlet template - then return $ fmap (readHamletTemplateWith settings) item - -- Hakyll template - else return $ fmap readTemplate item +templateCompiler = cached "Hakyll.Web.Template.templateCompiler" $ do + item <- getResourceString + return $ fmap readTemplate item -------------------------------------------------------------------------------- diff --git a/src/Hakyll/Web/Template/Context.hs b/src/Hakyll/Web/Template/Context.hs index eeec728..acc01bf 100644 --- a/src/Hakyll/Web/Template/Context.hs +++ b/src/Hakyll/Web/Template/Context.hs @@ -4,6 +4,7 @@ module Hakyll.Web.Template.Context , mapContext , field , constField + , functionField , defaultContext , bodyField @@ -71,6 +72,15 @@ constField key = field key . const . return -------------------------------------------------------------------------------- +functionField :: String -> ([String] -> Item a -> Compiler String) -> Context a +functionField name value = Context $ \k i -> case words k of + [] -> empty + (n : args) + | n == name -> value args i + | otherwise -> empty + + +-------------------------------------------------------------------------------- defaultContext :: Context String defaultContext = bodyField "body" `mappend` diff --git a/src/Hakyll/Web/Template/Read.hs b/src/Hakyll/Web/Template/Read.hs index 421b7e9..f8583ff 100644 --- a/src/Hakyll/Web/Template/Read.hs +++ b/src/Hakyll/Web/Template/Read.hs @@ -1,10 +1,42 @@ --- | Re-exports all different template reading modules --- +-------------------------------------------------------------------------------- +-- | Read templates in Hakyll's native format module Hakyll.Web.Template.Read ( readTemplate - , readHamletTemplate - , readHamletTemplateWith ) where -import Hakyll.Web.Template.Read.Hakyll -import Hakyll.Web.Template.Read.Hamlet + +-------------------------------------------------------------------------------- +import Data.List (isPrefixOf) +import Data.Char (isAlphaNum) + + +-------------------------------------------------------------------------------- +import Hakyll.Web.Template.Internal + + +-------------------------------------------------------------------------------- +-- | Construct a @Template@ from a string. +readTemplate :: String -> Template +readTemplate = Template . readTemplate' + where + readTemplate' [] = [] + readTemplate' string + | "$$" `isPrefixOf` string = + Escaped : readTemplate' (drop 2 string) + | "$" `isPrefixOf` string = + case readKey (drop 1 string) of + Just (key, rest) -> Key key : readTemplate' rest + Nothing -> Chunk "$" : readTemplate' (drop 1 string) + | otherwise = + let (chunk, rest) = break (== '$') string + in Chunk chunk : readTemplate' rest + + -- Parse an key into (key, rest) if it's valid, and return + -- Nothing otherwise + readKey string = + let (key, rest) = span validKeyChar string + in if not (null key) && "$" `isPrefixOf` rest + then Just (key, drop 1 rest) + else Nothing + + validKeyChar x = isAlphaNum x || x == ' ' diff --git a/src/Hakyll/Web/Template/Read/Hakyll.hs b/src/Hakyll/Web/Template/Read/Hakyll.hs deleted file mode 100644 index fecf772..0000000 --- a/src/Hakyll/Web/Template/Read/Hakyll.hs +++ /dev/null @@ -1,35 +0,0 @@ --- | Read templates in Hakyll's native format --- -module Hakyll.Web.Template.Read.Hakyll - ( readTemplate - ) where - -import Data.List (isPrefixOf) -import Data.Char (isAlphaNum) - -import Hakyll.Web.Template.Internal - --- | Construct a @Template@ from a string. --- -readTemplate :: String -> Template -readTemplate = Template . readTemplate' - where - readTemplate' [] = [] - readTemplate' string - | "$$" `isPrefixOf` string = - Escaped : readTemplate' (drop 2 string) - | "$" `isPrefixOf` string = - case readKey (drop 1 string) of - Just (key, rest) -> Key key : readTemplate' rest - Nothing -> Chunk "$" : readTemplate' (drop 1 string) - | otherwise = - let (chunk, rest) = break (== '$') string - in Chunk chunk : readTemplate' rest - - -- Parse an key into (key, rest) if it's valid, and return - -- Nothing otherwise - readKey string = - let (key, rest) = span isAlphaNum string - in if not (null key) && "$" `isPrefixOf` rest - then Just (key, drop 1 rest) - else Nothing diff --git a/src/Hakyll/Web/Template/Read/Hamlet.hs b/src/Hakyll/Web/Template/Read/Hamlet.hs deleted file mode 100644 index a08cb1d..0000000 --- a/src/Hakyll/Web/Template/Read/Hamlet.hs +++ /dev/null @@ -1,46 +0,0 @@ --- | Read templates in the hamlet format --- -{-# LANGUAGE MultiParamTypeClasses #-} -module Hakyll.Web.Template.Read.Hamlet - ( readHamletTemplate - , readHamletTemplateWith - ) where - -import Text.Hamlet (HamletSettings, defaultHamletSettings) -import Text.Hamlet.RT - -import Hakyll.Web.Template.Internal - --- | Read a hamlet template using the default settings --- -readHamletTemplate :: String -> Template -readHamletTemplate = readHamletTemplateWith defaultHamletSettings - --- | Read a hamlet template using the specified settings --- -readHamletTemplateWith :: HamletSettings -> String -> Template -readHamletTemplateWith settings string = - let result = parseHamletRT settings string - in case result of - Just hamlet -> fromHamletRT hamlet - Nothing -> error - "Hakyll.Web.Template.Read.Hamlet.readHamletTemplateWith: \ - \Could not parse Hamlet file" - --- | Convert a 'HamletRT' to a 'Template' --- -fromHamletRT :: HamletRT -- ^ Hamlet runtime template - -> Template -- ^ Hakyll template -fromHamletRT (HamletRT sd) = Template $ map fromSimpleDoc sd - where - fromSimpleDoc :: SimpleDoc -> TemplateElement - fromSimpleDoc (SDRaw chunk) = Chunk chunk - fromSimpleDoc (SDVar [var]) = Key var - fromSimpleDoc (SDVar _) = error - "Hakyll.Web.Template.Read.Hamlet.fromHamletRT: \ - \Hakyll does not support '.' in identifier names when using \ - \hamlet templates." - fromSimpleDoc _ = error - "Hakyll.Web.Template.Read.Hamlet.fromHamletRT: \ - \Only simple $key$ identifiers are allowed when using hamlet \ - \templates." diff --git a/tests/Hakyll/Web/Template/Tests.hs b/tests/Hakyll/Web/Template/Tests.hs index 42be506..4e49d1f 100644 --- a/tests/Hakyll/Web/Template/Tests.hs +++ b/tests/Hakyll/Web/Template/Tests.hs @@ -6,6 +6,7 @@ module Hakyll.Web.Template.Tests -------------------------------------------------------------------------------- +import Data.Monoid (mconcat) import Test.Framework (Test, testGroup) import Test.Framework.Providers.HUnit (testCase) import Test.HUnit (Assertion, (@=?)) @@ -32,9 +33,17 @@ case01 :: Assertion case01 = withTestStore $ \store -> do provider <- newTestProvider store - out <- resourceString provider "example.md.out" + out <- resourceString provider "template.html.out" tpl <- testCompilerDone store provider "template.html" $ templateCompiler item <- testCompilerDone store provider "example.md" $ - pageCompiler >>= applyTemplate (itemBody tpl) defaultContext + pageCompiler >>= applyTemplate (itemBody tpl) testContext out @=? itemBody item + + +-------------------------------------------------------------------------------- +testContext :: Context String +testContext = mconcat + [ functionField "echo" (\args _ -> return $ unwords args) + , defaultContext + ] diff --git a/tests/data/template.html b/tests/data/template.html index 8fa47e4..52189a3 100644 --- a/tests/data/template.html +++ b/tests/data/template.html @@ -1,3 +1,4 @@ <div> + $echo test$ $body$ </div> diff --git a/tests/data/example.md.out b/tests/data/template.html.out index f2c4c28..51a105e 100644 --- a/tests/data/example.md.out +++ b/tests/data/template.html.out @@ -1,3 +1,4 @@ <div> + test <p>This is an example.</p> </div> |