diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-25 01:44:35 +0000 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-02-25 01:44:35 +0000 |
commit | da0aef961fef08e2690f0dff272b57340dc1d151 (patch) | |
tree | f65fdf390a83d508131fe724ab077f8c3e737377 /tp-bbcode/src | |
parent | 133a91f949025308a9985e6ab9db7d542bbd6678 (diff) | |
download | thermoprint-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')
-rw-r--r-- | tp-bbcode/src/Thermoprint/Printout/BBCode.hs | 3 | ||||
-rw-r--r-- | tp-bbcode/src/Thermoprint/Printout/BBCode/Inverse.hs | 53 |
2 files changed, 56 insertions, 0 deletions
diff --git a/tp-bbcode/src/Thermoprint/Printout/BBCode.hs b/tp-bbcode/src/Thermoprint/Printout/BBCode.hs index ce2aa43..cbe2618 100644 --- a/tp-bbcode/src/Thermoprint/Printout/BBCode.hs +++ b/tp-bbcode/src/Thermoprint/Printout/BBCode.hs | |||
@@ -8,6 +8,7 @@ module Thermoprint.Printout.BBCode | |||
8 | , BBCodeError(..) | 8 | , BBCodeError(..) |
9 | , TreeError(..) | 9 | , TreeError(..) |
10 | , SemanticError(..) | 10 | , SemanticError(..) |
11 | , module Thermoprint.Printout.BBCode.Inverse | ||
11 | ) where | 12 | ) where |
12 | 13 | ||
13 | import Data.Text (Text) | 14 | import Data.Text (Text) |
@@ -34,6 +35,8 @@ import Data.List (groupBy) | |||
34 | import Text.BBCode (DomForest, DomTree(..), TreeError(..)) | 35 | import Text.BBCode (DomForest, DomTree(..), TreeError(..)) |
35 | import qualified Text.BBCode as Raw (bbcode, BBCodeError(..)) | 36 | import qualified Text.BBCode as Raw (bbcode, BBCodeError(..)) |
36 | 37 | ||
38 | import Thermoprint.Printout.BBCode.Inverse | ||
39 | |||
37 | import Thermoprint.Printout | 40 | import Thermoprint.Printout |
38 | 41 | ||
39 | import Thermoprint.Printout.BBCode.Attribute | 42 | import Thermoprint.Printout.BBCode.Attribute |
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 | |||
3 | module Thermoprint.Printout.BBCode.Inverse | ||
4 | ( cobbcode | ||
5 | , UnicodeException(..) | ||
6 | ) where | ||
7 | |||
8 | import Data.Sequence (Seq) | ||
9 | import qualified Data.Sequence as Seq () | ||
10 | |||
11 | import Data.Text (Text) | ||
12 | import qualified Data.Text as T (pack, empty, isSuffixOf) | ||
13 | import qualified Data.Text.Lazy as LT (toStrict) | ||
14 | |||
15 | import Data.ByteString.Lazy as LBS (toStrict) | ||
16 | |||
17 | import Data.Text.Encoding | ||
18 | import Data.Text.Encoding.Error (UnicodeException(..)) | ||
19 | |||
20 | import Data.Foldable (toList) | ||
21 | import Data.List | ||
22 | import Data.Monoid | ||
23 | |||
24 | import Thermoprint.Printout | ||
25 | |||
26 | cobbcode :: Printout -> Either UnicodeException Text | ||
27 | cobbcode (toList -> ps) = mconcat . intersperse "\n\n" <$> mapM handlePar ps | ||
28 | |||
29 | handlePar :: Seq Chunk -> Either UnicodeException Text | ||
30 | handlePar (toList -> cs) = mconcat <$> mapM handleChunk cs | ||
31 | |||
32 | handleChunk :: Chunk -> Either UnicodeException Text | ||
33 | handleChunk (Cooked b) = Right $ handleBlock b | ||
34 | handleChunk (Raw bs) = decodeUtf8' $ LBS.toStrict bs | ||
35 | |||
36 | handleBlock :: Block -> Text | ||
37 | handleBlock (Line l) = handleLine l | ||
38 | handleBlock (VSpace i) | ||
39 | | i /= 0 = "[vspace=" <> T.pack (show i) <> "/]" | ||
40 | | otherwise = "" | ||
41 | handleBlock (NewlSep (toList -> bs)) = mconcat . intersperse "\n" $ map handleBlock bs | ||
42 | |||
43 | handleLine :: Line -> Text | ||
44 | handleLine = 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 | ||