diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-18 05:56:02 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-18 05:56:02 +0000 |
commit | 683bbbf4c3935851610969fd565fbe97598d294c (patch) | |
tree | 34f02071c8fac0f87bfb23a999bc87b01f7079a6 /spec/src/Thermoprint/Printout/BBCode | |
parent | 40e87034470548108b04d9a156760690404b6636 (diff) | |
download | thermoprint-683bbbf4c3935851610969fd565fbe97598d294c.tar thermoprint-683bbbf4c3935851610969fd565fbe97598d294c.tar.gz thermoprint-683bbbf4c3935851610969fd565fbe97598d294c.tar.bz2 thermoprint-683bbbf4c3935851610969fd565fbe97598d294c.tar.xz thermoprint-683bbbf4c3935851610969fd565fbe97598d294c.zip |
Better attribute parsing
Diffstat (limited to 'spec/src/Thermoprint/Printout/BBCode')
-rw-r--r-- | spec/src/Thermoprint/Printout/BBCode/Attribute.hs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/spec/src/Thermoprint/Printout/BBCode/Attribute.hs b/spec/src/Thermoprint/Printout/BBCode/Attribute.hs new file mode 100644 index 0000000..60815c8 --- /dev/null +++ b/spec/src/Thermoprint/Printout/BBCode/Attribute.hs | |||
@@ -0,0 +1,33 @@ | |||
1 | {-# LANGUAGE DefaultSignatures #-} | ||
2 | |||
3 | -- | Parsing attributes | ||
4 | module Thermoprint.Printout.BBCode.Attribute | ||
5 | ( Attribute(..) | ||
6 | , lookupAttr | ||
7 | ) where | ||
8 | |||
9 | import Data.Text (Text) | ||
10 | import qualified Data.Text as T (unpack) | ||
11 | |||
12 | import Data.Map (Map) | ||
13 | import qualified Data.Map as Map (lookup) | ||
14 | |||
15 | import Data.CaseInsensitive (CI) | ||
16 | import qualified Data.CaseInsensitive as CI | ||
17 | |||
18 | import Text.Read (readMaybe) | ||
19 | import Data.Maybe (fromMaybe) | ||
20 | |||
21 | -- | We build our own version of 'Read' so we can override the presentation used | ||
22 | -- | ||
23 | -- We provide a default implementation for 'Read a => Attribute a' | ||
24 | class Attribute a where | ||
25 | attrRead :: Text -> Maybe a | ||
26 | default attrRead :: Read a => Text -> Maybe a | ||
27 | attrRead = readMaybe . T.unpack | ||
28 | |||
29 | instance Attribute Integer | ||
30 | |||
31 | lookupAttr :: Attribute a => CI Text -> a -> Map (CI Text) Text -> a | ||
32 | -- ^ Extract an attribute by name | ||
33 | lookupAttr t def attrs = fromMaybe def $ Map.lookup t attrs >>= attrRead | ||