module Text.Pandoc.Util.Filter
( attToString
, convertToStyle
, revealjsSpecialAttrs
, revealjsRewriteAttr
, classToRevealAttr
, toHtml
, toBlockHtml
, addToAtt
, addToStyle
)
where
import Text.Pandoc.Definition
import Data.Monoid
import Data.List (partition, isInfixOf)
addToAtt :: Eq a => a -> [a] -> [a]
addToAtt toAdd (a:as)
| a == toAdd = toAdd:as
| otherwise = a:addToAtt toAdd as
addToAtt toAdd [] = [toAdd]
addToStyle :: String -> [(String, String)] -> [(String, String)]
addToStyle toAdd (("style",val):as) = ("style", if toAdd `isInfixOf` val then val else val <> " " <> toAdd):as
addToStyle toAdd (a:as) = a:addToStyle toAdd as
addToStyle toAdd [] = [("style", toAdd)]
attToString :: Attr -> String
attToString ("", classes, kvpairs) = "class=\"" <> unwords classes <> "\" " <> unwords ((\(k,v) -> k <> "=\"" <> v <> "\"") <$> kvpairs')
where
kvpairs' = convertToStyle ["width","height","transform"] kvpairs
attToString (id', classes, kvpairs) = "id=\"" <> id' <> "\" class=\"" <> unwords classes <> "\" " <> unwords ((\(k,v) -> k <> "=\"" <> v <> "\"") <$> kvpairs')
where
kvpairs' = convertToStyle ["width","height","transform"] kvpairs
convertToStyle :: [String] -> [(String,String)] -> [(String,String)]
convertToStyle keys kvpairs = ("style", newstyle):rest
where
oldstyle = case filter (\(k,_) -> k == "style") kvpairs of
[(_,st)] -> st
_ -> ""
stylesToAdd = filter (\(k,_) -> k `elem` keys) kvpairs
rest = filter (\(k,_) -> k `notElem` ("style":keys)) kvpairs
newstyle = concat ((\(k,v) -> k <> ":" <> v <> ";") <$> stylesToAdd) <> oldstyle
revealjsSpecialAttrs :: [String]
revealjsSpecialAttrs =
[ "data-markdown"
, "data-timing"
, "data-template"
, "data-autoplay"
, "data-prevent-swipe"
, "data-background-interactive"
, "data-trim"
, "data-noescape"
, "data-ignore"
, "controls"
]
classToRevealAttr :: [String] -> ([String],[String])
classToRevealAttr = partition (`elem` revealjsSpecialAttrs)
revealjsRewriteAttr :: [String] -> [String]
revealjsRewriteAttr = fmap replace
where
replace :: String -> String
replace a = case filter ((==a) . fst) replacements of
[(_,b)] -> b
_ -> a
replacements :: [(String, String)]
replacements = [ ("autoplay", "data-autoplay")
]
toHtml :: String -> Inline
toHtml = RawInline (Format "html")
toBlockHtml :: String -> Block
toBlockHtml = RawBlock (Format "html")