diff options
Diffstat (limited to 'tp-bbcode')
| -rw-r--r-- | tp-bbcode/src/Thermoprint/Printout/BBCode.hs | 27 | ||||
| -rw-r--r-- | tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs | 2 | ||||
| -rw-r--r-- | tp-bbcode/thermoprint-bbcode.cabal | 4 | 
3 files changed, 27 insertions, 6 deletions
diff --git a/tp-bbcode/src/Thermoprint/Printout/BBCode.hs b/tp-bbcode/src/Thermoprint/Printout/BBCode.hs index dd5edb0..1770b6d 100644 --- a/tp-bbcode/src/Thermoprint/Printout/BBCode.hs +++ b/tp-bbcode/src/Thermoprint/Printout/BBCode.hs  | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | {-# LANGUAGE OverloadedStrings #-} | 1 | {-# LANGUAGE OverloadedStrings #-} | 
| 2 | {-# LANGUAGE OverloadedLists #-} | ||
| 2 | {-# LANGUAGE DeriveGeneric #-} | 3 | {-# LANGUAGE DeriveGeneric #-} | 
| 3 | {-# LANGUAGE GADTs #-} | 4 | {-# LANGUAGE GADTs #-} | 
| 4 | 5 | ||
| @@ -13,6 +14,9 @@ module Thermoprint.Printout.BBCode | |||
| 13 | 14 | ||
| 14 | import Data.Text (Text) | 15 | import Data.Text (Text) | 
| 15 | import Data.Map (Map) | 16 | import Data.Map (Map) | 
| 17 | import qualified Data.Map as Map | ||
| 18 | import Data.Set (Set) | ||
| 19 | import qualified Data.Set as Set | ||
| 16 | 20 | ||
| 17 | import qualified Data.Text.Lazy as Lazy (Text) | 21 | import qualified Data.Text.Lazy as Lazy (Text) | 
| 18 | import qualified Data.Text.Lazy as TL (fromStrict) | 22 | import qualified Data.Text.Lazy as TL (fromStrict) | 
| @@ -31,6 +35,7 @@ import Data.Bifunctor (bimap, first) | |||
| 31 | import Control.Monad (join) | 35 | import Control.Monad (join) | 
| 32 | 36 | ||
| 33 | import Data.List (groupBy) | 37 | import Data.List (groupBy) | 
| 38 | import Data.Maybe (mapMaybe) | ||
| 34 | 39 | ||
| 35 | import Text.BBCode (DomForest, DomTree(..), TreeError(..)) | 40 | import Text.BBCode (DomForest, DomTree(..), TreeError(..)) | 
| 36 | import qualified Text.BBCode as Raw (bbcode, BBCodeError(..)) | 41 | import qualified Text.BBCode as Raw (bbcode, BBCodeError(..)) | 
| @@ -140,9 +145,23 @@ parse BlockCtx = fmap mconcat . mapM mergeResult' . groupBy sameCtx . map parseD | |||
| 140 | parse ctx = mergeResult ctx . map parseDom | 145 | parse ctx = mergeResult ctx . map parseDom | 
| 141 | 146 | ||
| 142 | asBlock :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Block | 147 | asBlock :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Block | 
| 143 | asBlock "VSpace" _ = Right . VSpace . lookupAttr "height" True 1 | 148 | asBlock "VSpace" _ attrs = Right . VSpace $ lookupAttr "height" True 1 attrs | 
| 144 | asBlock t _ = const $ Left . UnmappedBlockElement . CI.original $ t | 149 | asBlock t _ _ = Left . UnmappedBlockElement . CI.original $ t | 
| 145 | 150 | ||
| 146 | asLine :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Line | 151 | asLine :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Line | 
| 147 | asLine "HSpace" _ = Right . HSpace . lookupAttr "width" True 1 | 152 | asLine "HSpace" _ attrs = Right . HSpace $ lookupAttr "width" True 1 attrs | 
| 148 | asLine t _ = const $ Left . UnmappedLineElement . CI.original $ t | 153 | asLine c t attrs | 
| 154 | | (Just m) <- lookup c $ map (\(c, _, m) -> (CI.mk [c], m)) mTable | ||
| 155 | = Markup [m] <$> parse LineCtx t | ||
| 156 | | (Just m) <- lookup c $ map (\(_, a, m) -> (a, m)) mTable | ||
| 157 | = Markup [m] <$> parse LineCtx t | ||
| 158 | | "markup" <- c | ||
| 159 | , ms <- Set.fromList $ mapMaybe (\(_, a, m) -> m <$ Map.lookup a attrs) mTable | ||
| 160 | = Markup ms <$> parse LineCtx t | ||
| 161 | where | ||
| 162 | mTable = [ ('b', "bold", Bold) | ||
| 163 | , ('u', "underline", Underline) | ||
| 164 | , ('h', "doubleHeight", DoubleHeight) | ||
| 165 | , ('w', "doubleWidth", DoubleWidth) | ||
| 166 | ] | ||
| 167 | asLine t _ _ = Left . UnmappedLineElement . CI.original $ t | ||
diff --git a/tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs b/tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs index 7909360..2f70ddc 100644 --- a/tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs +++ b/tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs  | |||
| @@ -64,4 +64,6 @@ examples = [ ("Hello World!" | |||
| 64 | , Right [Line ("Par1"), Line ("Par2"), Line (JuxtaPos ["Par3", HSpace 1, "Word2"])]) | 64 | , Right [Line ("Par1"), Line ("Par2"), Line (JuxtaPos ["Par3", HSpace 1, "Word2"])]) | 
| 65 | , ("Par1 [hspace=3/]\n\n[vspace=2/]Par2\n\nPar3 Word2" | 65 | , ("Par1 [hspace=3/]\n\n[vspace=2/]Par2\n\nPar3 Word2" | 
| 66 | , Right [Line (JuxtaPos ["Par1", HSpace 4]), NewlSep [VSpace 2, Line ("Par2")], Line (JuxtaPos ["Par3", HSpace 1, "Word2"])]) | 66 | , Right [Line (JuxtaPos ["Par1", HSpace 4]), NewlSep [VSpace 2, Line ("Par2")], Line (JuxtaPos ["Par3", HSpace 1, "Word2"])]) | 
| 67 | , ("[b]B [u]BM[/u][/b] [markup bold=1 underline=1]BM[/markup]" | ||
| 68 | , Right [Line (JuxtaPos [Markup [Bold] (JuxtaPos ["B",HSpace 1,Markup [Underline] "BM"]),HSpace 1,Markup [Bold,Underline] "BM"])]) | ||
| 67 | ] | 69 | ] | 
diff --git a/tp-bbcode/thermoprint-bbcode.cabal b/tp-bbcode/thermoprint-bbcode.cabal index 29855e2..b476753 100644 --- a/tp-bbcode/thermoprint-bbcode.cabal +++ b/tp-bbcode/thermoprint-bbcode.cabal  | |||
| @@ -24,7 +24,7 @@ library | |||
| 24 | , OverloadedLists | 24 | , OverloadedLists | 
| 25 | -- other-extensions: | 25 | -- other-extensions: | 
| 26 | build-depends: base >=4.8.1 && <5 | 26 | build-depends: base >=4.8.1 && <5 | 
| 27 | , thermoprint-spec ==4.0.* | 27 | , thermoprint-spec ==5.0.* | 
| 28 | , bbcode >=3.1.1 && <4 | 28 | , bbcode >=3.1.1 && <4 | 
| 29 | , containers -any | 29 | , containers -any | 
| 30 | , text -any | 30 | , text -any | 
| @@ -44,7 +44,7 @@ Test-Suite tests | |||
| 44 | , OverloadedLists | 44 | , OverloadedLists | 
| 45 | build-depends: base >=4.8.1 && <5 | 45 | build-depends: base >=4.8.1 && <5 | 
| 46 | , thermoprint-bbcode -any | 46 | , thermoprint-bbcode -any | 
| 47 | , thermoprint-spec ==4.0.* | 47 | , thermoprint-spec ==5.0.* | 
| 48 | , hspec >=2.2.1 && <3 | 48 | , hspec >=2.2.1 && <3 | 
| 49 | , QuickCheck >=2.8.1 && <3 | 49 | , QuickCheck >=2.8.1 && <3 | 
| 50 | , quickcheck-instances >=0.3.11 && <4 | 50 | , quickcheck-instances >=0.3.11 && <4 | 
