blob: 60815c82e2039448d6d8b7ca6a34273816b07c72 (
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
|
{-# LANGUAGE DefaultSignatures #-}
-- | Parsing attributes
module Thermoprint.Printout.BBCode.Attribute
( Attribute(..)
, lookupAttr
) where
import Data.Text (Text)
import qualified Data.Text as T (unpack)
import Data.Map (Map)
import qualified Data.Map as Map (lookup)
import Data.CaseInsensitive (CI)
import qualified Data.CaseInsensitive as CI
import Text.Read (readMaybe)
import Data.Maybe (fromMaybe)
-- | We build our own version of 'Read' so we can override the presentation used
--
-- We provide a default implementation for 'Read a => Attribute a'
class Attribute a where
attrRead :: Text -> Maybe a
default attrRead :: Read a => Text -> Maybe a
attrRead = readMaybe . T.unpack
instance Attribute Integer
lookupAttr :: Attribute a => CI Text -> a -> Map (CI Text) Text -> a
-- ^ Extract an attribute by name
lookupAttr t def attrs = fromMaybe def $ Map.lookup t attrs >>= attrRead
|