on master help sign in
Remove hamlet, add function fields
802742cJasper Van der Jeugt
hakyll.cabal+1−5
@@ -50,8 +50,8 @@ Data-Files:
50Extra-source-files:
50Extra-source-files:
51 tests/data/example.md
51 tests/data/example.md
52 tests/data/example.md.metadata
52 tests/data/example.md.metadata
53− tests/data/example.md.out
54 tests/data/template.html
53 tests/data/template.html
54+ tests/data/template.html.out
55
55
56Source-Repository head
56Source-Repository head
57 Type: git
57 Type: git
@@ -82,7 +82,6 @@ Library
82 deepseq >= 1.3 && < 1.4,
82 deepseq >= 1.3 && < 1.4,
83 directory >= 1.0 && < 1.3,
83 directory >= 1.0 && < 1.3,
84 filepath >= 1.0 && < 1.4,
84 filepath >= 1.0 && < 1.4,
85− hamlet >= 1.0 && < 1.2,
86 lrucache >= 1.1.1 && < 1.2,
85 lrucache >= 1.1.1 && < 1.2,
87 mtl >= 1 && < 2.2,
86 mtl >= 1 && < 2.2,
88 old-locale >= 1.0 && < 1.1,
87 old-locale >= 1.0 && < 1.1,
@@ -145,8 +144,6 @@ Library
145 Hakyll.Core.Provider.Modified
144 Hakyll.Core.Provider.Modified
146 Hakyll.Core.Rules.Internal
145 Hakyll.Core.Rules.Internal
147 Hakyll.Web.Template.Internal
146 Hakyll.Web.Template.Internal
148− Hakyll.Web.Template.Read.Hakyll
149− Hakyll.Web.Template.Read.Hamlet
150 Paths_hakyll
147 Paths_hakyll
151
148
152 If flag(previewServer)
149 If flag(previewServer)
@@ -191,7 +188,6 @@ Test-suite hakyll-tests
191 deepseq >= 1.3 && < 1.4,
188 deepseq >= 1.3 && < 1.4,
192 directory >= 1.0 && < 1.3,
189 directory >= 1.0 && < 1.3,
193 filepath >= 1.0 && < 1.4,
190 filepath >= 1.0 && < 1.4,
194− hamlet >= 1.0 && < 1.2,
195 lrucache >= 1.1.1 && < 1.2,
191 lrucache >= 1.1.1 && < 1.2,
196 mtl >= 1 && < 2.2,
192 mtl >= 1 && < 2.2,
197 old-locale >= 1.0 && < 1.1,
193 old-locale >= 1.0 && < 1.1,
src/Hakyll/Web/Feed.hs+3−3
@@ -24,8 +24,8 @@ module Hakyll.Web.Feed
24
24
25
25
26--------------------------------------------------------------------------------
26--------------------------------------------------------------------------------
27−import Control.Monad (forM, (<=<))
27+import Control.Monad (forM, (<=<))
28−import Data.Monoid (mconcat)
28+import Data.Monoid (mconcat)
29
29
30
30
31--------------------------------------------------------------------------------
31--------------------------------------------------------------------------------
@@ -34,7 +34,7 @@ import Hakyll.Core.Compiler.Internal
34import Hakyll.Core.Item
34import Hakyll.Core.Item
35import Hakyll.Web.Template
35import Hakyll.Web.Template
36import Hakyll.Web.Template.Context
36import Hakyll.Web.Template.Context
37−import Hakyll.Web.Template.Read.Hakyll (readTemplate)
37+import Hakyll.Web.Template.Read
38
38
39
39
40--------------------------------------------------------------------------------
40--------------------------------------------------------------------------------
src/Hakyll/Web/Template.hs+4−45
@@ -36,32 +36,9 @@
36--
36--
37-- Because of it's simplicity, these templates can be used for more than HTML:
37-- Because of it's simplicity, these templates can be used for more than HTML:
38-- you could make, for example, CSS or JS templates as well.
38-- you could make, for example, CSS or JS templates as well.
39−--
40−-- In addition to the native format, Hakyll also supports hamlet templates. For
41−-- more information on hamlet templates, please refer to:
42−-- <http://hackage.haskell.org/package/hamlet>. Internally, hamlet templates are
43−-- converted to hakyll templates -- which means that you can only use variable
44−-- insertion (and not all hamlet's features).
45−--
46−-- This is an example of a valid hamlet template. You should place them in
47−-- files with a @.hamlet@ extension:
48−--
49−-- > !!!
50−-- > <html>
51−-- > <head>
52−-- > <meta charset="UTF-8">
53−-- > <title> MyAweSomeCompany - #{title}
54−-- > <body>
55−-- > <h1> MyAweSomeCompany - #{title}
56−-- > <div id="navigation">
57−-- > <a href="/index.html"> Home
58−-- > <a href="/about.html"> About
59−-- > <a href="/code.html"> Code
60−-- > #{body}
61module Hakyll.Web.Template
39module Hakyll.Web.Template
62 ( Template
40 ( Template
63 , templateCompiler
41 , templateCompiler
64− , templateCompilerWith
65 , applyTemplate
42 , applyTemplate
66 , applyTemplateWith
43 , applyTemplateWith
67 ) where
44 ) where
@@ -70,14 +47,10 @@ module Hakyll.Web.Template
70--------------------------------------------------------------------------------
47--------------------------------------------------------------------------------
71import Control.Monad (forM, liftM)
48import Control.Monad (forM, liftM)
72import Prelude hiding (id)
49import Prelude hiding (id)
73−import System.FilePath (takeExtension)
74−import Text.Hamlet (HamletSettings,
75− defaultHamletSettings)
76
50
77
51
78--------------------------------------------------------------------------------
52--------------------------------------------------------------------------------
79import Hakyll.Core.Compiler
53import Hakyll.Core.Compiler
80−import Hakyll.Core.Identifier
81import Hakyll.Core.Item
54import Hakyll.Core.Item
82import Hakyll.Web.Template.Context
55import Hakyll.Web.Template.Context
83import Hakyll.Web.Template.Internal
56import Hakyll.Web.Template.Internal
@@ -85,25 +58,11 @@ import Hakyll.Web.Template.Read
85
58
86
59
87--------------------------------------------------------------------------------
60--------------------------------------------------------------------------------
88−-- | Read a template. If the extension of the file we're compiling is
61+-- | Read a template.
89−-- @.hml@ or @.hamlet@, it will be considered as a Hamlet template, and parsed
90−-- as such.
91templateCompiler :: Compiler (Item Template)
62templateCompiler :: Compiler (Item Template)
92−templateCompiler = templateCompilerWith defaultHamletSettings
93−
94−
95−--------------------------------------------------------------------------------
96−-- | Version of 'templateCompiler' that enables custom settings.
97−templateCompilerWith :: HamletSettings -> Compiler (Item Template)
98−templateCompilerWith settings =
99− cached "Hakyll.Web.Template.templateCompilerWith" $ do
63+templateCompiler = cached "Hakyll.Web.Template.templateCompiler" $ do
100− identifier <- getUnderlying
101− item <- getResourceString
64+ item <- getResourceString
102− if takeExtension (toFilePath identifier) `elem` [".hml", ".hamlet"]
103− -- Hamlet template
104− then return $ fmap (readHamletTemplateWith settings) item
105− -- Hakyll template
106− else return $ fmap readTemplate item
65+ return $ fmap readTemplate item
107
66
108
67
109--------------------------------------------------------------------------------
68--------------------------------------------------------------------------------
src/Hakyll/Web/Template/Context.hs+10
@@ -4,6 +4,7 @@ module Hakyll.Web.Template.Context
4 , mapContext
4 , mapContext
5 , field
5 , field
6 , constField
6 , constField
7+ , functionField
7
8
8 , defaultContext
9 , defaultContext
9 , bodyField
10 , bodyField
@@ -70,6 +71,15 @@ constField :: String -> String -> Context a
70constField key = field key . const . return
71constField key = field key . const . return
71
72
72
73
74+--------------------------------------------------------------------------------
75+functionField :: String -> ([String] -> Item a -> Compiler String) -> Context a
76+functionField name value = Context $ \k i -> case words k of
77+ [] -> empty
78+ (n : args)
79+ | n == name -> value args i
80+ | otherwise -> empty
81+
82+
73--------------------------------------------------------------------------------
83--------------------------------------------------------------------------------
74defaultContext :: Context String
84defaultContext :: Context String
75defaultContext =
85defaultContext =
src/Hakyll/Web/Template/Read.hs+38−6
@@ -1,10 +1,42 @@
1−-- | Re-exports all different template reading modules
1+--------------------------------------------------------------------------------
2−--
2+-- | Read templates in Hakyll's native format
3module Hakyll.Web.Template.Read
3module Hakyll.Web.Template.Read
4 ( readTemplate
4 ( readTemplate
5− , readHamletTemplate
6− , readHamletTemplateWith
7 ) where
5 ) where
8
6
9−import Hakyll.Web.Template.Read.Hakyll
7+
8+--------------------------------------------------------------------------------
9+import Data.List (isPrefixOf)
10+import Data.Char (isAlphaNum)
11+
12+
13+--------------------------------------------------------------------------------
10−import Hakyll.Web.Template.Read.Hamlet
14+import Hakyll.Web.Template.Internal
15+
16+
17+--------------------------------------------------------------------------------
18+-- | Construct a @Template@ from a string.
19+readTemplate :: String -> Template
20+readTemplate = Template . readTemplate'
21+ where
22+ readTemplate' [] = []
23+ readTemplate' string
24+ | "$$" `isPrefixOf` string =
25+ Escaped : readTemplate' (drop 2 string)
26+ | "$" `isPrefixOf` string =
27+ case readKey (drop 1 string) of
28+ Just (key, rest) -> Key key : readTemplate' rest
29+ Nothing -> Chunk "$" : readTemplate' (drop 1 string)
30+ | otherwise =
31+ let (chunk, rest) = break (== '$') string
32+ in Chunk chunk : readTemplate' rest
33+
34+ -- Parse an key into (key, rest) if it's valid, and return
35+ -- Nothing otherwise
36+ readKey string =
37+ let (key, rest) = span validKeyChar string
38+ in if not (null key) && "$" `isPrefixOf` rest
39+ then Just (key, drop 1 rest)
40+ else Nothing
41+
42+ validKeyChar x = isAlphaNum x || x == ' '
src/Hakyll/Web/Template/Read/Hakyll.hsdeleted−35
@@ -1,35 +0,0 @@
1−-- | Read templates in Hakyll's native format
2−--
3−module Hakyll.Web.Template.Read.Hakyll
4− ( readTemplate
5− ) where
6−
7−import Data.List (isPrefixOf)
8−import Data.Char (isAlphaNum)
9−
10−import Hakyll.Web.Template.Internal
11−
12−-- | Construct a @Template@ from a string.
13−--
14−readTemplate :: String -> Template
15−readTemplate = Template . readTemplate'
16− where
17− readTemplate' [] = []
18− readTemplate' string
19− | "$$" `isPrefixOf` string =
20− Escaped : readTemplate' (drop 2 string)
21− | "$" `isPrefixOf` string =
22− case readKey (drop 1 string) of
23− Just (key, rest) -> Key key : readTemplate' rest
24− Nothing -> Chunk "$" : readTemplate' (drop 1 string)
25− | otherwise =
26− let (chunk, rest) = break (== '$') string
27− in Chunk chunk : readTemplate' rest
28−
29− -- Parse an key into (key, rest) if it's valid, and return
30− -- Nothing otherwise
31− readKey string =
32− let (key, rest) = span isAlphaNum string
33− in if not (null key) && "$" `isPrefixOf` rest
34− then Just (key, drop 1 rest)
35− else Nothing
src/Hakyll/Web/Template/Read/Hamlet.hsdeleted−46
@@ -1,46 +0,0 @@
1−-- | Read templates in the hamlet format
2−--
3−{-# LANGUAGE MultiParamTypeClasses #-}
4−module Hakyll.Web.Template.Read.Hamlet
5− ( readHamletTemplate
6− , readHamletTemplateWith
7− ) where
8−
9−import Text.Hamlet (HamletSettings, defaultHamletSettings)
10−import Text.Hamlet.RT
11−
12−import Hakyll.Web.Template.Internal
13−
14−-- | Read a hamlet template using the default settings
15−--
16−readHamletTemplate :: String -> Template
17−readHamletTemplate = readHamletTemplateWith defaultHamletSettings
18−
19−-- | Read a hamlet template using the specified settings
20−--
21−readHamletTemplateWith :: HamletSettings -> String -> Template
22−readHamletTemplateWith settings string =
23− let result = parseHamletRT settings string
24− in case result of
25− Just hamlet -> fromHamletRT hamlet
26− Nothing -> error
27− "Hakyll.Web.Template.Read.Hamlet.readHamletTemplateWith: \
28− \Could not parse Hamlet file"
29−
30−-- | Convert a 'HamletRT' to a 'Template'
31−--
32−fromHamletRT :: HamletRT -- ^ Hamlet runtime template
33− -> Template -- ^ Hakyll template
34−fromHamletRT (HamletRT sd) = Template $ map fromSimpleDoc sd
35− where
36− fromSimpleDoc :: SimpleDoc -> TemplateElement
37− fromSimpleDoc (SDRaw chunk) = Chunk chunk
38− fromSimpleDoc (SDVar [var]) = Key var
39− fromSimpleDoc (SDVar _) = error
40− "Hakyll.Web.Template.Read.Hamlet.fromHamletRT: \
41− \Hakyll does not support '.' in identifier names when using \
42− \hamlet templates."
43− fromSimpleDoc _ = error
44− "Hakyll.Web.Template.Read.Hamlet.fromHamletRT: \
45− \Only simple $key$ identifiers are allowed when using hamlet \
46− \templates."
tests/Hakyll/Web/Template/Tests.hs+11−2
@@ -6,6 +6,7 @@ module Hakyll.Web.Template.Tests
6
6
7
7
8--------------------------------------------------------------------------------
8--------------------------------------------------------------------------------
9+import Data.Monoid (mconcat)
9import Test.Framework (Test, testGroup)
10import Test.Framework (Test, testGroup)
10import Test.Framework.Providers.HUnit (testCase)
11import Test.Framework.Providers.HUnit (testCase)
11import Test.HUnit (Assertion, (@=?))
12import Test.HUnit (Assertion, (@=?))
@@ -32,9 +33,17 @@ case01 :: Assertion
32case01 = withTestStore $ \store -> do
33case01 = withTestStore $ \store -> do
33 provider <- newTestProvider store
34 provider <- newTestProvider store
34
35
35− out <- resourceString provider "example.md.out"
36+ out <- resourceString provider "template.html.out"
36 tpl <- testCompilerDone store provider "template.html" $ templateCompiler
37 tpl <- testCompilerDone store provider "template.html" $ templateCompiler
37 item <- testCompilerDone store provider "example.md" $
38 item <- testCompilerDone store provider "example.md" $
38− pageCompiler >>= applyTemplate (itemBody tpl) defaultContext
39+ pageCompiler >>= applyTemplate (itemBody tpl) testContext
39
40
40 out @=? itemBody item
41 out @=? itemBody item
42+
43+
44+--------------------------------------------------------------------------------
45+testContext :: Context String
46+testContext = mconcat
47+ [ functionField "echo" (\args _ -> return $ unwords args)
48+ , defaultContext
49+ ]
tests/data/template.html+1
@@ -1,3 +1,4 @@
1<div>
1<div>
2+ $echo test$
2 $body$
3 $body$
3</div>
4</div>
tests/data/example.md.out → tests/data/template.html.outrenamed+1
@@ -1,3 +1,4 @@
1<div>
1<div>
2+ test
2 <p>This is an example.</p>
3 <p>This is an example.</p>
3</div>
4</div>