aboutsummaryrefslogtreecommitdiff
path: root/spec
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 /spec
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 'spec')
-rw-r--r--spec/src/Thermoprint/Printout/BBCode.hs145
-rw-r--r--spec/src/Thermoprint/Printout/BBCode/Attribute.hs39
-rw-r--r--spec/test/Thermoprint/Printout/BBCodeSpec.hs42
-rw-r--r--spec/thermoprint-spec.cabal9
-rw-r--r--spec/thermoprint-spec.nix13
5 files changed, 8 insertions, 240 deletions
diff --git a/spec/src/Thermoprint/Printout/BBCode.hs b/spec/src/Thermoprint/Printout/BBCode.hs
deleted file mode 100644
index ce2aa43..0000000
--- a/spec/src/Thermoprint/Printout/BBCode.hs
+++ /dev/null
@@ -1,145 +0,0 @@
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/spec/src/Thermoprint/Printout/BBCode/Attribute.hs b/spec/src/Thermoprint/Printout/BBCode/Attribute.hs
deleted file mode 100644
index 538cca2..0000000
--- a/spec/src/Thermoprint/Printout/BBCode/Attribute.hs
+++ /dev/null
@@ -1,39 +0,0 @@
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
diff --git a/spec/test/Thermoprint/Printout/BBCodeSpec.hs b/spec/test/Thermoprint/Printout/BBCodeSpec.hs
deleted file mode 100644
index f3f1840..0000000
--- a/spec/test/Thermoprint/Printout/BBCodeSpec.hs
+++ /dev/null
@@ -1,42 +0,0 @@
1{-# LANGUAGE OverloadedStrings, OverloadedLists #-}
2{-# LANGUAGE StandaloneDeriving #-}
3
4module Thermoprint.Printout.BBCodeSpec (spec) where
5
6import Test.Hspec
7import Test.Hspec.QuickCheck (prop)
8import Test.QuickCheck.Instances
9
10import Thermoprint.Printout.BBCode
11import Thermoprint.Printout
12
13import Data.Text (Text)
14
15import Control.Monad (zipWithM_)
16import Data.Monoid ((<>))
17import Data.Function (on)
18
19import qualified Data.Sequence as Seq (fromList)
20
21instance Eq Block where
22 (==) = (==) `on` cotext
23deriving instance Eq Chunk
24
25spec :: Spec
26spec = do
27 zipWithM_ example [1..] examples
28 where
29 example n (s, ts) = let str = "Example " <> show n
30 in specify str $ bbcode s == (pOut <$> ts)
31
32pOut :: [Block] -> Printout
33pOut = pure . Seq.fromList . map Cooked
34
35examples :: [(Text, Either BBCodeError [Block])]
36examples = [ ("Hello World!"
37 , Right [Line (JuxtaPos [word "Hello", HSpace 1, word "World!"])])
38 , ("Hello [hspace width=2/] World!"
39 , Right [Line (JuxtaPos [word "Hello", HSpace 4, word "World!"])])
40 ]
41 where
42 word = (\(Right l) -> l) . text
diff --git a/spec/thermoprint-spec.cabal b/spec/thermoprint-spec.cabal
index 5624a3b..21ed439 100644
--- a/spec/thermoprint-spec.cabal
+++ b/spec/thermoprint-spec.cabal
@@ -19,10 +19,9 @@ cabal-version: >=1.10
19library 19library
20 hs-source-dirs: src 20 hs-source-dirs: src
21 exposed-modules: Thermoprint.Printout 21 exposed-modules: Thermoprint.Printout
22 , Thermoprint.Printout.BBCode
23 , Thermoprint.Identifiers 22 , Thermoprint.Identifiers
24 , Thermoprint.API 23 , Thermoprint.API
25 other-modules: Thermoprint.Printout.BBCode.Attribute 24 -- other-modules:
26 -- other-extensions: 25 -- other-extensions:
27 extensions: DeriveGeneric 26 extensions: DeriveGeneric
28 , DeriveAnyClass 27 , DeriveAnyClass
@@ -33,7 +32,6 @@ library
33 , GADTs 32 , GADTs
34 , DefaultSignatures 33 , DefaultSignatures
35 build-depends: base >=4.8.1 && <5 34 build-depends: base >=4.8.1 && <5
36 , bbcode -any
37 , containers >=0.5.6 && <1 35 , containers >=0.5.6 && <1
38 , text >=1.2.1 && <2 36 , text >=1.2.1 && <2
39 , bytestring >=0.10.6 && <1 37 , bytestring >=0.10.6 && <1
@@ -46,7 +44,6 @@ library
46 , aeson >=0.9.0 && <1 44 , aeson >=0.9.0 && <1
47 , base64-bytestring >=1.0.0 && <2 45 , base64-bytestring >=1.0.0 && <2
48 , encoding >=0.8 && <1 46 , encoding >=0.8 && <1
49 , case-insensitive >=1.2 && <2
50 -- hs-source-dirs: 47 -- hs-source-dirs:
51 default-language: Haskell2010 48 default-language: Haskell2010
52 49
@@ -63,6 +60,4 @@ Test-Suite tests
63 , hspec >=2.2.1 && <3 60 , hspec >=2.2.1 && <3
64 , QuickCheck >=2.8.1 && <3 61 , QuickCheck >=2.8.1 && <3
65 , quickcheck-instances >=0.3.11 && <4 62 , quickcheck-instances >=0.3.11 && <4
66 , aeson >=0.9.0 && <1 63 , aeson >=0.9.0 && <1 \ No newline at end of file
67 , containers >=0.5.6 && <1
68 , text >=1.2.1 && <2 \ No newline at end of file
diff --git a/spec/thermoprint-spec.nix b/spec/thermoprint-spec.nix
index 0e548a6..5a89bcf 100644
--- a/spec/thermoprint-spec.nix
+++ b/spec/thermoprint-spec.nix
@@ -1,16 +1,15 @@
1{ mkDerivation, aeson, base, base64-bytestring, bbcode, bytestring 1{ mkDerivation, aeson, base, base64-bytestring, bytestring, Cabal
2, Cabal, cabal-test-quickcheck, case-insensitive, containers 2, cabal-test-quickcheck, containers, deepseq, encoding, hspec
3, deepseq, encoding, hspec, QuickCheck, quickcheck-instances 3, QuickCheck, quickcheck-instances, servant, stdenv, text
4, servant, stdenv, text
5}: 4}:
6mkDerivation { 5mkDerivation {
7 pname = "thermoprint-spec"; 6 pname = "thermoprint-spec";
8 version = "2.0.0"; 7 version = "2.0.0";
9 src = ./.; 8 src = ./.;
10 libraryHaskellDepends = [ 9 libraryHaskellDepends = [
11 aeson base base64-bytestring bbcode bytestring Cabal 10 aeson base base64-bytestring bytestring Cabal cabal-test-quickcheck
12 cabal-test-quickcheck case-insensitive containers deepseq encoding 11 containers deepseq encoding QuickCheck quickcheck-instances servant
13 QuickCheck quickcheck-instances servant text 12 text
14 ]; 13 ];
15 testHaskellDepends = [ 14 testHaskellDepends = [
16 aeson base hspec QuickCheck quickcheck-instances 15 aeson base hspec QuickCheck quickcheck-instances