Skip to content

Commit

Permalink
Explicitly set local encoding to UTF-8 in site.hs
Browse files Browse the repository at this point in the history
This change makes sure that the site executable *always* expects UTF-8, regardless of any locale environment variables. This should avoid problems like #29 and #249 in a more robust way than trying to set the right environment variables in `buildAndWatch` or `shell.nix`.

The diff ended up a bit noisy because of indentation changes; the key change was adding `Encoding.setLocaleEncoding Encoding.utf8` to the beginning of `main`.

I tested this by reproducing the Unicode error (using `nix-shell --pure`) and checking that the Haskell code change fixes the problem.
  • Loading branch information
TikhonJelvis committed Feb 3, 2023
1 parent f36b210 commit 011a0dd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 53 deletions.
1 change: 0 additions & 1 deletion buildAndWatch
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ function buildAndWatchWithNix() {
exit 1
fi

export LC_ALL=C.UTF-8 # fix locale error with Hakyll (see #29)
nix-build -A builder && \
./result/bin/haskell-org-site clean && \
./result/bin/haskell-org-site build && \
Expand Down
101 changes: 52 additions & 49 deletions builder/site.hs
Original file line number Diff line number Diff line change
@@ -1,68 +1,71 @@
--------------------------------------------------------------------------------
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
import Data.Aeson
import qualified Data.ByteString.Lazy as BL
import Data.Monoid ((<>))
import Data.Time.Calendar
import Data.Time.Clock
import qualified GHC.IO.Encoding as Encoding
import Hakyll
import Hakyll.Core.Compiler
import Hakyll.Core.Compiler.Internal
import Hakyll.Core.Provider
import System.FilePath.Posix
import Testimonial


--------------------------------------------------------------------------------
main :: IO ()
main = mkContext >>= \ctx -> hakyllWith configuration $ do
match "testimonials/logos/*" $ do
route idRoute
compile copyFileCompiler

match "testimonials/*.yaml" $ do
compile parseTestimonialCompiler

create ["testimonials.json"] $ do
route idRoute
compile $ do
testimonials <- loadAll @Testimonial "testimonials/*.yaml"
item <- (makeItem . BL.unpack . encode . map itemBody) testimonials
saveSnapshot "_final" item
pure item

match "img/*" $ do
route idRoute
compile copyFileCompiler

match "css/*" $ do
route idRoute
compile compressCssCompiler

match "js/*" $ do
route idRoute
compile copyFileCompiler

match "index.html" $ do
route idRoute
compile $ do
testimonials <- loadAll @Testimonial "testimonials/*.yaml"
let
indexCtx = listField "testimonials" testimonialContext (pure testimonials) `mappend`
ctx
defCompiler indexCtx

match ("**/*.markdown" .||. "*.markdown") $ do
route cleanRoute
compile $ mdCompiler ctx

match "*.pdf" $ do
route idRoute
compile copyFileCompiler

match "templates/*" $
compile templateCompiler
main = do
Encoding.setLocaleEncoding Encoding.utf8
ctx <- mkContext
hakyllWith configuration $ do
match "testimonials/logos/*" $ do
route idRoute
compile copyFileCompiler

match "testimonials/*.yaml" $ do
compile parseTestimonialCompiler

create ["testimonials.json"] $ do
route idRoute
compile $ do
testimonials <- loadAll @Testimonial "testimonials/*.yaml"
item <- (makeItem . BL.unpack . encode . map itemBody) testimonials
saveSnapshot "_final" item
pure item

match "img/*" $ do
route idRoute
compile copyFileCompiler

match "css/*" $ do
route idRoute
compile compressCssCompiler

match "js/*" $ do
route idRoute
compile copyFileCompiler

match "index.html" $ do
route idRoute
compile $ do
testimonials <- loadAll @Testimonial "testimonials/*.yaml"
let
indexCtx = listField "testimonials" testimonialContext (pure testimonials) `mappend`
ctx
defCompiler indexCtx

match ("**/*.markdown" .||. "*.markdown") $ do
route cleanRoute
compile $ mdCompiler ctx

match "*.pdf" $ do
route idRoute
compile copyFileCompiler

match "templates/*" $
compile templateCompiler


configuration :: Configuration
Expand Down
3 changes: 0 additions & 3 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ let
] ./.;
buildInputs = [ builder pkgs.linkchecker ];

LOCALE_ARCHIVE = "${pkgs.glibcLocales}/lib/locale/locale-archive";
LC_ALL = "C.UTF-8";

buildPhase = ''
${builder}/bin/haskell-org-site build
'';
Expand Down

0 comments on commit 011a0dd

Please sign in to comment.