2021-04-29 11:52:44 +02:00
|
|
|
module LaantasImage
|
2024-11-28 01:32:27 +01:00
|
|
|
(Item (..), splitItem, splitItem', makeItem)
|
2021-04-29 11:52:44 +02:00
|
|
|
where
|
|
|
|
|
2024-06-03 03:33:36 +02:00
|
|
|
import Lang
|
2024-11-28 01:32:27 +01:00
|
|
|
import Text.Pandoc.Definition
|
2021-04-29 11:52:44 +02:00
|
|
|
import Data.Bifunctor
|
|
|
|
import Data.Function
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.Map (Map)
|
|
|
|
import qualified Data.Map as Map
|
|
|
|
import Data.Text (Text)
|
|
|
|
import qualified Data.Text as Text
|
2024-11-28 01:32:27 +01:00
|
|
|
import qualified Data.Text.Lazy as Lazy
|
|
|
|
import qualified Laantas
|
2024-11-30 20:03:19 +01:00
|
|
|
import Data.Char (isSpace)
|
2021-04-29 11:52:44 +02:00
|
|
|
|
|
|
|
|
2024-11-28 01:32:27 +01:00
|
|
|
data Item =
|
|
|
|
Item {
|
2021-04-29 11:52:44 +02:00
|
|
|
text, title :: Text,
|
|
|
|
size, stroke :: Double,
|
2024-11-28 01:32:27 +01:00
|
|
|
width :: Double,
|
2021-04-29 11:52:44 +02:00
|
|
|
color :: Text,
|
|
|
|
showText :: Bool
|
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
2024-11-28 01:32:27 +01:00
|
|
|
splitItem :: Vars => Text -> Maybe Item
|
|
|
|
splitItem (Text.uncons -> Just (c, txt))
|
|
|
|
| c == '!' = Just $ splitItem' txt
|
|
|
|
| c == '#' = Just $ (splitItem' txt) {showText = False}
|
|
|
|
splitItem _ = Nothing
|
2021-04-29 11:52:44 +02:00
|
|
|
|
2024-11-28 01:32:27 +01:00
|
|
|
splitItem' :: Vars => Text -> Item
|
|
|
|
splitItem' txt₀ =
|
2021-04-29 11:52:44 +02:00
|
|
|
case imageOpts txt₀ of
|
2024-11-28 01:32:27 +01:00
|
|
|
Just (txt, opts) -> defaultItem txt ?defColor
|
2021-04-29 11:52:44 +02:00
|
|
|
& withOpt opts "size" (\s i -> i {size = readt s})
|
|
|
|
& withOpt opts "stroke" (\k i -> i {stroke = readt k})
|
|
|
|
& withOpt opts "width" (\w i -> i {width = readt w})
|
|
|
|
& withOpt opts "color" (\c i -> i {color = c})
|
2024-11-28 01:32:27 +01:00
|
|
|
Nothing -> defaultItem txt₀ ?defColor
|
2021-04-29 11:52:44 +02:00
|
|
|
where readt x = read $ Text.unpack x
|
|
|
|
|
|
|
|
withOpt :: Ord k => Map k v -> k -> (v -> a -> a) -> (a -> a)
|
|
|
|
withOpt m k f =
|
|
|
|
case Map.lookup k m of
|
|
|
|
Just v -> f v
|
|
|
|
Nothing -> id
|
|
|
|
|
2024-11-28 01:32:27 +01:00
|
|
|
defaultItem :: Text -> Text -> Item
|
|
|
|
defaultItem txt color =
|
|
|
|
Item {
|
2021-04-29 11:52:44 +02:00
|
|
|
text = Text.filter notPunc txt,
|
|
|
|
title = toTitle txt,
|
2024-11-28 01:32:27 +01:00
|
|
|
size = 2,
|
|
|
|
stroke = 1.25,
|
2021-04-29 11:52:44 +02:00
|
|
|
width = 600,
|
2024-06-03 03:33:36 +02:00
|
|
|
color = color,
|
2021-04-29 11:52:44 +02:00
|
|
|
showText = True
|
|
|
|
}
|
|
|
|
|
|
|
|
split1 :: Text -> Text -> Maybe (Text, Text)
|
|
|
|
split1 s txt =
|
|
|
|
let (a, b) = Text.breakOn s txt in
|
|
|
|
if Text.null b then
|
|
|
|
Nothing
|
|
|
|
else
|
|
|
|
Just (Text.strip a, Text.strip $ Text.drop (Text.length s) b)
|
|
|
|
|
|
|
|
type Opts = Map Text Text
|
|
|
|
|
|
|
|
imageOpts :: Text -> Maybe (Text, Opts)
|
|
|
|
imageOpts = fmap (second splitOpts) . getOpts
|
|
|
|
|
|
|
|
getOpts :: Text -> Maybe (Text, Text)
|
|
|
|
getOpts = split1 "|"
|
|
|
|
|
|
|
|
splitOpts :: Text -> Map Text Text
|
|
|
|
splitOpts = Map.fromList . map splitOpt . Text.splitOn ";" where
|
|
|
|
splitOpt txt = fromMaybe ("file", txt) $ split1 "=" txt
|
|
|
|
|
|
|
|
toTitle :: Text -> Text
|
|
|
|
toTitle = Text.filter \c -> c /= '\\' && c /= '#'
|
|
|
|
|
2024-11-28 01:32:27 +01:00
|
|
|
makeItem :: Item -> Inline
|
|
|
|
makeItem (Item {..}) =
|
|
|
|
let env = Laantas.E {..}
|
|
|
|
words = Laantas.split text in
|
2024-11-30 20:03:19 +01:00
|
|
|
RawInline "html" $
|
|
|
|
Lazy.toStrict $ Lazy.dropAround isSpace $ Laantas.prettyText $
|
|
|
|
Laantas.doGlyphsNoDoctype words env `Laantas.with`
|
|
|
|
[Laantas.Class_ Laantas.<<- "scr"]
|
2021-04-29 11:52:44 +02:00
|
|
|
|
|
|
|
notPunc :: Char -> Bool
|
|
|
|
notPunc c = c `notElem` ("{}·" :: String)
|