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

Building post lists of multiple versions #805

Open
omalaspinas opened this issue Nov 7, 2020 · 4 comments
Open

Building post lists of multiple versions #805

omalaspinas opened this issue Nov 7, 2020 · 4 comments

Comments

@omalaspinas
Copy link

omalaspinas commented Nov 7, 2020

Hello,

sorry if it is a silly question. I am currently producing multiple versions of posts (html and pdf) and want to list them on a page. While have only the html version works just fine, I can't find a way to dynamically also show both versions. I usually end up with an error message like

  [ERROR] Hakyll.Core.Compiler.Require.load: test.markdown (pdf) (snapshot _final) was found in the cache, but does not have the right type: expected [Char] but got TmpFile

Any idea/advice on how to have both versions in the list?

Thanks in advance for your help.

@Minoru
Copy link
Collaborator

Minoru commented Nov 7, 2020

Hi! A fellow user here. Do you have a minimal example of this somewhere? I don't have an answer for you offhand, but I'd like to take a look at the code and see if I can help somehow.

@omalaspinas
Copy link
Author

Hello,

thank you very much for your answer. Basically what I'm doing is that I first generate two "versions" of my markdown files: one in html and the other in pdf (this is copy pasted from https://github.com/jaspervdj/jaspervdj basically):

    -- posts
    match "posts/*.markdown" $ do
        route $ setExtension "html"
        compile $ pandocCompiler
            >>= loadAndApplyTemplate "templates/class.html" postCtx
            >>= relativizeUrls

    -- posts in PDF
     match "posts/*.markdown" $ version "pdf" $ do
         route   $ setExtension "pdf"
         compile $ do getResourceBody
             >>= readPandoc
             >>= writeXeTex
             >>= loadAndApplyTemplate "templates/default.latex" defaultContext
             >>= xelatex

    where
      writeXeTex :: Item Pandoc.Pandoc -> Compiler (Item String)
      writeXeTex = traverse $ \pandoc ->
          case Pandoc.runPure (Pandoc.writeLaTeX Pandoc.def pandoc) of
              Left err -> fail $ show err
              Right x  -> return (T.unpack x)

xelatex :: Item String -> Compiler (Item TmpFile)
xelatex item = do
    TmpFile texPath <- newTmpFile "xelatex.tex"
    let tmpDir  = takeDirectory texPath
        pdfPath = replaceExtension texPath "pdf"

    unsafeCompiler $ do
        writeFile texPath $ itemBody item
        _ <- Process.system $ unwords ["xelatex", "-halt-on-error",
            "-output-directory", tmpDir, texPath, ">/dev/null", "2>&1"]
        return ()

    makeItem $ TmpFile pdfPath

This generates .html and .pdf files as I want (which is a good first step).

Then I would like to do something like

    -- post list
    create ["posts.html"] $ do
        route idRoute
        compile $ do
            posts <- recentFirst =<< loadAll ("posts/*.markdown" .&&. hasVersion "pdf")
            makeItem ""
                >>= loadAndApplyTemplate "templates/archive.html" (pageCtx posts "MyPage")
                >>= relativizeUrls

Unfortunately it miserably fails with the error message printed above (that I reproduce below for completeness....). It works fine without the pdf part though.

  [ERROR] Hakyll.Core.Compiler.Require.load: test.markdown (pdf) (snapshot _final) was found in the cache, but does not have the right type: expected [Char] but got TmpFile

The rest of the code is basically like in any hakyll example (copying static files, ...).

@Minoru
Copy link
Collaborator

Minoru commented Nov 7, 2020

Hmm, I don't see anything wrong with your code. I tried to reproduce the problem with my own blog by replacing hasNoVersion with hasVersion "gzipped" here ("gzipped" versions are produced here using a compiler very similar to yours), but no luck: for me, it just works.

Perhaps your cache is broken by some previous experiments? Try running yoursite clean (stack run -- clean, Cabal probably has a command for that too, haven't used it in a while) and retrying then.

If that doesn't help, can you please post the whole code somewhere so I can clone it and play around? Copy-pasting things from here and there is a bit slow, and I might end up with a setup that's slightly — but crucially — different from yours.

@omalaspinas
Copy link
Author

Thanks! I'll try to put a minimal example somewhere. For the moment it's in a huge file with some useless stuff that only add noise. I'll post it during the week I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants