summaryrefslogtreecommitdiff
path: root/ymir/mlmmj-expose/FoxReplace.hs
blob: ca28fa43880d18e8a274eeff2e948443083483cb (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
{-# LANGUAGE RecordWildCards, OverloadedStrings #-}

module FoxReplace where

import Data.Aeson

import Data.Set (Set)
import qualified Data.Set as Set


newtype FoxReplace = FoxReplace (Set FoxReplaceGroup)
  deriving (Ord, Eq, Show)

data FoxReplaceGroup = FoxReplaceGroup
                       { groupName :: String
                       , groupUrls :: Set String
                       , groupSubs :: Set FoxReplaceSub
                       , groupHtmlMode  :: FoxReplaceHTML
                       }
  deriving (Ord, Eq, Show)

data FoxReplaceHTML = NoHTML | OutputOnlyHTML | BothHTML
  deriving (Ord, Eq, Enum, Show)

data FoxReplaceSub = FoxReplaceSub
                     { rInput, rOutput :: String
                     , rInputType :: SubInput
                     , rCaseSensitive :: Bool
                     }
  deriving (Ord, Eq, Show)

data SubInput = TextInput | WordInput | RegexpInput
  deriving (Ord, Eq, Enum, Show)


instance ToJSON FoxReplace where
  toJSON (FoxReplace groupSet) = object
                                 [ "version" .= ("0.15" :: String)
                                 , "groups" .= groupSet
                                 ]

instance ToJSON FoxReplaceGroup where
  toJSON FoxReplaceGroup{..} = object
                               [ "name" .= groupName
                               , "html" .= groupHtmlMode
                               , "enabled" .= True
                               , "urls" .= groupUrls
                               , "substitutions" .= groupSubs
                               ]

instance ToJSON FoxReplaceHTML where
  toJSON NoHTML         = String "none"
  toJSON OutputOnlyHTML = String "output"
  toJSON BothHTML       = String "inputoutput"

instance ToJSON FoxReplaceSub where
  toJSON FoxReplaceSub{..} = object
                             [ "input" .= rInput
                             , "output" .= rOutput
                             , "inputType" .= rInputType
                             , "caseSensitive" .= rCaseSensitive
                             ]

instance ToJSON SubInput where
  toJSON TextInput   = String "text"
  toJSON WordInput   = String "wholewords"
  toJSON RegexpInput = String "regexp"