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
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
--
xml_from_contents)
where
+import Data.String.Utils (replace)
import Text.HandsomeSoup (css)
import Text.XML.HXT.Core (
(>>>),
SysConfigList,
XmlTree,
changeAttrValue,
+ changeText,
deep,
getAttrValue,
hasAttrValue,
remove_comment_links
>>>
replace_links_with_spans
+ >>>
+ replace_double_newline_in_pre
is_link :: (ArrowXml a) => a XmlTree XmlTree
is_link =
isElem >>> hasName "a"
+is_pre :: (ArrowXml a) => a XmlTree XmlTree
+is_pre =
+ isElem
+ >>>
+ hasName "pre"
+
+replace_double_newline_in_pre :: (ArrowXml a) => a XmlTree XmlTree
+replace_double_newline_in_pre =
+ processTopDown $ fix_it `when` is_pre
+ where
+ fix_it :: (ArrowXml a) => a XmlTree XmlTree
+ fix_it =
+ changeText $ replace "\n\n" "<br /><br />"
remove_full_story_paragraphs :: (ArrowXml a) => a XmlTree XmlTree
remove_full_story_paragraphs =