packages feed

wordchoice-0.1.1.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 whereimport Text.Pandocimport Filesystem.Path.CurrentOS as Fimport qualified Data.Text as Timport qualified Data.Text.IO as TIOimport qualified Data.ByteString.Lazy as BSLimport System.FilePath.GlobeitherError = either (error . show) id-- | Process files given a filename globglobFile :: String -> IO T.TextglobFile str = do    files <- glob str    fmap T.concat . sequence $ fmap processFile files-- | Process a file given a filename. Return text only, discarding superflouous material.processFile :: String -> IO T.TextprocessFile filepath = T.filter goodChar <$> case (extension . decodeString $ filepath) of    (Just "md") -> T.pack . writePlain def . filterCode . eitherError . readMarkdown def . T.unpack <$> TIO.readFile filepath    (Just "dbk") -> T.pack . writePlain def . filterCode . eitherError . readDocBook def . T.unpack <$> TIO.readFile filepath    (Just "docx") -> T.pack . writePlain def . filterCode . fst . eitherError . readDocx def <$> BSL.readFile filepath    (Just "epub") -> T.pack . writePlain def . filterCode . fst . eitherError . readEPUB def <$> BSL.readFile filepath    (Just "html") -> T.pack . writePlain def . filterCode . eitherError . readHtml def . T.unpack <$> TIO.readFile filepath    (Just "tex") -> T.pack . writePlain def . filterCode . eitherError . readLaTeX def . T.unpack <$> TIO.readFile filepath    (Just "xml") -> T.pack . writePlain def . filterCode . eitherError . readOPML def . T.unpack <$> TIO.readFile filepath    (Just "odt") -> T.pack . writePlain def . filterCode . fst . eitherError . readOdt def <$> BSL.readFile filepath    (Just "rst") -> T.pack . writePlain def . filterCode . eitherError . readRST def . T.unpack <$> TIO.readFile filepath    (Just "textile") -> T.pack . writePlain def . filterCode . eitherError . readTextile def . T.unpack <$> TIO.readFile filepath    _ -> TIO.readFile filepathgoodChar :: Char -> BoolgoodChar = not . flip any (".,?_()![]{}*&$#" :: String) . (==)-- | Filter out code and tables from the documentfilterCode :: Pandoc -> PandocfilterCode (Pandoc meta content) = Pandoc meta $ filter rightBlock content    where rightBlock CodeBlock { } = False          rightBlock Table { }     = False          rightBlock _             = True