aboutsummaryrefslogtreecommitdiff
path: root/tp-bbcode/src/Thermoprint/Printout/BBCode
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-02-25 01:44:35 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-02-25 01:44:35 +0000
commitda0aef961fef08e2690f0dff272b57340dc1d151 (patch)
treef65fdf390a83d508131fe724ab077f8c3e737377 /tp-bbcode/src/Thermoprint/Printout/BBCode
parent133a91f949025308a9985e6ab9db7d542bbd6678 (diff)
downloadthermoprint-da0aef961fef08e2690f0dff272b57340dc1d151.tar
thermoprint-da0aef961fef08e2690f0dff272b57340dc1d151.tar.gz
thermoprint-da0aef961fef08e2690f0dff272b57340dc1d151.tar.bz2
thermoprint-da0aef961fef08e2690f0dff272b57340dc1d151.tar.xz
thermoprint-da0aef961fef08e2690f0dff272b57340dc1d151.zip
Inverse to bbcode
Diffstat (limited to 'tp-bbcode/src/Thermoprint/Printout/BBCode')
-rw-r--r--tp-bbcode/src/Thermoprint/Printout/BBCode/Inverse.hs53
1 files changed, 53 insertions, 0 deletions
diff --git a/tp-bbcode/src/Thermoprint/Printout/BBCode/Inverse.hs b/tp-bbcode/src/Thermoprint/Printout/BBCode/Inverse.hs
new file mode 100644
index 0000000..edd4c5a
--- /dev/null
+++ b/tp-bbcode/src/Thermoprint/Printout/BBCode/Inverse.hs
@@ -0,0 +1,53 @@
1{-# LANGUAGE ViewPatterns #-}
2
3module Thermoprint.Printout.BBCode.Inverse
4 ( cobbcode
5 , UnicodeException(..)
6 ) where
7
8import Data.Sequence (Seq)
9import qualified Data.Sequence as Seq ()
10
11import Data.Text (Text)
12import qualified Data.Text as T (pack, empty, isSuffixOf)
13import qualified Data.Text.Lazy as LT (toStrict)
14
15import Data.ByteString.Lazy as LBS (toStrict)
16
17import Data.Text.Encoding
18import Data.Text.Encoding.Error (UnicodeException(..))
19
20import Data.Foldable (toList)
21import Data.List
22import Data.Monoid
23
24import Thermoprint.Printout
25
26cobbcode :: Printout -> Either UnicodeException Text
27cobbcode (toList -> ps) = mconcat . intersperse "\n\n" <$> mapM handlePar ps
28
29handlePar :: Seq Chunk -> Either UnicodeException Text
30handlePar (toList -> cs) = mconcat <$> mapM handleChunk cs
31
32handleChunk :: Chunk -> Either UnicodeException Text
33handleChunk (Cooked b) = Right $ handleBlock b
34handleChunk (Raw bs) = decodeUtf8' $ LBS.toStrict bs
35
36handleBlock :: Block -> Text
37handleBlock (Line l) = handleLine l
38handleBlock (VSpace i)
39 | i /= 0 = "[vspace=" <> T.pack (show i) <> "/]"
40 | otherwise = ""
41handleBlock (NewlSep (toList -> bs)) = mconcat . intersperse "\n" $ map handleBlock bs
42
43handleLine :: Line -> Text
44handleLine = flip handleLine' T.empty
45 where
46 handleLine' (HSpace i) p
47 | i == 0 = ""
48 | i == 1 = " "
49 | " " `T.isSuffixOf` p = "[hspace=" <> T.pack (show i) <> "/]"
50 | i <= 2 = T.pack $ genericReplicate i ' '
51 | otherwise = " [hspace=" <> T.pack (show $ i - 2) <> "/] "
52 handleLine' (JuxtaPos ls) p = foldl (\p l -> p <> handleLine' l p) "" ls
53 handleLine' (Line -> b) _ = LT.toStrict $ cotext b