{-# LANGUAGE OverloadedStrings #-} -- | Manual dump tool (not automated tests). Opens a real EPUB, prints the -- resolved chapter list (rendering each on demand to show content sizes) and -- the TOC tree: -- -- > cabal run svitak-test -- path/to/book.epub module Main (main) where import qualified Data.Text as T import EpubParser (openEpub) import System.Environment (getArgs) import System.Exit (exitFailure) import Types ( BookInfo (..), BookStructure (..), Chapter (..), ChapterIndex (..), ChapterRef (..), InternalPath (..), TocEntry (..), ) main :: IO () main = do args <- getArgs case args of [path] -> run path [path, n] -> dumpOne path (read n) _ -> putStrLn "usage: svitak-test [chapter-index]" >> exitFailure -- | Print the rendered HTML of a single chapter (for debugging image inlining). dumpOne :: FilePath -> Int -> IO () dumpOne path n = do Right env <- openEpub path Right (BookStructure refs _) <- loadStructure env Right ch <- renderChapter env (refs !! n) putStrLn (T.unpack (T.take 800 (chapterHtml ch))) run :: FilePath -> IO () run path = do opened <- openEpub path case opened of Left err -> putStrLn ("open failed: " ++ err) >> exitFailure Right env -> do putStrLn ("Title: " ++ T.unpack (bookTitle env)) putStrLn ("Authors: " ++ show (map T.unpack (bookAuthors env))) structure <- loadStructure env case structure of Left err -> putStrLn ("structure failed: " ++ err) >> exitFailure Right (BookStructure refs toc) -> do putStrLn ("\n== Chapters (" ++ show (length refs) ++ ") ==") mapM_ (dumpChapter env) refs putStrLn "\n== TOC tree ==" mapM_ (dumpToc 0) toc dumpChapter :: (BookInfo a) => a -> ChapterRef -> IO () dumpChapter env ref = do rendered <- renderChapter env ref let size = case rendered of Left err -> "ERR " ++ err Right c -> show (T.length (chapterHtml c)) ++ " chars" putStrLn (" [" ++ show idx ++ "] " ++ T.unpack (refTitle ref) ++ " (" ++ path ++ ", " ++ size ++ ")") where ChapterIndex idx = refIndex ref InternalPath path = refPath ref dumpToc :: Int -> TocEntry -> IO () dumpToc depth t = do putStrLn (replicate (depth * 2) ' ' ++ "- " ++ T.unpack (tocLabel t) ++ " -> " ++ target ++ frag) mapM_ (dumpToc (depth + 1)) (tocChildren t) where InternalPath target = tocTarget t frag = if T.null (tocFragment t) then "" else "#" ++ T.unpack (tocFragment t)