on master help sign in
Merge pull request #482 from nickie/fix-yaml-number

Fix integer fields in YAML metadata

commit0382eee8c86fe31082b9ecd44c40170d2f910cd5 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateWed 16 Nov 2016, 17:42 (10 years ago) parente61d910f9f8603
5 files changed+22−1
src/Data/Yaml/Extended.hs+5−1 tests/Hakyll/Web/Template/Tests.hs+1 tests/data/example.md.metadata+2 tests/data/just-meta.html+7 tests/data/just-meta.html.out+7
src/Data/Yaml/Extended.hs+5−1
@@ -7,12 +7,16 @@ module Data.Yaml.Extended
77import qualified Data.Text as T
88import qualified Data.Vector as V
99import Data.Yaml
10+import Data.Scientific
1011
1112toString :: Value -> Maybe String
1213toString (String t) = Just (T.unpack t)
1314toString (Bool True) = Just "true"
1415toString (Bool False) = Just "false"
15−toString (Number d) = Just (show d)
16+-- | Make sure that numeric fields containing integer numbers are shown as
17+-- | integers (i.e., "42" instead of "42.0").
18+toString (Number d) | isInteger d = Just (formatScientific Fixed (Just 0) d)
19+ | otherwise = Just (show d)
1620toString _ = Nothing
1721
1822toList :: Value -> Maybe [Value]
tests/Hakyll/Web/Template/Tests.hs+1
@@ -29,6 +29,7 @@ tests :: TestTree
2929tests = testGroup "Hakyll.Core.Template.Tests" $ concat
3030 [ [ testCase "case01" $ test ("template.html.out", "template.html", "example.md")
3131 , testCase "case02" $ test ("strip.html.out", "strip.html", "example.md")
32+ , testCase "case03" $ test ("just-meta.html.out", "just-meta.html", "example.md")
3233 , testCase "applyJoinTemplateList" testApplyJoinTemplateList
3334 ]
3435
tests/data/example.md.metadata+2
@@ -1,3 +1,5 @@
11external: External data
22date: 2012-10-22 14:35:24
33subblog: food
4+intfield: 42
5+numfield: 3.14
tests/data/just-meta.htmlnew file+7
@@ -0,0 +1,7 @@
1+<pre>
2+external: {$external$}
3+date: {$date$}
4+subblog: {$subblog$}
5+intfield: {$intfield$}
6+numfield: {$numfield$}
7+</pre>
tests/data/just-meta.html.outnew file+7
@@ -0,0 +1,7 @@
1+<pre>
2+external: {External data}
3+date: {2012-10-22 14:35:24}
4+subblog: {food}
5+intfield: {42}
6+numfield: {3.14}
7+</pre>