Skip to content

Commit 84cbce5

Browse files
committed
Use with-utf8 to fix text encoding problems
1 parent cb3e9b7 commit 84cbce5

File tree

8 files changed

+51
-7
lines changed

8 files changed

+51
-7
lines changed

.circleci/config.yml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
working_directory: backend
3737
command: |
3838
source ~/.profile
39+
cachix use rnons
3940
nix-build -j2 --cores 2
4041
cp -r result/bin bin
4142

backend/default.nix

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
, compiler ? "ghc864"
33
}:
44

5-
nixpkgs.pkgs.haskell.packages.${compiler}.callPackage ./ted2srt.nix {}
5+
let
6+
overrides = import ../nix/all.nix { inherit nixpkgs compiler; };
7+
in
8+
nixpkgs.pkgs.haskell.packages.${compiler}.callPackage ./ted2srt.nix overrides

backend/package.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dependencies:
4242
- unordered-containers
4343
- vector
4444
- wai
45+
- with-utf8
4546
- xml-conduit
4647

4748
# The library contains all of our application code. The executable

backend/src/Web/TED.hs

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import RIO
1919
import RIO.List.Partial (head)
2020
import System.Directory
2121
import System.IO (hPutStr, hPutStrLn, openFile, print)
22+
import qualified System.IO.Utf8 as Utf8
2223
import Text.Printf
2324

2425
import Web.TED.API as TED
@@ -141,7 +142,7 @@ oneSub sub = do
141142
"%02d:%02d:%02d,%03d\n%s\n\n"
142143
else "%d\n%02d:%02d:%02d.%03d --> " ++
143144
"%02d:%02d:%02d.%03d\n%s\n\n"
144-
ppr h (c,i) = do
145+
ppr h (c,i) = Utf8.withHandle h $ do
145146
let st = startTime c + floor (timeLag sub)
146147
sh = st `div` 1000 `div` 3600
147148
sm = st `div` 1000 `mod` 3600 `div` 60
@@ -165,9 +166,10 @@ oneTxt sub = do
165166
else do
166167
txt <- TED.getTalkTranscript (talkId sub) (head $ language sub)
167168
-- Prepend the UTF-8 byte order mark to do Windows user a favor.
168-
withBinaryFile path WriteMode $ \h ->
169+
withBinaryFile path WriteMode $ \h -> Utf8.withHandle h $
169170
hPutStr h "\xef\xbb\xbf"
170-
T.appendFile path txt
171+
Utf8.withFile path AppendMode $ \h ->
172+
T.hPutStrLn h txt
171173
return $ Just path
172174

173175
oneLrc :: Subtitle -> IO (Maybe FilePath)

backend/stack.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ packages:
4141
# (e.g., acme-missiles-0.3)
4242
extra-deps:
4343
- hedis-0.12.9
44+
- with-utf8-1.0.0.0
4445

4546
# Override default flag values for local packages and extra-deps
4647
flags: {}

backend/ted2srt.nix

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
, persistent, persistent-postgresql, persistent-template
66
, raw-strings-qq, regex-posix, rio, servant-lucid, servant-server
77
, stdenv, system-filepath, text, time, transformers
8-
, unordered-containers, vector, wai, wai-extra, warp, xml-conduit
8+
, unordered-containers, vector, wai, wai-extra, warp, with-utf8
9+
, xml-conduit
910
, postgresql_11}:
1011
mkDerivation {
1112
pname = "ted2srt";
@@ -20,7 +21,7 @@ mkDerivation {
2021
monad-logger mtl network persistent persistent-postgresql
2122
persistent-template raw-strings-qq regex-posix rio servant-lucid
2223
servant-server system-filepath text time transformers
23-
unordered-containers vector wai xml-conduit
24+
unordered-containers vector wai with-utf8 xml-conduit
2425
];
2526
librarySystemDepends = [ postgresql_11 ];
2627
libraryToolDepends = [ hpack ];
@@ -31,7 +32,8 @@ mkDerivation {
3132
monad-logger mtl network persistent persistent-postgresql
3233
persistent-template raw-strings-qq regex-posix rio servant-lucid
3334
servant-server system-filepath text time transformers
34-
unordered-containers vector wai wai-extra warp xml-conduit
35+
unordered-containers vector wai wai-extra warp with-utf8
36+
xml-conduit
3537
];
3638
doHaddock = false;
3739
prePatch = "hpack";

nix/all.nix

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{ nixpkgs ? import ./nixpkgs.nix {}
2+
, compiler ? "ghc864"
3+
}:
4+
5+
let
6+
global = { inherit nixpkgs compiler; };
7+
8+
in {
9+
with-utf8 = import ./pkgs/with-utf8.nix global;
10+
}

nix/pkgs/with-utf8.nix

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{ nixpkgs
2+
, compiler
3+
, callPackage ? nixpkgs.pkgs.haskell.packages.${compiler}.callPackage
4+
}:
5+
let with-utf8 = callPackage
6+
({ mkDerivation, base, deepseq, hedgehog, HUnit, safe-exceptions
7+
, tasty, tasty-discover, tasty-hedgehog, tasty-hunit, temporary
8+
, text, unix, stdenv
9+
}:
10+
mkDerivation {
11+
pname = "with-utf8";
12+
version = "1.0.0.0";
13+
sha256 = "06xznaszw7d6rznvzhzw3y4z31b4vx4djms85rq4qsbpfbdrh2zc";
14+
libraryHaskellDepends = [ base safe-exceptions text ];
15+
testHaskellDepends = [
16+
base deepseq hedgehog HUnit safe-exceptions tasty tasty-hedgehog
17+
tasty-hunit temporary text unix
18+
];
19+
testToolDepends = [ tasty-discover ];
20+
description = "Get your IO right on the first try";
21+
license = stdenv.lib.licenses.mpl20;
22+
}) {};
23+
in nixpkgs.pkgs.haskell.lib.dontHaddock
24+
(nixpkgs.pkgs.haskell.lib.dontCheck with-utf8)

0 commit comments

Comments
 (0)