module Cake.Tex where
import Cake.Core
import Cake.Process
import System.Directory
import System.FilePath
import Control.Applicative
import Control.Monad.RWS (liftIO)
import Control.Monad (when)
import Data.List
import Data.List.Split
import Data.Char (isSpace)
import Cake.Actions
_mpost x = system ["mpost", x]
_pdflatex x = system ["pdflatex",x]
_xelatex x = system ["xelatex",x]
_bibtex x = system ["bibtex",x]
pandoc inp typ options = produce out $ do
need inp
cut $ system $ ["pandoc",inp,"-t",typ,"-o",out] ++ options
where out = replaceExtension inp ext
ext = case typ of
"latex" -> "tex"
_ -> typ
graphviz program inp typ options = produce out $ do
needs [inp]
cut $ system $ [program, "-T"++typ, "-o"++out, inp] ++ options
where out = replaceExtension inp typ
argsOf s = map (init . (drop $ 2 + length s)) . filter (("\\" ++ s ++ "{") `isPrefixOf`)
getBibFiles input = distill (Custom ["bibfiles",input]) $ do
ls <- map (drop 14) . filter ("\\bibliography{" `isPrefixOf`) . lines <$> Cake.Actions.readFile input
let bibs = map (++".bib") $ case ls of
[] -> []
(l:_) -> splitOn "," $ reverse . dropWhile (== '}') . reverse $ l
return $ Text bibs
includedTex = map (++? ".tex") . argsOf "input" . lines
includedLhs = filter (`notElem` stdinclude) .
map (dropSpaces . drop (length prefix)) . filter (prefix `isPrefixOf`) .
map (dropWhile isSpace) .
lines
where stdinclude = ["lhs2TeX.fmt","polycode.fmt"]
prefix = "%include"
pdflatexBibtex c = do
let [input,aux,pdf,bbl] = map (c <.>) ["tex","aux","pdf","bbl"]
produces [aux,pdf] $ do
chaseDeps includedTex input
cut $ _pdflatex c
bibliographyExists <- do
aux_contents <- Cake.Actions.readFile aux
return ("\\bibdata" `isInfixOf` aux_contents)
when bibliographyExists $ do
produce bbl $ do
Text bibs <- getBibFiles input
needs bibs
cut $ _bibtex c
updates [aux,pdf] $ do
use aux
when bibliographyExists $ use bbl >> return ()
cut $ _pdflatex c
_lhs2TeX i o = do
system ["lhs2TeX","-o",o,i]
lhs2tex c = produce tex $ do
chaseDeps includedLhs lhs
cut $ _lhs2TeX lhs tex
where lhs = c ++ ".lhs"
tex = c ++ ".tex"