Skip to content

Commit

Permalink
Allow to wrap embedded video in a div
Browse files Browse the repository at this point in the history
Set environment variable VIDEO_WRAPPER_CSS_CLASS="your-css-class" and
the iframe will be embeded in a div with the provided class.
  • Loading branch information
unmanbearpig committed Jul 30, 2019
1 parent 887ff4e commit 04b071d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
12 changes: 7 additions & 5 deletions src/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
16 changes: 16 additions & 0 deletions src/WrapperElement.hs
Original file line number Diff line number Diff line change
@@ -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 =
"<div class=\"" <> (LT.pack cssClass) <> "\">" <> innerText <> "</div>"
16 changes: 12 additions & 4 deletions test/Spec.hs
Original file line number Diff line number Diff line change
@@ -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")
"<iframe allowfullscreen mozallowfullscreen frameborder=\"0\" src=\"https://player.vimeo.com/video/123?title=0&amp;amp;portrait=0&amp;amp;badge=0\" webkitallowfullscreen></iframe>")
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")
"<iframe height=\"20\" width=\"10\" allowfullscreen mozallowfullscreen frameborder=\"0\" src=\"https://player.vimeo.com/video/123?title=0&amp;amp;portrait=0&amp;amp;badge=0\" webkitallowfullscreen></iframe>")
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")
"<iframe height=\"50\" width=\"40\" allowfullscreen mozallowfullscreen frameborder=\"0\" src=\"https://player.vimeo.com/video/123?title=0&amp;amp;portrait=0&amp;amp;badge=0\" webkitallowfullscreen></iframe>")
it "wraps it in a div" $ do
inlineHtmlVideo Nothing (DivWithClass "class1 class2") (Image nullAttr [] ("vimeo:123", ""))
`shouldBe` (Just $
RawInline (Format "html")
"<div class=\"class1 class2\"><iframe allowfullscreen mozallowfullscreen frameborder=\"0\" src=\"https://player.vimeo.com/video/123?title=0&amp;amp;portrait=0&amp;amp;badge=0\" webkitallowfullscreen></iframe></div>")
it "doesn't wrap when no video" $ do
inlineHtmlVideo Nothing (DivWithClass "testclass") (Str "hello") `shouldBe` (Nothing)
1 change: 1 addition & 0 deletions videofilter.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ library
, VideoParser
, Definitions
, VideoRenderer
, WrapperElement
build-depends: base >= 4.7 && < 5
, lucid
, pandoc
Expand Down

0 comments on commit 04b071d

Please sign in to comment.