Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error messages #722

Merged
merged 22 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2b87d13
Better error messages for context fields
bergus Aug 4, 2016
8ac7949
Better error messages for template applications
bergus Aug 4, 2016
9ec43a6
Better error messages for template parsing
bergus Aug 4, 2016
7031661
docs, docs, docs! Full coverage for Template and Context
bergus Aug 5, 2016
f98a220
Less debug messages from templates
bergus Aug 11, 2016
0ec007e
Better error messages from Alternative fails
bergus Aug 17, 2016
458e78d
Better error messages for templates
bergus Aug 17, 2016
dd68b2e
Merge branch 'master' into error-messages
bergus Mar 8, 2018
f546381
test case for mismatched template syntax
bergus Mar 8, 2018
7f1b00e
Cleanup of CompilerResult, use extra data type instead of Verbosity
bergus Mar 10, 2018
1e04f93
Better names, some minor simplifications
bergus Mar 10, 2018
2f6ef3a
Added useful exports and improved documenttion
bergus Mar 15, 2018
c098216
Merge remote-tracking branch 'origin/master'
bergus Mar 27, 2018
e523fb7
better error messages for file system operations
bergus Mar 25, 2018
f6af7d4
more precise error messages for template application
bergus Mar 19, 2018
e263ccf
fix documentation syntax
bergus Apr 9, 2018
9108ec5
Merge branch 'bergus/error-messages' into error-messages
jaspervdj Aug 20, 2019
b2eba3c
Renaming, use NonEmpty for compilation failures
jaspervdj Aug 20, 2019
1afc807
More renaming, fix tests
jaspervdj Aug 20, 2019
c7c581c
Fix some PR comments
jaspervdj Aug 27, 2019
588d6b7
Fix feed template types
jaspervdj Aug 27, 2019
f96b99e
Explicitly embed templates
jaspervdj Aug 28, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions hakyll.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,26 @@ Library
data-default >= 0.4 && < 0.8,
deepseq >= 1.3 && < 1.5,
directory >= 1.0 && < 1.4,
file-embed >= 0.0.10.1 && < 0.0.12,
filepath >= 1.0 && < 1.5,
lrucache >= 1.1.1 && < 1.3,
mtl >= 1 && < 2.3,
network-uri >= 2.6 && < 2.7,
optparse-applicative >= 0.12 && < 0.15,
parsec >= 3.0 && < 3.2,
process >= 1.6 && < 1.7,
random >= 1.0 && < 1.2,
regex-tdfa >= 1.1 && < 1.3,
resourcet >= 1.1 && < 1.3,
scientific >= 0.3.4 && < 0.4,
tagsoup >= 0.13.1 && < 0.15,
template-haskell >= 2.14 && < 2.15,
text >= 0.11 && < 1.3,
time >= 1.8 && < 1.10,
time-locale-compat >= 0.1 && < 0.2,
unordered-containers >= 0.2 && < 0.3,
vector >= 0.11 && < 0.13,
yaml >= 0.8.11 && < 0.12,
optparse-applicative >= 0.12 && < 0.15,
file-embed >= 0.0.10.1 && < 0.0.12
yaml >= 0.8.11 && < 0.12

If flag(previewServer)
Build-depends:
Expand Down
22 changes: 10 additions & 12 deletions lib/Hakyll/Web/Feed.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

--------------------------------------------------------------------------------
-- | A Module that allows easy rendering of RSS feeds.
Expand Down Expand Up @@ -38,27 +38,25 @@ import Hakyll.Web.Template.List


--------------------------------------------------------------------------------
import Data.FileEmbed (makeRelativeToProject, embedFile)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.FileEmbed (makeRelativeToProject)


--------------------------------------------------------------------------------
rssTemplate :: Template
rssTemplate = readTemplate $ T.unpack $ T.decodeUtf8 $
$(makeRelativeToProject "data/templates/rss.xml" >>= embedFile)
rssTemplate =
$(makeRelativeToProject "data/templates/rss.xml" >>= embedTemplate)

rssItemTemplate :: Template
rssItemTemplate = readTemplate $ T.unpack $ T.decodeUtf8 $
$(makeRelativeToProject "data/templates/rss-item.xml" >>= embedFile)
rssItemTemplate =
$(makeRelativeToProject "data/templates/rss-item.xml" >>= embedTemplate)

