Copying a file outside of my website's directory

15 views
Skip to first unread message

Ashley Smith

unread,
Aug 2, 2022, 4:56:36 PM8/2/22
to hakyll
Hello, I have a bit of a weird site setup, and I'm in a situation where I have a path to something I want to copy into my build directory of my site. I have successfully read the file, but I'm struggling to try and dump the contents into a file. Here's my code:

```hs
copyFile :: FilePath -> FilePath -> FilePath -> Rules ()
copyFile from to file = create [fromFilePath $ to </> file] $ do
  route idRoute
  compile $ do
    content <- unsafeCompiler $ readFile (from </> file)
    makeItem content
```
However, when I do this I regularly get the error:
```
  updated assets/thirdparty/mathjax/node-main.js
  updated assets/thirdparty/mathjax/output/chtml.js
  updated assets/thirdparty/mathjax/output/chtml/fonts/tex.js
  updated assets/thirdparty/mathjax/output/chtml/fonts/woff-v2/MathJax_AMS-Regular.woff
  updated assets/thirdparty/mathjax/output/chtml/fonts/woff-v2/MathJax_Calligraphic-Bold.woff
site: /nix/store/86jgml5h1smrppy753xfpq5ip8d1m0ac-thirdparty/mathjax/output/chtml/fonts/woff-v2/MathJax_AMS-Regular.woff: hGetContents: invalid argument (invalid byte sequence)
```
I suspect this isn't a problem with Hakyll in particular.. however, when I used to have this file as a part of my website's data it would copy it just fine, so maybe there'd be a better way of writing this file. Maybe I should ignore hakyll completely and just use writeFile?

Ashley Smith

unread,
Aug 2, 2022, 5:10:00 PM8/2/22
to hakyll
Okay, I tried using writeFile and I have the same issue.. so not a hakyll issue I guess. Hopefully can get this sorted soon :( 

Gwern Branwen

unread,
Aug 2, 2022, 5:11:19 PM8/2/22
to hak...@googlegroups.com
On Tue, Aug 2, 2022 at 4:56 PM Ashley Smith <ash...@aas.sh> wrote:
> Maybe I should ignore hakyll completely and just use writeFile?

That seems reasonable to me. I write or copy stuff into _site/ all the
time, and it works fine as long as Hakyll doesn't need to handle that
file any further.

--
gwern
https://www.gwern.net

Ashley Smith

unread,
Aug 2, 2022, 5:14:16 PM8/2/22
to hakyll
Yes, I've tried that which is quite annoying. It's specifically something in MathJax - MathJax_AMS-Regular.woff.

I saw another answer somewhere and so I'm trying this at the moment:

```

copyFile :: FilePath -> FilePath -> FilePath -> Rules ()
copyFile from to file = create [fromFilePath $ to </> file] $ do
  route idRoute
  compile $ do
    content <- unsafeCompiler $ do
      setLocaleEncoding utf8
      setFileSystemEncoding utf8
      setForeignEncoding utf8

      readFile (from </> file)
    makeItem content
```
Going to sleep soon, hopefully the answer will come to me tomorrow? Would love to get this working

Gwern Branwen

unread,
Aug 2, 2022, 5:24:19 PM8/2/22
to hak...@googlegroups.com
Well, as far as that goes, I think the obvious question one would ask
is, why you are trying to read a WOFF as a text file? Isn't readFIle
for returning a String or ByteString or Text? A WOFF font file is a
bunch of zlib-compressed binary gunk designed to convey fonts as
efficiently as possible over the web. Not something you'd expect to be
valid readable UTF8, nor would it mean anything when interpreted as
natural-language text. It's a binary blob.

--
gwern

Ashley Smith

unread,
Aug 3, 2022, 4:04:30 AM8/3/22
to hakyll
Yes, that was the issue. I had a lapse of brain functionality it seems!

Thanks for the sanity check! It works now :) 

Reply all
Reply to author
Forward
0 new messages