aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-01-18 05:56:02 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-01-18 05:56:02 +0000
commit683bbbf4c3935851610969fd565fbe97598d294c (patch)
tree34f02071c8fac0f87bfb23a999bc87b01f7079a6 /spec
parent40e87034470548108b04d9a156760690404b6636 (diff)
downloadthermoprint-683bbbf4c3935851610969fd565fbe97598d294c.tar
thermoprint-683bbbf4c3935851610969fd565fbe97598d294c.tar.gz
thermoprint-683bbbf4c3935851610969fd565fbe97598d294c.tar.bz2
thermoprint-683bbbf4c3935851610969fd565fbe97598d294c.tar.xz
thermoprint-683bbbf4c3935851610969fd565fbe97598d294c.zip
Better attribute parsing
Diffstat (limited to 'spec')
-rw-r--r--spec/src/Thermoprint/Printout/BBCode.hs14
-rw-r--r--spec/src/Thermoprint/Printout/BBCode/Attribute.hs33
-rw-r--r--spec/thermoprint-spec.cabal2
3 files changed, 37 insertions, 12 deletions
diff --git a/spec/src/Thermoprint/Printout/BBCode.hs b/spec/src/Thermoprint/Printout/BBCode.hs
index 1ca1e01..7f09623 100644
--- a/spec/src/Thermoprint/Printout/BBCode.hs
+++ b/spec/src/Thermoprint/Printout/BBCode.hs
@@ -11,14 +11,11 @@ module Thermoprint.Printout.BBCode
11 ) where 11 ) where
12 12
13import Data.Text (Text) 13import Data.Text (Text)
14import qualified Data.Text as T (unpack) 14import Data.Map (Map)
15 15
16import qualified Data.Text.Lazy as Lazy (Text) 16import qualified Data.Text.Lazy as Lazy (Text)
17import qualified Data.Text.Lazy as TL (fromStrict) 17import qualified Data.Text.Lazy as TL (fromStrict)
18 18
19import Data.Map (Map)
20import qualified Data.Map as Map (lookup)
21
22import Data.Sequence (Seq) 19import Data.Sequence (Seq)
23import qualified Data.Sequence as Seq (fromList, singleton) 20import qualified Data.Sequence as Seq (fromList, singleton)
24 21
@@ -32,15 +29,13 @@ import Data.Typeable (Typeable)
32import Data.Bifunctor (bimap, first) 29import Data.Bifunctor (bimap, first)
33import Control.Monad (join) 30import Control.Monad (join)
34 31
35import Text.Read (readMaybe)
36
37import Data.Maybe (fromMaybe)
38
39import Text.BBCode (DomForest, DomTree(..), TreeError(..)) 32import Text.BBCode (DomForest, DomTree(..), TreeError(..))
40import qualified Text.BBCode as Raw (bbcode, BBCodeError(..)) 33import qualified Text.BBCode as Raw (bbcode, BBCodeError(..))
41 34
42import Thermoprint.Printout 35import Thermoprint.Printout
43 36
37import Thermoprint.Printout.BBCode.Attribute
38
44-- ^ We replicate 'Raw.BBCodeError' but add a new failure mode documenting incompatibly of the parsed syntax tree with our document format 39-- ^ We replicate 'Raw.BBCodeError' but add a new failure mode documenting incompatibly of the parsed syntax tree with our document format
45data BBCodeError = LexerError String -- ^ Error while parsing input to stream of tokens 40data BBCodeError = LexerError String -- ^ Error while parsing input to stream of tokens
46 | TreeError TreeError -- ^ Error while parsing stream of tokens to syntax tree 41 | TreeError TreeError -- ^ Error while parsing stream of tokens to syntax tree
@@ -123,9 +118,6 @@ parse :: Monoid a => Context a -> [DomTree] -> Either SemanticError a
123-- @parse ctx = 'mergeResult' ctx . map 'parseDom'@ 118-- @parse ctx = 'mergeResult' ctx . map 'parseDom'@
124parse ctx = mergeResult ctx . map parseDom 119parse ctx = mergeResult ctx . map parseDom
125 120
126lookupAttr :: Read a => CI Text -> a -> Map (CI Text) Text -> a
127lookupAttr t def attrs = fromMaybe def $ Map.lookup t attrs >>= (readMaybe . T.unpack)
128
129asBlock :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Block 121asBlock :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Block
130asBlock "VSpace" [] = Right . VSpace . lookupAttr "height" 1 122asBlock "VSpace" [] = Right . VSpace . lookupAttr "height" 1
131asBlock t _ = const $ Left . UnmappedBlockElement . CI.original $ t 123asBlock t _ = const $ Left . UnmappedBlockElement . CI.original $ t
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
4module Thermoprint.Printout.BBCode.Attribute
5 ( Attribute(..)
6 , lookupAttr
7 ) where
8
9import Data.Text (Text)
10import qualified Data.Text as T (unpack)
11
12import Data.Map (Map)
13import qualified Data.Map as Map (lookup)
14
15import Data.CaseInsensitive (CI)
16import qualified Data.CaseInsensitive as CI
17
18import Text.Read (readMaybe)
19import 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'
24class Attribute a where
25 attrRead :: Text -> Maybe a
26 default attrRead :: Read a => Text -> Maybe a
27 attrRead = readMaybe . T.unpack
28
29instance Attribute Integer
30
31lookupAttr :: Attribute a => CI Text -> a -> Map (CI Text) Text -> a
32-- ^ Extract an attribute by name
33lookupAttr t def attrs = fromMaybe def $ Map.lookup t attrs >>= attrRead
diff --git a/spec/thermoprint-spec.cabal b/spec/thermoprint-spec.cabal
index eb9d6d3..ad861aa 100644
--- a/spec/thermoprint-spec.cabal
+++ b/spec/thermoprint-spec.cabal
@@ -22,7 +22,7 @@ library
22 , Thermoprint.Printout.BBCode 22 , Thermoprint.Printout.BBCode
23 , Thermoprint.Identifiers 23 , Thermoprint.Identifiers
24 , Thermoprint.API 24 , Thermoprint.API
25 -- other-modules: 25 other-modules: Thermoprint.Printout.BBCode.Attribute
26 -- other-extensions: 26 -- other-extensions:
27 extensions: DeriveGeneric 27 extensions: DeriveGeneric
28 , DeriveAnyClass 28 , DeriveAnyClass