wordchoice-0.1.0.0: src/Data/Text/WordCount/FileRead.hs
-- | Module with function to read file in with pandoc and discard everything superfluous.
module Data.Text.WordCount.FileRead where
import Text.Pandoc
import Filesystem.Path.CurrentOS as F
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TLIO
import qualified Data.ByteString.Lazy as BSL
eitherError = either (error . show) id
-- | Process a file given its filename. Return text only, discarding superflouous material.
processFile :: String -> IO TL.Text
processFile filepath = case (extension . decodeString $ filepath) of
(Just "md") -> TL.pack . writePlain def . filterCode . eitherError . readMarkdown def . TL.unpack <$> TLIO.readFile filepath
(Just "dbk") -> TL.pack . writePlain def . filterCode . eitherError . readDocBook def . TL.unpack <$> TLIO.readFile filepath
(Just "docx") -> TL.pack . writePlain def . filterCode . fst . eitherError . readDocx def <$> BSL.readFile filepath
(Just "epub") -> TL.pack . writePlain def . filterCode . fst . eitherError . readEPUB def <$> BSL.readFile filepath
(Just "html") -> TL.pack . writePlain def . filterCode . eitherError . readHtml def . TL.unpack <$> TLIO.readFile filepath
(Just "tex") -> TL.pack . writePlain def . filterCode . eitherError . readLaTeX def . TL.unpack <$> TLIO.readFile filepath
(Just "xml") -> TL.pack . writePlain def . filterCode . eitherError . readOPML def . TL.unpack <$> TLIO.readFile filepath
(Just "odt") -> TL.pack . writePlain def . filterCode . fst . eitherError . readOdt def <$> BSL.readFile filepath
(Just "rst") -> TL.pack . writePlain def . filterCode . eitherError . readRST def . TL.unpack <$> TLIO.readFile filepath
(Just "textile") -> TL.pack . writePlain def . filterCode . eitherError . readTextile def . TL.unpack <$> TLIO.readFile filepath
_ -> TLIO.readFile filepath
-- | Filter out code and tables from the document
filterCode :: Pandoc -> Pandoc
filterCode (Pandoc meta content) = Pandoc meta $ filter rightBlock content
where rightBlock CodeBlock { } = False
rightBlock Table { } = False
rightBlock _ = True