diff options
Diffstat (limited to 'spec/src')
-rw-r--r-- | spec/src/Thermoprint/Printout/BBCode.hs | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/spec/src/Thermoprint/Printout/BBCode.hs b/spec/src/Thermoprint/Printout/BBCode.hs index ca69efc..f80f780 100644 --- a/spec/src/Thermoprint/Printout/BBCode.hs +++ b/spec/src/Thermoprint/Printout/BBCode.hs | |||
@@ -9,7 +9,13 @@ module Thermoprint.Printout.BBCode | |||
9 | ) where | 9 | ) where |
10 | 10 | ||
11 | import Data.Text (Text) | 11 | import Data.Text (Text) |
12 | import qualified Data.Text as T () | 12 | import qualified Data.Text as T (unpack) |
13 | |||
14 | import Data.Map (Map) | ||
15 | import qualified Data.Map as Map (lookup) | ||
16 | |||
17 | import Data.CaseInsensitive (CI) | ||
18 | import qualified Data.CaseInsensitive as CI | ||
13 | 19 | ||
14 | import GHC.Generics (Generic) | 20 | import GHC.Generics (Generic) |
15 | import Control.Exception (Exception) | 21 | import Control.Exception (Exception) |
@@ -18,6 +24,10 @@ import Data.Typeable (Typeable) | |||
18 | import Data.Bifunctor (bimap, first) | 24 | import Data.Bifunctor (bimap, first) |
19 | import Control.Monad (join) | 25 | import Control.Monad (join) |
20 | 26 | ||
27 | import Text.Read (readMaybe) | ||
28 | |||
29 | import Data.Maybe (fromMaybe) | ||
30 | |||
21 | import Text.BBCode (DomTree(..), TreeError(..)) | 31 | import Text.BBCode (DomTree(..), TreeError(..)) |
22 | import qualified Text.BBCode as Raw (bbcode, BBCodeError(..)) | 32 | import qualified Text.BBCode as Raw (bbcode, BBCodeError(..)) |
23 | 33 | ||
@@ -31,7 +41,8 @@ data BBCodeError = LexerError String -- ^ Error while parsing input to stream of | |||
31 | 41 | ||
32 | instance Exception BBCodeError | 42 | instance Exception BBCodeError |
33 | 43 | ||
34 | data SemanticError = Placeholder | 44 | data SemanticError = BlockTagInLineContext Text |
45 | | UnmappedTag Text | ||
35 | deriving (Show, Eq, Generic, Typeable) | 46 | deriving (Show, Eq, Generic, Typeable) |
36 | 47 | ||
37 | instance Exception SemanticError | 48 | instance Exception SemanticError |
@@ -45,3 +56,14 @@ morph' (Raw.TreeError x) = TreeError x | |||
45 | 56 | ||
46 | morph :: [DomTree] -> Either SemanticError Printout | 57 | morph :: [DomTree] -> Either SemanticError Printout |
47 | morph = undefined | 58 | morph = undefined |
59 | |||
60 | asBlock :: CI Text -> Map (CI Text) Text -> Either SemanticError Block | ||
61 | asBlock "VSpace" = Right . VSpace . lookupAttr "size" 1 | ||
62 | asBlock t = Left . const (UnmappedTag . CI.original $ t) | ||
63 | |||
64 | asLine :: CI Text -> Map (CI Text) Text -> Either SemanticError Line | ||
65 | asLine "HSpace" = Right . HSpace . lookupAttr "size" 1 | ||
66 | asLine t = Left . const (UnmappedTag . CI.original $ t) | ||
67 | |||
68 | lookupAttr :: Read a => CI Text -> a -> Map (CI Text) Text -> a | ||
69 | lookupAttr t def attrs = fromMaybe def $ Map.lookup t attrs >>= (readMaybe . T.unpack) | ||