diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-18 12:09:36 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-18 12:09:36 +0000 |
commit | 57c56564d15cd5c83a4f1d1bab5490e6b75e8656 (patch) | |
tree | 55ea741665155b46b9e599b149dee3323fe479c0 /spec | |
parent | 9435083465a487553b21c599c1340aa5e5ed8a1c (diff) | |
download | thermoprint-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.hs | 145 | ||||
-rw-r--r-- | spec/src/Thermoprint/Printout/BBCode/Attribute.hs | 39 | ||||
-rw-r--r-- | spec/test/Thermoprint/Printout/BBCodeSpec.hs | 42 | ||||
-rw-r--r-- | spec/thermoprint-spec.cabal | 9 | ||||
-rw-r--r-- | spec/thermoprint-spec.nix | 13 |
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 | ||
6 | module Thermoprint.Printout.BBCode | ||
7 | ( bbcode | ||
8 | , BBCodeError(..) | ||
9 | , TreeError(..) | ||
10 | , SemanticError(..) | ||
11 | ) where | ||
12 | |||
13 | import Data.Text (Text) | ||
14 | import Data.Map (Map) | ||
15 | |||
16 | import qualified Data.Text.Lazy as Lazy (Text) | ||
17 | import qualified Data.Text.Lazy as TL (fromStrict) | ||
18 | |||
19 | import Data.Sequence (Seq) | ||
20 | import qualified Data.Sequence as Seq (fromList, singleton) | ||
21 | |||
22 | import Data.CaseInsensitive (CI) | ||
23 | import qualified Data.CaseInsensitive as CI | ||
24 | |||
25 | import GHC.Generics (Generic) | ||
26 | import Control.Exception (Exception) | ||
27 | import Data.Typeable (Typeable) | ||
28 | |||
29 | import Data.Bifunctor (bimap, first) | ||
30 | import Control.Monad (join) | ||
31 | |||
32 | import Data.List (groupBy) | ||
33 | |||
34 | import Text.BBCode (DomForest, DomTree(..), TreeError(..)) | ||
35 | import qualified Text.BBCode as Raw (bbcode, BBCodeError(..)) | ||
36 | |||
37 | import Thermoprint.Printout | ||
38 | |||
39 | import 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 | ||
42 | data 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 | |||
47 | instance Exception BBCodeError | ||
48 | |||
49 | morph' :: Raw.BBCodeError -> BBCodeError | ||
50 | -- ^ Transform 'Raw.BBCodeError' to 'BBCodeError' | ||
51 | morph' (Raw.LexerError x) = LexerError x | ||
52 | morph' (Raw.TreeError x) = TreeError x | ||
53 | |||
54 | -- | An error ocurred while parsing the DOM-Forest (`['DomTree']`) | ||
55 | data 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 | |||
61 | instance Exception SemanticError | ||
62 | |||
63 | -- | Result of parsing a single 'DomTree' | ||
64 | data 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 | ||
71 | data Context a where | ||
72 | BlockCtx :: Context Block | ||
73 | LineCtx :: Context Line | ||
74 | |||
75 | extract :: Context a -> ParseResult -> Either SemanticError a | ||
76 | -- ^ Extract information from a 'ParseResult' given 'Context' | ||
77 | extract BlockCtx (RBlock b) = Right b | ||
78 | extract LineCtx (RLine l) = Right l | ||
79 | extract BlockCtx (RAmbiguous b _) = Right b | ||
80 | extract LineCtx (RAmbiguous _ l) = Right l | ||
81 | extract BlockCtx (RNoParse bErr _) = Left bErr | ||
82 | extract LineCtx (RNoParse _ lErr) = Left lErr | ||
83 | extract BlockCtx _ = Left LineInBlockContext | ||
84 | extract LineCtx _ = Left BlockInLineContext | ||
85 | |||
86 | hasBlockCtx :: ParseResult -> Bool | ||
87 | -- ^ Result can be 'extract'ed in a 'Block' 'Context' | ||
88 | hasBlockCtx (RLine _) = False | ||
89 | hasBlockCtx _ = True | ||
90 | |||
91 | hasLineCtx :: ParseResult -> Bool | ||
92 | -- ^ Result can be 'extract'ed in a 'Line' 'Context' | ||
93 | hasLineCtx (RBlock _) = False | ||
94 | hasLineCtx _ = True | ||
95 | |||
96 | bbcode :: Text -> Either BBCodeError Printout | ||
97 | -- ^ Parse BBCode | ||
98 | bbcode = join . fmap (first SemanticError) . bimap morph' morph . Raw.bbcode | ||
99 | |||
100 | morph :: 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' | ||
104 | morph = fmap Seq.fromList . mapM (\t -> Seq.singleton . Cooked <$> parse BlockCtx t) | ||
105 | |||
106 | parseDom :: DomTree -> ParseResult | ||
107 | -- ^ Invoke 'asLine' and 'asBlock' to parse a single 'DomTree' | ||
108 | parseDom (Content t) = either RBlock (\l -> RAmbiguous (Line l) l) . text . TL.fromStrict $ t | ||
109 | parseDom (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 | |||
120 | mergeResult :: Monoid a => Context a -> [ParseResult] -> Either SemanticError a | ||
121 | -- ^ Merge a list of 'ParseResults' in a certain 'Context' | ||
122 | mergeResult _ [] = Right mempty | ||
123 | mergeResult ctx (amb@(RAmbiguous _ _):xs) = mappend <$> extract ctx amb <*> mergeResult ctx xs | ||
124 | mergeResult ctx (err@(RNoParse _ _):_) = extract ctx err | ||
125 | mergeResult ctx (x:xs) = mappend <$> extract ctx x <*> mergeResult ctx xs | ||
126 | |||
127 | parse :: 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'@ | ||
131 | parse 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 | ||
137 | parse ctx = mergeResult ctx . map parseDom | ||
138 | |||
139 | asBlock :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Block | ||
140 | asBlock "VSpace" _ = Right . VSpace . lookupAttr "height" True 1 | ||
141 | asBlock t _ = const $ Left . UnmappedBlockElement . CI.original $ t | ||
142 | |||
143 | asLine :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Line | ||
144 | asLine "HSpace" _ = Right . HSpace . lookupAttr "width" True 1 | ||
145 | asLine 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 | ||
4 | module Thermoprint.Printout.BBCode.Attribute | ||
5 | ( Attribute(..) | ||
6 | , lookupAttr | ||
7 | ) where | ||
8 | |||
9 | import Data.Text (Text) | ||
10 | import qualified Data.Text as T (unpack, empty) | ||
11 | |||
12 | import Data.Map (Map) | ||
13 | import qualified Data.Map as Map (lookup) | ||
14 | |||
15 | import Data.CaseInsensitive (CI) | ||
16 | import qualified Data.CaseInsensitive as CI | ||
17 | |||
18 | import Text.Read (readMaybe) | ||
19 | import Data.Maybe (fromMaybe) | ||
20 | |||
21 | import 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' | ||
26 | class Attribute a where | ||
27 | attrRead :: Text -> Maybe a | ||
28 | default attrRead :: Read a => Text -> Maybe a | ||
29 | attrRead = readMaybe . T.unpack | ||
30 | |||
31 | instance Attribute Integer | ||
32 | |||
33 | lookupAttr :: 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 | ||
35 | lookupAttr 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 | |||
4 | module Thermoprint.Printout.BBCodeSpec (spec) where | ||
5 | |||
6 | import Test.Hspec | ||
7 | import Test.Hspec.QuickCheck (prop) | ||
8 | import Test.QuickCheck.Instances | ||
9 | |||
10 | import Thermoprint.Printout.BBCode | ||
11 | import Thermoprint.Printout | ||
12 | |||
13 | import Data.Text (Text) | ||
14 | |||
15 | import Control.Monad (zipWithM_) | ||
16 | import Data.Monoid ((<>)) | ||
17 | import Data.Function (on) | ||
18 | |||
19 | import qualified Data.Sequence as Seq (fromList) | ||
20 | |||
21 | instance Eq Block where | ||
22 | (==) = (==) `on` cotext | ||
23 | deriving instance Eq Chunk | ||
24 | |||
25 | spec :: Spec | ||
26 | spec = 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 | |||
32 | pOut :: [Block] -> Printout | ||
33 | pOut = pure . Seq.fromList . map Cooked | ||
34 | |||
35 | examples :: [(Text, Either BBCodeError [Block])] | ||
36 | examples = [ ("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 | |||
19 | library | 19 | library |
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 | }: |
6 | mkDerivation { | 5 | mkDerivation { |
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 |