aboutsummaryrefslogtreecommitdiff
path: root/tp-bbcode/src/Thermoprint
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-01-18 12:09:36 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-01-18 12:09:36 +0000
commit57c56564d15cd5c83a4f1d1bab5490e6b75e8656 (patch)
tree55ea741665155b46b9e599b149dee3323fe479c0 /tp-bbcode/src/Thermoprint
parent9435083465a487553b21c599c1340aa5e5ed8a1c (diff)
downloadthermoprint-57c56564d15cd5c83a4f1d1bab5490e6b75e8656.tar
thermoprint-57c56564d15cd5c83a4f1d1bab5490e6b75e8656.tar.gz
thermoprint-57c56564d15cd5c83a4f1d1bab5490e6b75e8656.tar.bz2
thermoprint-57c56564d15cd5c83a4f1d1bab5490e6b75e8656.tar.xz
thermoprint-57c56564d15cd5c83a4f1d1bab5490e6b75e8656.zip
Moved Printout.BBCode to own module
Diffstat (limited to 'tp-bbcode/src/Thermoprint')
-rw-r--r--tp-bbcode/src/Thermoprint/Printout/BBCode.hs145
-rw-r--r--tp-bbcode/src/Thermoprint/Printout/BBCode/Attribute.hs39
2 files changed, 184 insertions, 0 deletions
diff --git a/tp-bbcode/src/Thermoprint/Printout/BBCode.hs b/tp-bbcode/src/Thermoprint/Printout/BBCode.hs
new file mode 100644
index 0000000..ce2aa43
--- /dev/null
+++ b/tp-bbcode/src/Thermoprint/Printout/BBCode.hs
@@ -0,0 +1,145 @@
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE GADTs #-}
4
5-- | Use 'Text.BBCode' to parse BBCode
6module Thermoprint.Printout.BBCode
7 ( bbcode
8 , BBCodeError(..)
9 , TreeError(..)
10 , SemanticError(..)
11 ) where
12
13import Data.Text (Text)
14import Data.Map (Map)
15
16import qualified Data.Text.Lazy as Lazy (Text)
17import qualified Data.Text.Lazy as TL (fromStrict)
18
19import Data.Sequence (Seq)
20import qualified Data.Sequence as Seq (fromList, singleton)
21
22import Data.CaseInsensitive (CI)
23import qualified Data.CaseInsensitive as CI
24
25import GHC.Generics (Generic)
26import Control.Exception (Exception)
27import Data.Typeable (Typeable)
28
29import Data.Bifunctor (bimap, first)
30import Control.Monad (join)
31
32import Data.List (groupBy)
33
34import Text.BBCode (DomForest, DomTree(..), TreeError(..))
35import qualified Text.BBCode as Raw (bbcode, BBCodeError(..))
36
37import Thermoprint.Printout
38
39import Thermoprint.Printout.BBCode.Attribute
40
41-- ^ We replicate 'Raw.BBCodeError' but add a new failure mode documenting incompatibly of the parsed syntax tree with our document format
42data BBCodeError = LexerError String -- ^ Error while parsing input to stream of tokens
43 | TreeError TreeError -- ^ Error while parsing stream of tokens to syntax tree
44 | SemanticError SemanticError -- ^ Error while mapping syntax tree to document format
45 deriving (Show, Eq, Generic, Typeable)
46
47instance Exception BBCodeError
48
49morph' :: Raw.BBCodeError -> BBCodeError
50-- ^ Transform 'Raw.BBCodeError' to 'BBCodeError'
51morph' (Raw.LexerError x) = LexerError x
52morph' (Raw.TreeError x) = TreeError x
53
54-- | An error ocurred while parsing the DOM-Forest (`['DomTree']`)
55data SemanticError = BlockInLineContext -- ^ A 'Block' structure was encountered when a 'Line' was expected
56 | LineInBlockContext -- ^ A 'Line' structure was encountered when a 'Block' was expected
57 | UnmappedBlockElement Text -- ^ We encountered an 'Element' that, in a 'Block' context, does not map to any structure
58 | UnmappedLineElement Text -- ^ We encountered an 'Element' that, in a 'Line' context, does not map to any structure
59 deriving (Show, Eq, Generic, Typeable)
60
61instance Exception SemanticError
62
63-- | Result of parsing a single 'DomTree'
64data ParseResult = RBlock Block -- ^ Parses only as 'Block'
65 | RLine Line -- ^ Parses only as 'Line'
66 | RAmbiguous Block Line -- ^ Parses as either 'Block' or 'Line' depending on context
67 | RNoParse SemanticError SemanticError -- ^ Does not parse as either 'Block' or 'Line'
68 deriving (Show)
69
70-- | Current parser context
71data Context a where
72 BlockCtx :: Context Block
73 LineCtx :: Context Line
74
75extract :: Context a -> ParseResult -> Either SemanticError a
76-- ^ Extract information from a 'ParseResult' given 'Context'
77extract BlockCtx (RBlock b) = Right b
78extract LineCtx (RLine l) = Right l
79extract BlockCtx (RAmbiguous b _) = Right b
80extract LineCtx (RAmbiguous _ l) = Right l
81extract BlockCtx (RNoParse bErr _) = Left bErr
82extract LineCtx (RNoParse _ lErr) = Left lErr
83extract BlockCtx _ = Left LineInBlockContext
84extract LineCtx _ = Left BlockInLineContext
85
86hasBlockCtx :: ParseResult -> Bool
87-- ^ Result can be 'extract'ed in a 'Block' 'Context'
88hasBlockCtx (RLine _) = False
89hasBlockCtx _ = True
90
91hasLineCtx :: ParseResult -> Bool
92-- ^ Result can be 'extract'ed in a 'Line' 'Context'
93hasLineCtx (RBlock _) = False
94hasLineCtx _ = True
95
96bbcode :: Text -> Either BBCodeError Printout
97-- ^ Parse BBCode
98bbcode = join . fmap (first SemanticError) . bimap morph' morph . Raw.bbcode
99
100morph :: DomForest -> Either SemanticError Printout
101-- ^ Parse a list of paragraphs
102--
103-- Since we permit only cooked input via 'Raw' 'Paragraph' is identical to 'Block'
104morph = fmap Seq.fromList . mapM (\t -> Seq.singleton . Cooked <$> parse BlockCtx t)
105
106parseDom :: DomTree -> ParseResult
107-- ^ Invoke 'asLine' and 'asBlock' to parse a single 'DomTree'
108parseDom (Content t) = either RBlock (\l -> RAmbiguous (Line l) l) . text . TL.fromStrict $ t
109parseDom (Element t attrs cs)
110 | Right blockParse' <- blockParse
111 , Right lineParse' <- lineParse = RAmbiguous blockParse' lineParse'
112 | Right blockParse' <- blockParse = RBlock blockParse'
113 | Right lineParse' <- lineParse = RLine lineParse'
114 | Left bErr <- blockParse
115 , Left lErr <- lineParse = RNoParse bErr lErr
116 where
117 blockParse = asBlock t cs attrs
118 lineParse = asLine t cs attrs
119
120mergeResult :: Monoid a => Context a -> [ParseResult] -> Either SemanticError a
121-- ^ Merge a list of 'ParseResults' in a certain 'Context'
122mergeResult _ [] = Right mempty
123mergeResult ctx (amb@(RAmbiguous _ _):xs) = mappend <$> extract ctx amb <*> mergeResult ctx xs
124mergeResult ctx (err@(RNoParse _ _):_) = extract ctx err
125mergeResult ctx (x:xs) = mappend <$> extract ctx x <*> mergeResult ctx xs
126
127parse :: Monoid a => Context a -> [DomTree] -> Either SemanticError a
128-- ^ Parse a list of 'DomTree's in a certain 'Context'
129--
130-- @parse ctx = 'mergeResult' ctx . map 'parseDom'@
131parse BlockCtx = fmap mconcat . mapM mergeResult' . groupBy sameCtx . map parseDom
132 where
133 sameCtx a b = (hasLineCtx a && hasLineCtx b) || (hasBlockCtx a && hasBlockCtx b)
134 mergeResult' xs
135 | hasLineCtx `all` xs = Line <$> mergeResult LineCtx xs
136 | otherwise = mergeResult BlockCtx xs
137parse ctx = mergeResult ctx . map parseDom
138
139asBlock :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Block
140asBlock "VSpace" _ = Right . VSpace . lookupAttr "height" True 1
141asBlock t _ = const $ Left . UnmappedBlockElement . CI.original $ t
142
143asLine :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Line
144asLine "HSpace" _ = Right . HSpace . lookupAttr "width" True 1
145asLine t _ = const $ Left . UnmappedLineElement . CI.original $ t
diff --git a/tp-bbcode/src/Thermoprint/Printout/BBCode/Attribute.hs b/tp-bbcode/src/Thermoprint/Printout/BBCode/Attribute.hs
new file mode 100644
index 0000000..538cca2
--- /dev/null
+++ b/tp-bbcode/src/Thermoprint/Printout/BBCode/Attribute.hs
@@ -0,0 +1,39 @@
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, empty)
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
21import Control.Applicative (Alternative(..))
22
23-- | We build our own version of 'Read' so we can override the presentation used
24--
25-- We provide a default implementation for 'Read a => Attribute a'
26class Attribute a where
27 attrRead :: Text -> Maybe a
28 default attrRead :: Read a => Text -> Maybe a
29 attrRead = readMaybe . T.unpack
30
31instance Attribute Integer
32
33lookupAttr :: Attribute a => CI Text -> Bool -> a -> Map (CI Text) Text -> a
34-- ^ Extract an attribute by name -- the 'Bool' attribute specifies whether we additionally accept the empty string as key
35lookupAttr t emptyOk def attrs = fromMaybe def $ (emptyOk' $ Map.lookup t attrs) >>= attrRead
36 where
37 emptyOk'
38 | emptyOk = (<|> Map.lookup (CI.mk T.empty) attrs)
39 | otherwise = id