]> gitweb.michael.orlitzky.com - dead/lwn-epub.git/blobdiff - src/Epublishable.hs
Add a first draft using HXT, HandsomeSoup, and the Haskell epub library.
[dead/lwn-epub.git] / src / Epublishable.hs
diff --git a/src/Epublishable.hs b/src/Epublishable.hs
new file mode 100644 (file)
index 0000000..01d27f9
--- /dev/null
@@ -0,0 +1,37 @@
+module Epublishable
+where
+
+import Codec.EBook
+import qualified Data.ByteString.Lazy as B (writeFile)
+import Data.List (foldl')
+import Data.Tree.NTree.TypeDefs (NTree)
+import System.FilePath (normalise)
+import Text.XML.HXT.Core (IOSArrow, XNode, XmlTree)
+
+import Misc (string_to_bytestring)
+import XHTML
+
+
+class (XHTML a) => Epublishable a where
+  parse :: IOSArrow XmlTree (NTree XNode) -> IO (Maybe a)
+
+  title :: a -> String
+
+  epublish :: a -> FilePath -> Integer -> IO ()
+  epublish obj path time = do
+    let book_name = title obj
+    let book =
+          emptyBook { 
+            bookID = "http://lwn.net/" ++ book_name,
+            bookAuthor = "LWN <http://lwn.net> Copyright Eklektix, Inc.",
+            bookTitle = book_name
+          }
+    let xhtml = to_xhtml obj
+    bs_xhtml <- string_to_bytestring xhtml
+    let iid = "iid-1"
+    let normalized_path = normalise path
+    let metadata = Just (ChapterMetadata book_name)
+    let bi = BookItem iid normalized_path bs_xhtml opsMediatype metadata
+    let bookFull = foldl' addItem2Book book [bi]
+    let outdata = book2Bin bookFull time
+    B.writeFile normalized_path outdata