atomTemplate :: Template
atomTemplate = readTemplate $ T.unpack $ T.decodeUtf8 $
$(makeRelativeToProject "data/templates/atom.xml" >>= embedFile)
atomTemplate =
$(makeRelativeToProject "data/templates/atom.xml" >>= embedTemplate)

atomItemTemplate :: Template
atomItemTemplate = readTemplate $ T.unpack $ T.decodeUtf8 $
$(makeRelativeToProject "data/templates/atom-item.xml" >>= embedFile)
atomItemTemplate =
$(makeRelativeToProject "data/templates/atom-item.xml" >>= embedTemplate)


--------------------------------------------------------------------------------
Expand Down
23 changes: 23 additions & 0 deletions lib/Hakyll/Web/Template.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
-- > 3...2...1
-- > </p>
--
{-# LANGUAGE TemplateHaskell #-}
module Hakyll.Web.Template
( Template
, templateBodyCompiler
Expand All @@ -148,8 +149,30 @@ module Hakyll.Web.Template
, readTemplate
, compileTemplateItem
, unsafeReadTemplateFile
, embedTemplate
) where


--------------------------------------------------------------------------------
import Hakyll.Web.Template.Internal


--------------------------------------------------------------------------------
import Data.FileEmbed (embedFile)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Language.Haskell.TH (Exp, Q)


--------------------------------------------------------------------------------
-- | Embed template allows you embed a template within the Haskell binary.
-- Example:
--
-- > myTemplate :: Template
-- > myTemplate = $(embedTemplate "test.html")
embedTemplate :: FilePath -> Q Exp
embedTemplate filePath = [|
let source = T.unpack $ T.decodeUtf8 $(embedFile filePath) in
case parseTemplateElemsFile filePath source of
Left err -> error err
Right tpl -> template filePath tpl |]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is how to do it properly with Template Haskell - thanks!

1 change: 1 addition & 0 deletions lib/Hakyll/Web/Template/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ readTemplate :: String -> Template
readTemplate = either error (template origin) . parseTemplateElemsFile origin
where
origin = "{literal}"
{-# DEPRECATED readTemplate "Use templateCompiler instead" #-}

--------------------------------------------------------------------------------
-- | Parse an item body into a template.
Expand Down
31 changes: 27 additions & 4 deletions tests/Hakyll/Web/Template/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Hakyll.Web.Template.Tests
( tests
) where


--------------------------------------------------------------------------------
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (Assertion, testCase, (@=?),
(@?=), assertBool)
import Test.Tasty.HUnit (Assertion, assertBool, testCase,
(@=?), (@?=))

import Data.Either (isLeft)
import Data.Either (isLeft)

--------------------------------------------------------------------------------
import Hakyll.Core.Compiler
Expand Down Expand Up @@ -83,6 +84,8 @@ tests = testGroup "Hakyll.Web.Template.Tests" $ concat
, assertBool "did not fail to parse" $ isLeft $
parse "$for(xs)$\n <p>foo</p>\n$endif$"
]

, [testCase "embeddedTemplate" testEmbeddedTemplate]
]
where
parse = parseTemplateElemsFile ""
Expand Down Expand Up @@ -122,6 +125,8 @@ testApplyJoinTemplateList :: Assertion
testApplyJoinTemplateList = do
store <- newTestStore
provider <- newTestProvider store
tpl <- testCompilerDone store provider "tpl" $
compileTemplateItem (Item "tpl" "<b>$body$</b>")
str <- testCompilerDone store provider "item3" $
applyJoinTemplateList ", " tpl defaultContext [i1, i2]

Expand All @@ -130,4 +135,22 @@ testApplyJoinTemplateList = do
where
i1 = Item "item1" "Hello"
i2 = Item "item2" "World"
tpl = readTemplate "<b>$body$</b>"


--------------------------------------------------------------------------------
embeddedTemplate :: Template
embeddedTemplate = $(embedTemplate "tests/data/embed.html")

--------------------------------------------------------------------------------
testEmbeddedTemplate :: Assertion
testEmbeddedTemplate = do
store <- newTestStore
provider <- newTestProvider store
str <- testCompilerDone store provider "item3" $
applyTemplate embeddedTemplate defaultContext item

itemBody str @?= "<p>Hello, world</p>\n"
cleanTestEnv
where
item = Item "item1" "Hello, world"

1 change: 1 addition & 0 deletions tests/data/embed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>$body$</p>