2020-03-04 18:19:10 +01:00
|
|
|
import Control.Monad ((>=>))
|
2020-02-08 08:15:32 +01:00
|
|
|
import qualified Data.ByteString.Char8 as BS (readFile)
|
2019-11-29 11:51:35 +01:00
|
|
|
import qualified Data.Map as Map (toList)
|
2020-02-08 08:15:32 +01:00
|
|
|
import qualified Data.Text.IO as Text (putStrLn)
|
2020-03-04 18:19:10 +01:00
|
|
|
import PDF (UnifiedLayers(..), parseDocument)
|
|
|
|
import PDF.Box (Box(..))
|
|
|
|
import PDF.Layer (Layer)
|
|
|
|
import PDF.Pages (Page(..), PageNumber(..), Pages(..), cacheFonts)
|
2019-09-23 18:00:47 +02:00
|
|
|
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
|
|
|
displayPage :: Page -> IO ()
|
2020-02-08 08:15:32 +01:00
|
|
|
displayPage = mapM_ Text.putStrLn . contents
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2020-03-04 18:19:10 +01:00
|
|
|
getAll :: Layer -> IO ()
|
|
|
|
getAll layer = (cacheFonts $ r Pages layer) >>= mapM_ (displayPage . snd) . Map.toList
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2020-03-04 18:19:10 +01:00
|
|
|
get :: Int -> Layer -> IO ()
|
|
|
|
get n layer = (cacheFonts $ r (P n) layer) >>= displayPage
|
|
|
|
|
|
|
|
onDoc :: FilePath -> (Layer -> IO ()) -> IO ()
|
|
|
|
onDoc inputFile f = do
|
|
|
|
(parseDocument <$> BS.readFile inputFile)
|
|
|
|
>>= either die (r UnifiedLayers >=> f)
|
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
|
2020-03-04 18:19:10 +01:00
|
|
|
[inputFile] -> onDoc inputFile getAll
|
|
|
|
[inputFile, pageNumber] -> onDoc inputFile (get $ read pageNumber)
|
2019-11-29 11:51:35 +01:00
|
|
|
_ -> die "Syntax: getText INPUT_FILE [PAGE_NUMBER]"
|