Skip to content

Commit

Permalink
Refactoring for lazy byteString.
Browse files Browse the repository at this point in the history
  • Loading branch information
dixiecko committed Jan 4, 2010
1 parent 490ae0b commit 90c23cf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
22 changes: 13 additions & 9 deletions src/Codec/EBook.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Codec.EBook (
module Codec.EBook.OPF,
module Codec.EBook.OCF,
book2Str,
book2Arch
)
where

Expand All @@ -15,12 +16,15 @@ import Codec.EBook.OCF
import qualified Data.ByteString.Lazy as B

book2Str :: Book -> B.ByteString
book2Str book = let ncxXMLFile = ("book.ncx",ncxXML book)
opfXMLFile = ("book.opf",opfXML book)
conXMLFile = containerXMLFile' "book.opf"
mimeFile = mimetypeFile
contFiles = bookFiles book
allFiles = mimeFile:ncxXMLFile:conXMLFile:opfXMLFile:contFiles
entries = map (\(n,c) -> toEntry n 0 c) allFiles
arch = foldl (\a e -> addEntryToArchive e a) emptyArchive entries
in fromArchive arch
book2Str book = fromArchive (book2Arch book)

book2Arch :: Book -> Archive
book2Arch book = let ncxXMLFile = ("book.ncx",ncxXML book)
opfXMLFile = ("book.opf",opfXML book)
conXMLFile = containerXMLFile' "book.opf"
mimeFile = mimetypeFile
contFiles = bookFiles book
allFiles = mimeFile:ncxXMLFile:conXMLFile:opfXMLFile:contFiles
entries = map (\(n,c) -> toEntry n 10000000 c) allFiles
arch = foldl (\a e -> addEntryToArchive e a) emptyArchive entries
in arch
6 changes: 3 additions & 3 deletions src/Codec/EBook/OCF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ module Codec.EBook.OCF(
)
where

import Codec.EBook.Types
import Text.XML.Light
import Codec.Archive.Zip
import Codec.Binary.UTF8.String as U
import qualified Data.ByteString.Lazy as B

defaultMediatype = "application/oebps-package+xml"
Expand All @@ -21,10 +21,10 @@ containerXMLFile' :: FilePath -> (FilePath, B.ByteString)
containerXMLFile' p = containerXMLFile p defaultMediatype

mimetypeFile :: (FilePath, B.ByteString)
mimetypeFile = ("mimetype", B.pack $ U.encode $ defaultMimetype)
mimetypeFile = ("mimetype", str2bstr defaultMimetype)

containerXMLFile :: FilePath -> String -> (FilePath, B.ByteString)
containerXMLFile p m = ("META-INF/container.xml", B.pack $ U.encode $ ppTopElement contTag)
containerXMLFile p m = ("META-INF/container.xml", str2bstr $ ppTopElement contTag)
where
contTag = add_attrs contAttrs $ unode "container" rootfilesTag
contAttrs = [ (Attr (unqual "version") "1.0")
Expand Down
6 changes: 2 additions & 4 deletions src/Codec/EBook/OPF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import Data.Maybe (fromJust)
import Text.XML.Light
import Codec.EBook.Types
import qualified Data.ByteString.Lazy as B
-- import Data.ByteString.Lazy.UTF8 as U
import Codec.Binary.UTF8.String as U

opfDefaultLang = "en"
opfDefaultCreator = "Unknown"

ncxXML :: Book -> B.ByteString
ncxXML o = B.pack $ U.encode $ ppTopElement packageT
ncxXML o = str2bstr $ ppTopElement packageT
where
packageT= add_attrs packageA $ unode "ncx" nestedT
packageA = [ (Attr (unqual "version") "2005-1")
Expand All @@ -40,7 +38,7 @@ ncxXML o = B.pack $ U.encode $ ppTopElement packageT
]

opfXML :: Book -> B.ByteString
opfXML o = B.pack $ U.encode $ ppTopElement packageT
opfXML o = str2bstr $ ppTopElement packageT
where
packageT= add_attrs packageA $ unode "package" nestedT
packageA = [ (Attr (unqual "version") "2.0")
Expand Down
7 changes: 6 additions & 1 deletion src/Codec/EBook/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ module Codec.EBook.Types (
emptyBook,
addItem2Book,
bookFiles,
chapterItems
chapterItems,
str2bstr
)
where
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.Char8 as U

data Book = Book {
bookID :: String,
Expand All @@ -32,6 +34,9 @@ data Metadata = ChapterMetadata {
chapterTitle :: String
} deriving (Show, Eq, Ord)

str2bstr :: String -> B.ByteString
str2bstr x = U.pack x

emptyBook :: Book
emptyBook = Book "NO ID" "NO TITLE" "NO AUTHOR" "en" []

Expand Down

0 comments on commit 90c23cf

Please sign in to comment.