summaryrefslogtreecommitdiff
path: root/src/WWW/HW.hs
blob: 93879610386b3742406409ff93025ca8a003c993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module WWW.HW
    ( removeEnv
    , removeSolutions
    , setDocLocation
    , setDocLocation_
    -- * LaTeX utils
    , LaTeX (..)
    , texmap
    -- * Hakyll utils
    , module Hakyll.TeX.HaTeX
    , module Hakyll.TeX.LaTeX
    ) where

import Prolog
import Hakyll
import Hakyll.TeX.HaTeX
import Hakyll.TeX.LaTeX
import Network.URI
import Text.LaTeX.Base.Syntax
import Text.LaTeX.Packages.Hyperref

removeEnv :: (String -> Bool) -> LaTeX -> LaTeX
removeEnv p = texmap (\case { TeXEnv s _ _ -> p s; _ -> False }) $ pure mempty

removeSolutions :: LaTeX -> LaTeX
removeSolutions = removeEnv (== "solution")

setDocLocation :: URI -> (String -> [TeXArg] -> Bool) -> Item LaTeX -> Compiler (Item LaTeX)
setDocLocation u p (Item i l) = Item i <$> texmapM (\case { TeXComm _ _ -> True; _ -> False }) mapper l
    where
        mapper = \case
            TeXComm s args | p s args -> do
                relpath <- getRoute i >>= maybe (fail $ show i <> " has no route") pure >>= maybe (fail $ "Cannot parse " <> show i <> " as a relative URI") pure . parseRelativeReference
                pure $ TeXComm s [ FixArg $ url $ toSL $ relpath `relativeTo` u ]
            x -> pure x

setDocLocation_ :: URI -> Item LaTeX -> Compiler (Item LaTeX)
setDocLocation_ u = setDocLocation u $ \s _ -> s == "doclink"