where
import Control.Concurrent.ParallelIO (parallel)
-import qualified Data.Map as Map (lookup)
import Data.Time (getCurrentTime)
import qualified Data.ByteString.Lazy as B (ByteString, hPut)
import Data.String.Utils (split, strip)
-import qualified Data.Map as Map (Map, empty, insert)
+import qualified Data.Map as Map (Map, empty, insert, lookup)
import Data.Maybe (catMaybes, fromJust, isNothing)
import Prelude hiding (readFile)
import System.IO (Handle, hClose, hFlush)
defaultWriterOptions,
readHtml,
writeEPUB,
- writerEPUBMetadata)
+ writerEPUBMetadata,
+ writerUserDataDir)
+import Text.Pandoc.Shared ( readDataFile )
import Text.XML.HXT.Core (
ArrowXml,
IOSArrow,
+-- | Stolen from writeEPUB.
+default_stylesheet :: IO String
+default_stylesheet =
+ -- This comes with Pandoc, I guess.
+ readDataFile (writerUserDataDir defaultWriterOptions) "epub.css"
+
+
+construct_stylesheet :: IO String
+construct_stylesheet = do
+ defaults <- default_stylesheet
+ -- Allow word-wrapping in <pre> elements.
+ let my_additions = "\n" ++ "pre { white-space: pre-wrap; }" ++ "\n"
+ return $ defaults ++ my_additions
+
page_from_url :: Cfg -> URL -> IO (Maybe Page)
page_from_url cfg url = do
contents <- get_article_contents cfg url
change_src_func :: String -> String
change_src_func old_src =
case Map.lookup old_src image_map of
- -- Leave it alone if we don't have the file locally
- Nothing -> old_src
+ -- If we don't have the file, empty the src. Pandoc will crash
+ -- otherwise.
+ Nothing -> ""
Just v -> v
change_src :: (ArrowXml a) => a XmlTree XmlTree
fp_parse xml = do
hl <- parse_headline xml
parsed_articles <- fp_parse_articles xml
- case parsed_articles of
- [] -> return Nothing
- x -> return $ Just $ FullPage (fromJust hl) x
+ return $ case parsed_articles of
+ [] -> Nothing
+ x -> Just $ FullPage (fromJust hl) x
hClose handle
xhtml_to_epub :: String -> String -> IO B.ByteString
-xhtml_to_epub epmd =
- write_epub . read_html
- where
- my_writer_options = defaultWriterOptions { writerEPUBMetadata = epmd }
- write_epub = writeEPUB Nothing [] my_writer_options
- read_html = readHtml defaultParserState
+xhtml_to_epub epmd xhtml = do
+ stylesheet <- construct_stylesheet
+ writeEPUB
+ (Just stylesheet)
+ []
+ my_writer_options
+ (read_html xhtml)
+ where
+ my_writer_options = defaultWriterOptions { writerEPUBMetadata = epmd }
+ read_html = readHtml defaultParserState
--