diff --git a/src/Lib.hs b/src/Lib.hs index 5685819..8baec97 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -17,16 +17,17 @@ import Text.Pandoc.Walk (query) import Data.Maybe (fromMaybe, mapMaybe) import System.Environment (lookupEnv) import Control.Applicative ((<|>)) +import WrapperElement (WrapperElement(..), wrapWithElement, wrapperElementFromMaybeString) extractDimensions :: [Inline] -> Maybe Dimensions extractDimensions = fmap fst <$> uncons . mapMaybe (parseDimensions . query (\(Str s) -> s)) -inlineHtmlVideo :: Maybe Dimensions -> Inline -> Maybe Inline -inlineHtmlVideo defaultDimensions (Image _ inlines (url, _)) = - rawHtml . renderVideoEmbed . (`Video` dimensions) <$> parseVideoId url +inlineHtmlVideo :: Maybe Dimensions -> WrapperElement -> Inline -> Maybe Inline +inlineHtmlVideo defaultDimensions wrapperEl (Image _ inlines (url, _)) = + rawHtml . wrapWithElement wrapperEl . renderVideoEmbed . (`Video` dimensions) <$> parseVideoId url where rawHtml = RawInline (Format "html") . LT.unpack dimensions = extractDimensions inlines <|> defaultDimensions -inlineHtmlVideo _ _ = Nothing +inlineHtmlVideo _ _ _ = Nothing onFormat :: Format -> (a -> Maybe a) -> Maybe Format -> a -> a onFormat format f maybeFormat x @@ -36,4 +37,5 @@ onFormat format f maybeFormat x process :: IO () process = do defaultDimensions <- lookupEnv "VIDEO_DIMENSIONS" - toJSONFilter $ onFormat (Format "html") (inlineHtmlVideo (defaultDimensions >>= parseDimensions)) + wrapperElementStr <- lookupEnv "VIDEO_WRAPPER_CSS_CLASS" + toJSONFilter $ onFormat (Format "html") (inlineHtmlVideo (defaultDimensions >>= parseDimensions) (wrapperElementFromMaybeString wrapperElementStr)) diff --git a/src/WrapperElement.hs b/src/WrapperElement.hs new file mode 100644 index 0000000..50f24fe --- /dev/null +++ b/src/WrapperElement.hs @@ -0,0 +1,16 @@ +{-# LANGUAGE OverloadedStrings #-} + +module WrapperElement where + +import qualified Data.Text.Lazy as LT + +data WrapperElement = DivWithClass String | NoWrapper + +wrapperElementFromMaybeString :: Maybe String -> WrapperElement +wrapperElementFromMaybeString Nothing = NoWrapper +wrapperElementFromMaybeString (Just str) = DivWithClass str + +wrapWithElement :: WrapperElement -> LT.Text -> LT.Text +wrapWithElement NoWrapper innerText = innerText +wrapWithElement (DivWithClass cssClass) innerText = + "
(LT.pack cssClass) <> "\">" <> innerText <> "
" diff --git a/test/Spec.hs b/test/Spec.hs index 991e685..e727fd3 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -1,24 +1,32 @@ import Test.Hspec import Text.Pandoc.Definition (Format(..), Inline(..), Attr(), nullAttr) +import WrapperElement (WrapperElement(..)) import Lib (inlineHtmlVideo) main :: IO () main = hspec $ do describe "inlneHTMLVideo" $ do it "returns Nothing for non-image tag" $ do - inlineHtmlVideo Nothing (Str "hello") `shouldBe` (Nothing) + inlineHtmlVideo Nothing NoWrapper (Str "hello") `shouldBe` (Nothing) it "returns Vimeo iframe for correctly formatted image tag" $ do - inlineHtmlVideo Nothing (Image nullAttr [] ("vimeo:123", "")) + inlineHtmlVideo Nothing NoWrapper (Image nullAttr [] ("vimeo:123", "")) `shouldBe` (Just $ RawInline (Format "html") "") it "uses provided default dimensions" $ do - inlineHtmlVideo (Just (10, 20)) (Image nullAttr [] ("vimeo:123", "")) + inlineHtmlVideo (Just (10, 20)) NoWrapper (Image nullAttr [] ("vimeo:123", "")) `shouldBe` (Just $ RawInline (Format "html") "") it "overrides default dimensions" $ do - inlineHtmlVideo Nothing (Image nullAttr [(Str "40x50")] ("vimeo:123", "")) + inlineHtmlVideo Nothing NoWrapper (Image nullAttr [(Str "40x50")] ("vimeo:123", "")) `shouldBe` (Just $ RawInline (Format "html") "") + it "wraps it in a div" $ do + inlineHtmlVideo Nothing (DivWithClass "class1 class2") (Image nullAttr [] ("vimeo:123", "")) + `shouldBe` (Just $ + RawInline (Format "html") + "
") + it "doesn't wrap when no video" $ do + inlineHtmlVideo Nothing (DivWithClass "testclass") (Str "hello") `shouldBe` (Nothing) diff --git a/videofilter.cabal b/videofilter.cabal index 9f97853..827d7a7 100644 --- a/videofilter.cabal +++ b/videofilter.cabal @@ -19,6 +19,7 @@ library , VideoParser , Definitions , VideoRenderer + , WrapperElement build-depends: base >= 4.7 && < 5 , lucid , pandoc