aboutsummaryrefslogtreecommitdiff
path: root/tp-bbcode
diff options
context:
space:
mode:
authorGregor Kleen <pngwjpgh@users.noreply.github.com>2017-01-23 16:09:05 +0100
committerGregor Kleen <pngwjpgh@users.noreply.github.com>2017-01-23 16:09:05 +0100
commit64b6ead0d1e157701f8569743eda496bc71b8351 (patch)
treef7ae09e9ec950e91cacb1923aa54b96d59c9a8f3 /tp-bbcode
parente95dac748371afcad3ffddf5c98e5fcb0a8302b7 (diff)
downloadthermoprint-64b6ead0d1e157701f8569743eda496bc71b8351.tar
thermoprint-64b6ead0d1e157701f8569743eda496bc71b8351.tar.gz
thermoprint-64b6ead0d1e157701f8569743eda496bc71b8351.tar.bz2
thermoprint-64b6ead0d1e157701f8569743eda496bc71b8351.tar.xz
thermoprint-64b6ead0d1e157701f8569743eda496bc71b8351.zip
Add support for ESC/POS text decoration
Diffstat (limited to 'tp-bbcode')
-rw-r--r--tp-bbcode/src/Thermoprint/Printout/BBCode.hs27
-rw-r--r--tp-bbcode/test/Thermoprint/Printout/BBCodeSpec.hs2
-rw-r--r--tp-bbcode/thermoprint-bbcode.cabal4
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
14import Data.Text (Text) 15import Data.Text (Text)
15import Data.Map (Map) 16import Data.Map (Map)
17import qualified Data.Map as Map
18import Data.Set (Set)
19import qualified Data.Set as Set
16 20
17import qualified Data.Text.Lazy as Lazy (Text) 21import qualified Data.Text.Lazy as Lazy (Text)
18import qualified Data.Text.Lazy as TL (fromStrict) 22import qualified Data.Text.Lazy as TL (fromStrict)
@@ -31,6 +35,7 @@ import Data.Bifunctor (bimap, first)
31import Control.Monad (join) 35import Control.Monad (join)
32 36
33import Data.List (groupBy) 37import Data.List (groupBy)
38import Data.Maybe (mapMaybe)
34 39
35import Text.BBCode (DomForest, DomTree(..), TreeError(..)) 40import Text.BBCode (DomForest, DomTree(..), TreeError(..))
36import qualified Text.BBCode as Raw (bbcode, BBCodeError(..)) 41import qualified Text.BBCode as Raw (bbcode, BBCodeError(..))
@@ -140,9 +145,23 @@ parse BlockCtx = fmap mconcat . mapM mergeResult' . groupBy sameCtx . map parseD
140parse ctx = mergeResult ctx . map parseDom 145parse ctx = mergeResult ctx . map parseDom
141 146
142asBlock :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Block 147asBlock :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Block
143asBlock "VSpace" _ = Right . VSpace . lookupAttr "height" True 1 148asBlock "VSpace" _ attrs = Right . VSpace $ lookupAttr "height" True 1 attrs
144asBlock t _ = const $ Left . UnmappedBlockElement . CI.original $ t 149asBlock t _ _ = Left . UnmappedBlockElement . CI.original $ t
145 150
146asLine :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Line 151asLine :: CI Text -> [DomTree] -> Map (CI Text) Text -> Either SemanticError Line
147asLine "HSpace" _ = Right . HSpace . lookupAttr "width" True 1 152asLine "HSpace" _ attrs = Right . HSpace $ lookupAttr "width" True 1 attrs
148asLine t _ = const $ Left . UnmappedLineElement . CI.original $ t 153asLine 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 ]
167asLine 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