{-# LANGUAGE ScopedTypeVariables #-}
module Text.Pandoc.Util.Filter.Quiz
(quiz)
where
import Text.Pandoc.JSON
import Text.Pandoc.Walk
import Data.Monoid ((<>))
import Data.Maybe (isNothing, mapMaybe, listToMaybe)
quiz :: Block -> [Block]
quiz pb@(Plain b) = fmap makeQuiz (query findQuiz pb) <> [Plain (filter ((==) [] . findQuiz) b)]
quiz pb@(Para b) = fmap makeQuiz (query findQuiz pb) <> [Plain (filter ((==) [] . findQuiz) b)]
quiz x = [x]
findQuiz :: Inline -> [(Attr, [Inline], Maybe ([Inline],Attr))]
findQuiz (Span attributes@(_, att, _) answerText)
| "answer" `elem` att = [(attributes, answerText', tooltip)]
where
answerText' = filter (isNothing . findTooltip) answerText
tooltip = listToMaybe $ mapMaybe findTooltip answerText
findQuiz _ = []
findTooltip :: Inline -> Maybe ([Inline],Attr)
findTooltip (Span attr@(_,att,_) tooltipText)
| "tooltip" `elem` att = Just (tooltipText, attr)
findTooltip _ = Nothing
makeQuiz :: (Attr, [Inline], Maybe ([Inline],Attr)) -> Block
makeQuiz (att, answer, Nothing) = Div att [Plain answer]
makeQuiz (att, answer, Just (tooltip,a)) = Div att [Plain answer, Div a [Plain tooltip]]