2019-11-29 11:51:35 +01:00
|
|
|
import qualified Data.ByteString.Char8 as BS (putStrLn, readFile)
|
|
|
|
import qualified Data.Map as Map (toList)
|
2019-09-23 18:00:47 +02:00
|
|
|
import PDF (Document(..), parseDocument)
|
2019-11-29 11:51:35 +01:00
|
|
|
import PDF.Object (Content)
|
|
|
|
import PDF.Pages (Page(..), get, getAll)
|
2019-09-23 18:00:47 +02:00
|
|
|
import PDF.Update (unify)
|
|
|
|
import System.Environment (getArgs)
|
2019-11-29 11:51:35 +01:00
|
|
|
import System.Exit (die)
|
|
|
|
import System.IO (BufferMode(..), hSetBuffering, stdout)
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2019-11-29 11:51:35 +01:00
|
|
|
onDoc :: FilePath -> (Content -> Either String a) -> IO a
|
|
|
|
onDoc inputFile f = do
|
|
|
|
content <- fmap (unify . updates) . parseDocument <$> BS.readFile inputFile
|
|
|
|
case content >>= f of
|
|
|
|
Left someError -> die someError
|
|
|
|
Right value -> return value
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2019-11-29 11:51:35 +01:00
|
|
|
displayPage :: Page -> IO ()
|
|
|
|
displayPage = mapM_ BS.putStrLn . contents
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2019-11-29 11:51:35 +01:00
|
|
|
wholeDoc :: FilePath -> IO ()
|
|
|
|
wholeDoc inputFile = do
|
|
|
|
pages <- onDoc inputFile getAll
|
|
|
|
mapM_ (displayPage . snd) $ Map.toList pages
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2019-11-29 11:51:35 +01:00
|
|
|
singlePage :: FilePath -> Int -> IO ()
|
|
|
|
singlePage inputFile pageNumber =
|
|
|
|
onDoc inputFile (`get` pageNumber) >>= displayPage
|
2019-09-23 18:00:47 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2019-11-29 11:51:35 +01:00
|
|
|
hSetBuffering stdout LineBuffering
|
|
|
|
args <- getArgs
|
|
|
|
case args of
|
|
|
|
[inputFile] -> wholeDoc inputFile
|
|
|
|
[inputFile, pageNumber] -> singlePage inputFile (read pageNumber)
|
|
|
|
_ -> die "Syntax: getText INPUT_FILE [PAGE_NUMBER]"
|