git.iko.soy/typeable/pandoc
on master help sign in
HTML writer: output video and audio elements

depending on file extension of the image path

commita58304e00edc3c7d78c352c379e843cb908e6887 authormb21 <mb21@users.noreply.github.com> dateSat 11 May 2019, 18:54 (7 years ago) parent47249b0
8 files changed+52−37
src/Text/Pandoc/MIME.hs+6−2 src/Text/Pandoc/Writers/HTML.hs+22−30 test/command/2662.md+1−1 test/command/3450.md+1−1 test/command/4012.md+1−1 test/command/4677.md+1−1 test/command/5121.md+1−1 test/command/video-audio.md+19
src/Text/Pandoc/MIME.hs+6−2
@@ -55,9 +55,13 @@ reverseMimeTypes = M.fromList $ map (\(k,v) -> (v,k)) mimeTypesList
5555mimeTypes :: M.Map String MimeType
5656mimeTypes = M.fromList mimeTypesList
5757
58+-- | Collection of common mime types.
59+-- Except for first entry, list borrowed from
60+-- <https://github.com/Happstack/happstack-server/blob/master/src/Happstack/Server/FileServe/BuildingBlocks.hs happstack-server>
5861mimeTypesList :: [(String, MimeType)]
59−mimeTypesList = -- List borrowed from happstack-server.
60− [("gz","application/x-gzip")
62+mimeTypesList =
63+ [("cpt","image/x-corelphotopaint")
64+ ,("gz","application/x-gzip")
6165 ,("cabal","application/x-cabal")
6266 ,("%","application/x-trash")
6367 ,("323","text/h323")
src/Text/Pandoc/Writers/HTML.hs+22−30
@@ -65,18 +65,21 @@ import Text.Blaze.Internal (preEscapedString, preEscapedText)
6565#endif
6666#if MIN_VERSION_blaze_html(0,5,1)
6767import qualified Text.Blaze.XHtml5 as H5
68+import qualified Text.Blaze.XHtml5.Attributes as A5
6869#else
6970import qualified Text.Blaze.Html5 as H5
71+import qualified Text.Blaze.Html5.Attributes as A5
7072#endif
7173import Control.Monad.Except (throwError)
7274import Data.Aeson (Value)
73−import System.FilePath (takeBaseName, takeExtension)
75+import System.FilePath (takeBaseName)
7476import Text.Blaze.Html.Renderer.Text (renderHtml)
7577import qualified Text.Blaze.XHtml1.Transitional as H
7678import qualified Text.Blaze.XHtml1.Transitional.Attributes as A
7779import Text.Pandoc.Class (PandocMonad, report, runPure)
7880import Text.Pandoc.Error
7981import Text.Pandoc.Logging
82+import Text.Pandoc.MIME (mediaCategory)
8083import Text.TeXMath
8184import Text.XML.Light (elChildren, unode, unqual)
8285import qualified Text.XML.Light as XML
@@ -665,18 +668,6 @@ dimensionsToAttrList attr = consolidateStyles $ go Width ++ go Height
665668 (Just x) -> [("style", show dir ++ ":" ++ show x)]
666669 Nothing -> []
667670
668−imageExts :: [String]
669−imageExts = [ "art", "bmp", "cdr", "cdt", "cpt", "cr2", "crw", "djvu", "erf",
670− "gif", "ico", "ief", "jng", "jpg", "jpeg", "nef", "orf", "pat", "pbm",
671− "pcx", "pgm", "png", "pnm", "ppm", "psd", "ras", "rgb", "svg", "tiff",
672− "wbmp", "xbm", "xpm", "xwd" ]
673−
674−treatAsImage :: FilePath -> Bool
675−treatAsImage fp =
676− let path = maybe fp uriPath (parseURIReference fp)
677− ext = map toLower $ drop 1 $ takeExtension path
678− in null ext || ext `elem` imageExts
679−
680671figure :: PandocMonad m
681672 => WriterOptions -> Attr -> [Inline] -> (String, String)
682673 -> StateT WriterState m Html
@@ -1135,33 +1126,34 @@ inlineToHtml opts inline = do
11351126 return $ if null tit
11361127 then link'
11371128 else link' ! A.title (toValue tit)
1138− (Image attr txt (s,tit)) | treatAsImage s -> do
1139− let alternate' = stringify txt
1129+ (Image attr txt (s,tit)) -> do
1130+ let alternate = stringify txt
11401131 slideVariant <- gets stSlideVariant
11411132 let isReveal = slideVariant == RevealJsSlides
11421133 attrs <- imgAttrsToHtml opts attr
11431134 let attributes =
11441135 -- reveal.js uses data-src for lazy loading
1145− (if isReveal
1146− then customAttribute "data-src" $ toValue s
1147− else A.src $ toValue s) :
1148− [A.title $ toValue tit | not (null tit)] ++
1149− [A.alt $ toValue alternate' | not (null txt)] ++
1150− attrs
1151− let tag = if html5 then H5.img else H.img
1152− return $ foldl (!) tag attributes
1153− -- note: null title included, as in Markdown.pl
1154− (Image attr _ (s,tit)) -> do
1155− slideVariant <- gets stSlideVariant
1156− let isReveal = slideVariant == RevealJsSlides
1157− attrs <- imgAttrsToHtml opts attr
1158− let attributes =
11591136 (if isReveal
11601137 then customAttribute "data-src" $ toValue s
11611138 else A.src $ toValue s) :
11621139 [A.title $ toValue tit | not (null tit)] ++
11631140 attrs
1164− return $ foldl (!) H5.embed attributes
1141+ imageTag = (if html5 then H5.img else H.img
1142+ , [A.alt $ toValue alternate | not (null txt)] )
1143+ mediaTag tg fallbackTxt =
1144+ let linkTxt = if null txt
1145+ then fallbackTxt
1146+ else alternate
1147+ in (tg $ H.a ! A.href (toValue s) $ toHtml linkTxt
1148+ , [A5.controls ""] )
1149+ normSrc = maybe s uriPath (parseURIReference s)
1150+ (tag, specAttrs) = case mediaCategory normSrc of
1151+ Just "image" -> imageTag
1152+ Just "video" -> mediaTag H5.video "Video"
1153+ Just "audio" -> mediaTag H5.audio "Audio"
1154+ Just _ -> (H5.embed, [])
1155+ _ -> imageTag
1156+ return $ foldl (!) tag $ attributes ++ specAttrs
11651157 -- note: null title included, as in Markdown.pl
11661158 (Note contents) -> do
11671159 notes <- gets stNotes
test/command/2662.md+1−1
@@ -7,5 +7,5 @@
77 :scale: 300 %
88 :alt: alternate text
99^D
10−<p><img src="http://url.to.image/foo.png" alt="alternate text" class="align-left" width="600" height="300" /></p>
10+<p><img src="http://url.to.image/foo.png" class="align-left" width="600" height="300" alt="alternate text" /></p>
1111```
test/command/3450.md+1−1
@@ -2,7 +2,7 @@
22% pandoc -fmarkdown-implicit_figures
33![image](lalune.jpg){height=2em}
44^D
5−<p><img src="lalune.jpg" alt="image" style="height:2em" /></p>
5+<p><img src="lalune.jpg" style="height:2em" alt="image" /></p>
66```
77```
88% pandoc -fmarkdown-implicit_figures -t latex
test/command/4012.md+1−1
@@ -4,5 +4,5 @@ pandoc -f markdown-implicit_figures
44
55[image]: http://example.com/image.jpg {height=35mm}
66^D
7−<p><img src="http://example.com/image.jpg" alt="image" style="height:35mm" /></p>
7+<p><img src="http://example.com/image.jpg" style="height:35mm" alt="image" /></p>
88```
test/command/4677.md+1−1
@@ -3,6 +3,6 @@
33![Caption](img.png){#img:1}
44^D
55<figure>
6−<img src="img.png" alt="" id="img:1" /><figcaption>Caption</figcaption>
6+<img src="img.png" id="img:1" alt="" /><figcaption>Caption</figcaption>
77</figure>
88```
test/command/5121.md+1−1
@@ -5,7 +5,7 @@
55## Header 2
66^D
77<figure>
8−<img src="./my-figure.jpg" alt="" width="500" /><figcaption>My caption</figcaption>
8+<img src="./my-figure.jpg" width="500" alt="" /><figcaption>My caption</figcaption>
99</figure>
1010
1111Header 2
test/command/video-audio.mdnew file+19
@@ -0,0 +1,19 @@
1+```
2+% pandoc -f markdown-implicit_figures -t html
3+![](./test.mp4)
4+
5+![Your browser does not support video.](foo/test.webm){width=300}
6+
7+![](test.mp3)
8+
9+![](./test.pdf)
10+
11+![](./test.jpg)
12+^D
13+<p><video src="./test.mp4" controls=""><a href="./test.mp4">Video</a></video></p>
14+<p><video src="foo/test.webm" width="300" controls=""><a href="foo/test.webm">Your browser does not support video.</a></video></p>
15+<p><audio src="test.mp3" controls=""><a href="test.mp3">Audio</a></audio></p>
16+<p><embed src="./test.pdf" /></p>
17+<p><img src="./test.jpg" /></p>
18+```
19+