diff options
| author | Gregor Kleen <pngwjpgh@users.noreply.github.com> | 2017-01-23 16:11:37 +0100 |
|---|---|---|
| committer | Gregor Kleen <pngwjpgh@users.noreply.github.com> | 2017-01-23 16:11:37 +0100 |
| commit | 99fc4947543c1916e9fec952526a688eb7753490 (patch) | |
| tree | 9361649ae4639a00cff06ad654cb42e3e07bc637 /tp-bbcode/src | |
| parent | 59a7e3d173c23096fe3122505b1b759f26e3292a (diff) | |
| parent | 64b6ead0d1e157701f8569743eda496bc71b8351 (diff) | |
| download | thermoprint-99fc4947543c1916e9fec952526a688eb7753490.tar thermoprint-99fc4947543c1916e9fec952526a688eb7753490.tar.gz thermoprint-99fc4947543c1916e9fec952526a688eb7753490.tar.bz2 thermoprint-99fc4947543c1916e9fec952526a688eb7753490.tar.xz thermoprint-99fc4947543c1916e9fec952526a688eb7753490.zip | |
Merge branch 'feat-markup'
Diffstat (limited to 'tp-bbcode/src')
| -rw-r--r-- | tp-bbcode/src/Thermoprint/Printout/BBCode.hs | 27 |
1 files changed, 23 insertions, 4 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 | ||
