forked from scravy/simplex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplex.hs
More file actions
208 lines (154 loc) · 6.69 KB
/
simplex.hs
File metadata and controls
208 lines (154 loc) · 6.69 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
module Main (main) where
import Prelude hiding (lex)
import Simplex.Config
import Simplex.ConfigData
import Simplex.CmdLineOpts
import Simplex.Parser
import Simplex.Specials
import Simplex.ToTeX
import Simplex.Util
import Text.Printf
import Data.List (sort)
import Data.Maybe
import Data.Time
import System.Console.GetOpt (usageInfo)
import System.Directory
import System.FilePath
import System.Environment (getArgs, getProgName)
import System.IO
import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Monad.Cont
versionInfo = "Simplex -- Simple LaTeX -- v0.3.8 by Julian Fleischer"
dirtyExts = [".toc", ".aux", ".log", ".tex", ".out", ".lof", ".ent"]
simplexExts = [".simple", ".lhs", ".smplx", ".simplex"]
isSimplexFile = flip elem simplexExts . takeExtension
gatherChangedFiles :: String -> FilePath -> IO [(FilePath, UTCTime)]
gatherChangedFiles ext dir = do
files' <- getDirectoryContents dir
let files = filter isSimplexFile files'
mtimes1 <- mapM getModificationTime files
mtimes2 <- mapM (getModificationTime' . flip addExtension ext . dropExtension) files
let files' = zip3 files mtimes1 mtimes2
return $ map (\(f, t, _) -> (f, t)) $ filter (\(_, t, t') -> t > t') files'
main :: IO ()
main = parseArgs >>= either (uncurry simplex) (mapM_ putStr)
simplex :: Opts -> [String] -> IO ()
-- ^ The "start up sequence" of the simplex tool
simplex opts files
| optHelp opts = do
cmd <- getProgName
putStrLn (usageInfo (printf "%s\nUsage: %s [options] [files...]\n" versionInfo cmd) cmdOpts)
| optVersion opts = putStrLn versionInfo
| isJust $ optListSymbols opts = do
let prints f str = case f of
('%':'s':xs) -> str ++ prints xs str
(x:xs) -> x : prints xs str
_ -> ""
mapM_ (putStr . prints (fromJust (optListSymbols opts))) (sort knownSymbols)
| isJust $ optWatch opts = do
getCurrentTime >>= watch opts "." (fromJust $ optWatch opts)
| null files && optForce opts = do
files' <- getDirectoryContents "."
simplex' opts $ filter isSimplexFile files'
| null files = do
files' <- gatherChangedFiles (optType opts) "."
if null files' then putStrLn "All files up-to-date, nothing to do."
else simplex' opts $ map fst files'
| otherwise = simplex' opts files
simplex' :: Opts -> [String] -> IO ()
-- ^ This part delegates the list of files to @process@
simplex' opts files = do
mapM_ (flip runContT return . callCC . process opts) files
watch :: Opts -> FilePath -> Int -> UTCTime -> IO ()
-- ^ Polls the list of changed files and runs @simplex'@ periodically
watch opts dir int time = do
files <- gatherChangedFiles (optType opts) dir
max <- if null files then return time else do
let max = maximum $ map snd files
simplex' opts $ map fst $ filter ((> time) . snd) files
return max
threadDelay (int * 1000)
watch opts dir int max
data Result = Exc IOException
| Str String
| Err String
| Ok
deriving Show
process :: Opts -> FilePath
-> (Result -> ContT Result IO Result)
-> ContT Result IO Result
-- | ^ Does the actual processing of a simplex file.
process opts file exit = do
let filename = takeBaseName file
prepend = zipWith (++) (repeat filename)
filetype = optType opts
verbose = optVerbose opts
pdflatex = optPdflatex opts
pdfcrop = optPdfcrop opts
convert = optConvert opts
pdfopts = ["-interaction=nonstopmode", "-file-line-error"]
print x = liftIO (putStr x >> hFlush stdout)
println x = liftIO (putStrLn x >> hFlush stdout)
print' x = unless (optPrint opts) (print x)
throw' s x = do
liftIO $ mapM_ removeIfExists (sRemoveFiles s)
throw x
throw x = println " Splat!" >> error x >> exit x
where
error (Exc e) = print "-> " >> println (show e)
error (Str s) = print "-> " >> println s
error (Err e)
| length lns <= 2 = mapM_ (error . Str) (lines e)
| otherwise = mapM_ (error . Str) (filter isErr (lines e))
where lns = lines e
isErr = (~= "^[^ ]+:[0-9]+: ")
print' $ "Processing " ++ file
h <- liftIO $ openFile file ReadMode
liftIO $ hSetEncoding h utf8
f <- liftIO $ try (hGetContents h)
(Str c) <- either (throw . Exc) (return . Str) f
tok <- liftIO $ loadIncludes True (lex c) >>= loadHashbangs
print' "."
let cfg = defaultConfig { oStandalone = optType opts == "png" }
(spec, tok') <- liftIO $ if (optDryRun opts)
then (return $ (newSpec, parse tok))
else (processSpecials opts newSpec $ parse tok)
let tex = toTeX cfg tok'
print' "."
when (optType opts == "tex" || not (optDryRun opts)) $ do
-- write tex-file
r <- liftIO $ try (writeFile (filename ++ ".tex") tex)
_ <- either (throw . Exc) (return . const Ok) r
print' "."
unless (optDryRun opts || optType opts == "tex") $ do
-- run pdflatex
r <- liftIO $ exec verbose pdflatex (pdfopts ++ [filename ++ ".tex"])
_ <- either (throw' spec . Err . snd) (return . const Ok) r
print' "."
-- run pdflatex a second time
_ <- liftIO $ exec verbose pdflatex (pdfopts ++ [filename ++ ".tex"])
print' "."
-- run pdflatex a third time if desired
when (optThreeTimes opts) $ do { liftIO $ exec verbose pdflatex (pdfopts ++ [filename ++ ".tex"])
; print' "." }
-- clean files
unless (optNoClean opts) (liftIO $ do { mapM_ removeIfExists (prepend dirtyExts)
; mapM_ removeIfExists (sRemoveFiles spec) } )
when (optCrop opts) $ do
r <- liftIO $ exec verbose pdfcrop [filename ++ ".pdf", filename ++ "-crop.pdf"]
_ <- either (throw . Err . snd) (return . const Ok) r
r <- liftIO $ try $ renameFile (filename ++ "-crop.pdf") (filename ++ ".pdf")
_ <- either (throw . Exc) (return . const Ok) r
print' "."
when (elem filetype ["png", "jpg", "gif"]) $ do
print' "."
r <- liftIO $ exec verbose convert ["-density", show $ optDensity opts, filename ++ ".pdf",
"-quality", show $ optQuality opts, filename ++ "." ++ filetype]
_ <- either (throw . Err . snd) (return . const Ok) r
return ()
print' " OK\n"
when (optPrint opts) $ do
print tex
return Ok