summaryrefslogtreecommitdiffstats
path: root/src/Zettel.hs
blob: 99c2dd32a8184199862217e907029588521b677e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}

module Zettel where

import Text.Pandoc.Definition (Pandoc (..))

class Combine a where
  cappend :: a -> a -> a
  cempty :: a

newtype UseCombine a = UC a

instance (Monoid a) => Combine (UseCombine a) where
  cappend (UC a) (UC b) = UC $ a <> b
  cempty = UC mempty

deriving via (UseCombine Text) instance Combine Text

deriving via (UseCombine Pandoc) instance Combine Pandoc

newtype ZettelId = ZettelId
  { -- | The ZettelId is just Text, however, it should also be possible to convert
    -- it to a list of the ID split up into it's parts.
    unZettelId :: Text
  }
  deriving (Show, Eq, Ord, IsString, ToString, Semigroup, Monoid)
  deriving (Combine) via (UseCombine ZettelId)

data ZettelMetadata = ZettelMetadata
  { -- | Optional creation date of the Zettel.e
    zettelCreationDate :: Maybe Int,
    -- | Optional last modified date of the Zettel.
    zettelModifiedDate :: Maybe Int
  }
  deriving (Show, Eq)

instance Combine (Maybe a) where
  cappend Nothing a = a
  cappend a _ = a

  cempty = Nothing

instance Combine ZettelMetadata where
  cappend (ZettelMetadata c m) (ZettelMetadata c' m') =
    ZettelMetadata (cappend c c') (cappend m m')

  cempty = ZettelMetadata cempty cempty

data Zettel = Zettel
  { -- | The ID that is assigned to the Zettel.
    zettelId :: !ZettelId,
    -- | The title of the Zettel, which should also be present in the body, however,
    -- this is useful to gather metadata about the Zettel.
    zettelTitle :: !Text,
    -- | The text body of the Zettel, which is stored as a Pandoc document to make it
    -- easy to export to other documents.
    zettelBody :: Pandoc,
    -- | Zettel metadata which is mostly optional.
    zettelMetadata :: ZettelMetadata
  }
  deriving (Show, Eq)

instance Combine Zettel where
  cappend (Zettel a b c d) (Zettel a' b' c' d') =
    Zettel (cappend a a') (cappend b b') (cappend c c') (cappend d d')

  cempty = Zettel cempty cempty cempty cempty