add seperate types module

This commit is contained in:
Marko Andjelic 2026-02-05 03:25:31 +00:00
commit b24288eb70
4 changed files with 76 additions and 47 deletions

View file

@ -14,29 +14,21 @@ where
import Codec.Archive.Zip (Archive, findEntryByPath, fromEntry, toArchive)
import qualified Codec.Epub.Data.Manifest as DMan
import qualified Codec.Epub.Data.Metadata as DMeta
import qualified Codec.Epub.Data.Spine as DSpin
import Codec.Epub.Parse (getManifest, getMetadata, getSpine)
import Control.Monad.Except (runExceptT)
import Control.Monad.Reader (MonadReader (ask), ReaderT, liftIO)
import Control.Monad.Reader (MonadReader (ask), liftIO)
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.Lazy as B
import Data.List (find)
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import System.FilePath (normalise, takeDirectory, (</>))
import Text.HTML.TagSoup (Tag (..), innerText, parseTags)
import Text.HTML.Scalpel (Scraper, ScraperT, anySelector, attr, chroot, chroots, html, innerHTML, scrapeStringLike, text)
import Text.HTML.TagSoup (Tag (..), parseTags, renderTags)
import Control.Applicative ((<|>))
import Types (EpubEnv(..), EpubAction)
type EpubAction a = ReaderT EpubEnv IO a
data EpubEnv = EpubEnv
{ archive :: Archive,
opfXml :: String,
baseDir :: FilePath,
bookPath :: FilePath,
eMetadata :: DMeta.Metadata,
eManifest :: DMan.Manifest
}
openEpub :: FilePath -> IO (Either String EpubEnv)
openEpub path = do

View file

@ -2,28 +2,16 @@
module State where
import Control.Concurrent.Async (Async)
import qualified Data.Text as T
import EpubParser (EpubEnv, getChapterTitle)
import Text.HTML.TagSoup (Tag, renderTags)
import Text.HTML.TagSoup (renderTags)
import Types ( AppState(..), Chapter(chapterTags, chapterTitle), ChapterView(..), EpubEnv)
data ChapterView = ChapterView
{ viewTitle :: T.Text,
viewHtml :: T.Text,
viewIdx :: Int
}
data AppState = AppState
{ cEnv :: EpubEnv,
cIdx :: Int,
zoomlvl :: Double,
cSpine :: [FilePath],
bTitle :: T.Text,
cAllChapters :: [[Tag T.Text]],
activeTask :: Maybe (Async ()),
taskVersion :: Integer,
cTags :: Maybe [Tag T.Text]
}
-- Get an element from a list without crashing
safeGet :: Int -> [a] -> Maybe a
safeGet n xs
| n < 0 = Nothing
| otherwise = case drop n xs of
(x : _) -> Just x
[] -> Nothing
-- loading template
initialState :: EpubEnv -> AppState
@ -42,14 +30,11 @@ initialState env =
-- prepare the data for the UI
prepareView :: AppState -> Int -> Maybe ChapterView
prepareView st index
| null (cAllChapters st) = Nothing
| index < 0 || index >= length (cAllChapters st) = Nothing
| otherwise =
let tags = cAllChapters st !! index
in Just $
ChapterView
{ viewTitle = bTitle st <> " - " <> getChapterTitle tags,
viewHtml = renderTags tags,
viewIdx = index
}
prepareView st index = do
chapter <- safeGet index (cAllChapters st)
return $ ChapterView
{ viewTitle = bTitle st <> " - " <> chapterTitle chapter
, viewHtml = renderTags (chapterTags chapter)
, viewIdx = index
}

51
app/Types.hs Normal file
View file

@ -0,0 +1,51 @@
module Types
( EpubEnv(..),
ChapterView(..),
Chapter(..),
AppState(..),
EpubAction
)
where
import qualified Data.Text as T
import Codec.Archive.Zip (Archive)
import qualified Codec.Epub.Data.Metadata as DMeta
import qualified Codec.Epub.Data.Manifest as DMan
import Text.HTML.TagSoup (Tag)
import Control.Concurrent.Async (Async)
import Control.Monad.Reader (ReaderT)
data EpubEnv = EpubEnv
{ archive :: Archive,
opfXml :: String,
baseDir :: FilePath,
bookPath :: FilePath,
eMetadata :: DMeta.Metadata,
eManifest :: DMan.Manifest
}
data ChapterView = ChapterView
{ viewTitle :: T.Text,
viewHtml :: T.Text,
viewIdx :: Int
}
data Chapter = Chapter
{ chapterTitle :: T.Text,
chapterTags :: [Tag T.Text],
chapterIdx :: Int
}
data AppState = AppState
{ cEnv :: EpubEnv,
cIdx :: Int,
zoomlvl :: Double,
cSpine :: [FilePath],
bTitle :: T.Text,
cAllChapters :: [Chapter],
activeTask :: Maybe (Async ()),
taskVersion :: Integer,
cTags :: Maybe [Tag T.Text]
}
type EpubAction a = ReaderT EpubEnv IO a

View file

@ -54,7 +54,8 @@ executable svitak
other-modules: EpubParser,
UI,
Persistence,
State
State,
Types
build-depends: base ^>=4.21.0.0,
base64-bytestring,