diff --git a/app/Main.hs b/app/Main.hs index 1408ed1..36fe8de 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,19 +1,14 @@ {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE DataKinds #-} -{-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE DerivingVia #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE DeriveGeneric #-} module Main where -import Data.Text (Text(..)) +import Data.Text (Text) import Data.Aeson -import qualified MyLib (someFunc) import Database.SQLite.Simple -import Database.SQLite.Simple.FromRow (FromRow) -import Database.SQLite.Simple.FromField (fromField) import Database.SQLite.Simple.QQ -import Network.Wai import Network.Wai.Handler.Warp import Deriving.Aeson import Data.Proxy @@ -23,7 +18,10 @@ import Control.Monad.IO.Class data Quote = Quote { qQuote :: Text , qAuthor :: Text - , qBook :: Text + , qTitle :: Text + -- , qPage :: Text + -- , qChapter :: Text + -- , qTime :: UnixTime } deriving (Show, Eq, Ord, Generic) deriving (FromJSON,ToJSON) via CustomJSON '[OmitNothingFields, FieldLabelModifier '[StripPrefix "q", CamelToSnake]] Quote diff --git a/lib/KOReader.hs b/lib/KOReader.hs new file mode 100644 index 0000000..093b058 --- /dev/null +++ b/lib/KOReader.hs @@ -0,0 +1,51 @@ +-- | Module to parse KOReader hightlight file +{-# LANGUAGE DerivingVia #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE OverloadedStrings #-} +module KOReader where + +import Deriving.Aeson +import Data.Aeson +import Data.Text + +showT :: Show a => a -> Text +showT = pack . show + +newtype KoPage = KoPage Text + deriving (Show, Eq, Generic) + +instance FromJSON KoPage where + parseJSON (Number s) = pure (KoPage $ showT $ round s) + parseJSON (String s) = pure (KoPage (showT s)) + parseJSON _ = undefined + +instance ToJSON KoPage where + toJSON (KoPage a) = toJSON a + +data KoHighlight = KoHighlight { khlDocuments :: [KoDocument] + , khlVersion :: Text + -- TODO: UNIX time + , khlCreatedOn :: Integer } + deriving (Show, Eq, Generic) + deriving (FromJSON, ToJSON) + via CustomJSON '[OmitNothingFields, FieldLabelModifier '[StripPrefix "khl", CamelToSnake]] KoHighlight + +data KoDocument = KoDocument { kodTitle :: Text + , kodAuthor :: Maybe Text + , kodFile :: Maybe Text + , kodEntries :: [KoEntry] } + deriving (Show, Eq, Generic) + deriving (FromJSON, ToJSON) + via CustomJSON '[OmitNothingFields, FieldLabelModifier '[StripPrefix "kod", CamelToSnake]] KoDocument + +data KoEntry = KoEntry { koeChapter :: Maybe Text + , koePage :: KoPage + -- TODO: unix time + , koeTime :: Maybe Integer + , koeSort :: Maybe Text + , koeDrawer :: Maybe Text + , koeText :: Text} + deriving (Show, Eq, Generic) + deriving (FromJSON, ToJSON) + via CustomJSON '[OmitNothingFields, FieldLabelModifier '[StripPrefix "koe", CamelToSnake]] KoEntry diff --git a/quotes-api.cabal b/quotes-api.cabal index 688a03b..afbe51c 100644 --- a/quotes-api.cabal +++ b/quotes-api.cabal @@ -59,7 +59,7 @@ library import: warnings -- Modules exported by the library. - exposed-modules: MyLib + exposed-modules: KOReader -- Modules included in this library but not exported. -- other-modules: @@ -68,7 +68,12 @@ library -- other-extensions: -- Other library packages from which modules are imported. - build-depends: base ^>=4.16.3.0 + build-depends: base ^>=4.16.3.0, + text, + aeson, + deriving-aeson, + bytestring, + unordered-containers, -- Directories containing source files. hs-source-dirs: lib