{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE DeriveGeneric #-} -- | Use 'Text.BBCode' to parse BBCode module Thermoprint.Printout.BBCode ( bbcode , BBCodeError(..) , TreeError(..) , SemanticError(..) ) 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.Sequence (Seq) import qualified Data.Sequence as Seq () import Data.CaseInsensitive (CI) import qualified Data.CaseInsensitive as CI import GHC.Generics (Generic) import Control.Exception (Exception) import Data.Typeable (Typeable) import Data.Bifunctor (bimap, first) import Control.Monad (join) import Text.Read (readMaybe) import Data.Maybe (fromMaybe) import Text.BBCode (DomTree(..), TreeError(..)) import qualified Text.BBCode as Raw (bbcode, BBCodeError(..)) import Thermoprint.Printout -- ^ We replicate 'Raw.BBCodeError' but add a new failure mode documenting incompatibly of the parsed syntax tree with our document format data BBCodeError = LexerError String -- ^ Error while parsing input to stream of tokens | TreeError TreeError -- ^ Error while parsing stream of tokens to syntax tree | SemanticError SemanticError -- ^ Error while mapping syntax tree to document format deriving (Show, Eq, Generic, Typeable) instance Exception BBCodeError -- ^ An error ocurred while parsing the DOM-Forest (`['DomTree']`) data SemanticError = UnmappedTag Text -- ^ An `Element` does not map to any structure in the context it occurred in deriving (Show, Eq, Generic, Typeable) instance Exception SemanticError bbcode :: Text -> Either BBCodeError Printout -- ^ Parse BBCode bbcode = join . fmap (first SemanticError) . bimap morph' morph . Raw.bbcode morph' :: Raw.BBCodeError -> BBCodeError morph' (Raw.LexerError x) = LexerError x morph' (Raw.TreeError x) = TreeError x morph :: [DomTree] -> Either SemanticError Printout morph = undefined asBlock :: CI Text -> Map (CI Text) Text -> Either SemanticError Block asBlock "VSpace" = Right . VSpace . lookupAttr "size" 1 asBlock t = Left . const (UnmappedTag . CI.original $ t) asLine :: CI Text -> Map (CI Text) Text -> Either SemanticError Line asLine "HSpace" = Right . HSpace . lookupAttr "size" 1 asLine t = Left . const (UnmappedTag . CI.original $ t) lookupAttr :: Read a => CI Text -> a -> Map (CI Text) Text -> a lookupAttr t def attrs = fromMaybe def $ Map.lookup t attrs >>= (readMaybe . T.unpack)