2020-03-11 18:55:18 +01:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2020-03-04 18:19:10 +01:00
|
|
|
import Control.Monad ((>=>))
|
2020-03-11 18:55:18 +01:00
|
|
|
import Control.Monad.IO.Class (MonadIO, liftIO)
|
|
|
|
import Control.Monad.Reader (runReaderT)
|
2020-02-08 08:15:32 +01:00
|
|
|
import qualified Data.ByteString.Char8 as BS (readFile)
|
2020-03-10 22:57:11 +01:00
|
|
|
import Data.Map (Map, foldlWithKey, mapWithKey)
|
|
|
|
import qualified Data.Map as Map (empty, insert, toList, union)
|
2020-03-11 18:55:18 +01:00
|
|
|
import Data.OrderedMap (mapi)
|
2020-03-10 22:57:11 +01:00
|
|
|
import qualified Data.Text as Text (unpack)
|
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(..))
|
2020-03-10 22:57:11 +01:00
|
|
|
import PDF.Content.Text (Chunks(..))
|
2020-03-04 18:19:10 +01:00
|
|
|
import PDF.Layer (Layer)
|
2020-03-11 18:55:18 +01:00
|
|
|
import PDF.Output (ObjectId(..))
|
|
|
|
import PDF.Pages (
|
|
|
|
Contents(..), FontCache, Page(..), PageNumber(..), Pages(..), Text_(..), cacheFonts, withResources
|
|
|
|
)
|
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)
|
2020-03-10 22:57:11 +01:00
|
|
|
import Text.Printf (printf)
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2020-03-11 18:55:18 +01:00
|
|
|
displayPage :: (MonadIO m, FontCache m) => Page -> m ()
|
2020-03-10 22:57:11 +01:00
|
|
|
displayPage =
|
2020-03-11 18:55:18 +01:00
|
|
|
withResources (r Contents)
|
|
|
|
>=> sequence_ . mapi (\objectId content ->
|
|
|
|
r Chunks content -- >=> sequence_ . mapWithKey (display objectId)
|
|
|
|
)
|
|
|
|
where
|
|
|
|
display = undefined
|
|
|
|
|
|
|
|
{-
|
|
|
|
(
|
|
|
|
r Contents :: ReaderT FontSet m (OrderedMap ObjectId Content)
|
|
|
|
-- >=> sequenceA . mapi $ \(objectId, content) -> undefined
|
|
|
|
)
|
|
|
|
--sequenceA $ mapWithKey (display objectId) (r Chunks content)
|
2020-03-10 22:57:11 +01:00
|
|
|
-- >=> mapM_ Text.putStrLn
|
|
|
|
where
|
2020-03-11 18:55:18 +01:00
|
|
|
display a b v =
|
|
|
|
liftIO . putStrLn $
|
|
|
|
printf "%d@%s: %s" (getObjectId a) (show b) (Text.unpack v)
|
|
|
|
-}
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2020-03-04 18:19:10 +01:00
|
|
|
getAll :: Layer -> IO ()
|
2020-03-11 18:55:18 +01:00
|
|
|
getAll layer =
|
|
|
|
r Pages layer
|
|
|
|
>>= flip runReaderT layer . cacheFonts . 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 ()
|
2020-03-11 18:55:18 +01:00
|
|
|
get n layer = r (P n) layer >>= flip runReaderT layer . cacheFonts . displayPage
|
2020-03-04 18:19:10 +01:00
|
|
|
|
|
|
|
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]"
|