Holumbus-Searchengine 1.2.1 → 1.2.2
raw patch · 2 files changed
+31/−8 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Holumbus.Crawler.PdfToText: pdfResource :: MVar (FilePath, FilePath)
+ Holumbus.Crawler.PdfToText: releasePdf :: (FilePath, FilePath) -> IO ()
+ Holumbus.Crawler.PdfToText: requestPdf :: IO (FilePath, FilePath)
Files
Holumbus-Searchengine.cabal view
@@ -1,5 +1,5 @@ name: Holumbus-Searchengine-version: 1.2.1+version: 1.2.2 license: MIT license-file: LICENSE author: Sebastian M. Gauck, Timo B. Huebel, Uwe Schmidt
src/Holumbus/Crawler/PdfToText.hs view
@@ -5,6 +5,7 @@ module Holumbus.Crawler.PdfToText where +import Control.Concurrent.MVar import qualified Control.Exception as CE import qualified Data.ByteString.Lazy as BS@@ -16,12 +17,13 @@ import System.FilePath ( (</>) ) import System.Process ( rawSystem ) import System.Posix.Process ( getProcessID )+import System.IO.Unsafe ( unsafePerformIO ) import Text.XML.HXT.Core -- ------------------------------------------------------------ --- | Converstion of pdf data into plain text. The conversion is done+-- | Conversion of pdf data into plain text. The conversion is done -- by calling an external program @pdftotext@ (contained in linux packages @xpdf@). -- IO is done via the ByteString API. @@ -30,22 +32,18 @@ pdfToTextBS :: BS.ByteString -> IO String pdfToTextBS inp = ( do- td <- getTemporaryDirectory- pid <- getProcessID- let fn1 = fn td pid "pdfToText.pdf"- let fn2 = fn td pid "pdfToText.txt"+ fns@(fn1, fn2) <- requestPdf BS.writeFile fn1 inp _ <- rawSystem "pdftotext" ["-q", "-enc", "UTF-8", fn1, fn2] removeFile fn1 res <- BS.readFile fn2 BS.length res `seq` removeFile fn2+ releasePdf fns return ( fst . utf8ToUnicode . map (toEnum . fromEnum) . BS.unpack $ res ) ) `mycatch` ( const $ return "" ) where- fn d p f = d </> (show p ++ "-" ++ f)- mycatch :: IO a -> (CE.SomeException -> IO a) -> IO a mycatch = CE.catch @@ -55,5 +53,30 @@ arrIO pdfToText >>> perform ( traceString 2 (( "pdfToText result:\n" ++ ) . take 128 . show) )++-- ------------------------------------------------------------++-- The pdftotext call is not thread save++pdfResource :: MVar (FilePath, FilePath)+pdfResource+ = unsafePerformIO $ tmpFiles >>= newMVar+ where+ tmpFiles+ = do td <- getTemporaryDirectory+ pid <- getProcessID+ let fn1 = fn td pid "pdfToText.pdf"+ let fn2 = fn td pid "pdfToText.txt"+ return (fn1, fn2)+ fn d p f+ = d </> (show p ++ "-" ++ f)++{-# NOINLINE pdfResource #-}++requestPdf :: IO (FilePath, FilePath)+requestPdf = takeMVar pdfResource++releasePdf :: (FilePath, FilePath) -> IO ()+releasePdf = putMVar pdfResource -- ------------------------------------------------------------