]> gitweb.michael.orlitzky.com - dead/lwn-epub.git/blob - src/Epublishable.hs
3ca9b68b57babd9c6fa22e24defe059afdcca814
[dead/lwn-epub.git] / src / Epublishable.hs
1 module Epublishable
2 where
3
4 import Text.Pandoc
5 import qualified Data.ByteString.Lazy as B (ByteString, writeFile)
6 import Data.Time (getCurrentTime)
7 import Data.Tree.NTree.TypeDefs (NTree)
8 import System.FilePath (normalise)
9 import Text.XML.HXT.Core (IOSArrow, XNode, XmlTree)
10
11 import XHTML
12
13
14 class (XHTML a) => Epublishable a where
15 parse :: IOSArrow XmlTree (NTree XNode) -> IO (Maybe a)
16
17 title :: a -> String
18
19 metadata :: a -> IO String
20 metadata obj = do
21 date <- getCurrentTime
22 return $
23 "<dc:creator>http://lwn.net/</dc:creator>\n" ++
24 "<dc:date>" ++ (show date) ++ "</dc:date>\n" ++
25 "<dc:language>en-US</dc:language>\n" ++
26 "<dc:rights>Copyright Eklektix, Inc.</dc:rights>\n" ++
27 "<dc:title>" ++ (title obj) ++ "</dc:title>\n"
28
29 epublish :: a -> FilePath -> IO ()
30 epublish obj path = do
31 let xhtml = to_xhtml obj
32 epmd <- metadata obj
33 epub <- xhtml_to_epub epmd xhtml
34 let normalized_path = normalise path
35 B.writeFile normalized_path epub
36
37 xhtml_to_epub :: String -> String -> IO B.ByteString
38 xhtml_to_epub epmd =
39 write_epub . read_html
40 where
41 my_writer_options = defaultWriterOptions { writerEPUBMetadata = epmd }
42 write_epub = writeEPUB Nothing [] my_writer_options
43 read_html = readHtml defaultParserState