From 77a40d6f341933b136c6ddd54f2568152b4b31ef Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Tue, 27 Nov 2018 14:45:39 -0800
Subject: [PATCH] MediaWiki writer: fix caption, use 'thumb' instead of
 'frame'.

Captions used to have the word 'caption' prepended; this
has been removed.

Also, 'thumb' is used instead of 'frame' to allow images
to be resized.

Closes #5105.
---
 src/Text/Pandoc/Writers/MediaWiki.hs | 50 ++++++++++++++++++----------
 test/writer.mediawiki                |  2 +-
 2 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/src/Text/Pandoc/Writers/MediaWiki.hs b/src/Text/Pandoc/Writers/MediaWiki.hs
index 666853a3c..dfc1eeef5 100644
--- a/src/Text/Pandoc/Writers/MediaWiki.hs
+++ b/src/Text/Pandoc/Writers/MediaWiki.hs
@@ -109,14 +109,24 @@ blockToMediaWiki (Plain inlines) =
 
 -- title beginning with fig: indicates that the image is a figure
 blockToMediaWiki (Para [Image attr txt (src,'f':'i':'g':':':tit)]) = do
-  capt <- if null txt
-             then return ""
-             else ("|caption " ++) `fmap` inlineListToMediaWiki txt
+  capt <- inlineListToMediaWiki txt
   img  <- imageToMediaWiki attr
-  let opt = if null txt
-               then ""
-               else "|alt=" ++ if null tit then capt else tit ++ capt
-  return $ "[[File:" ++ src ++ "|frame|none" ++ img ++ opt ++ "]]\n"
+  let opt = if null tit
+               then
+                 if null capt
+                    then ""
+                    else "alt=" ++ capt
+               else "alt=" ++ tit
+  return $ "[[" ++
+            intercalate "|"
+            (filter (not . null) ["File:" ++ src
+                                 , "thumb"
+                                 , "none"
+                                 , img
+                                 , opt
+                                 , capt
+                                 ]) ++
+            "]]\n"
 
 blockToMediaWiki (Para inlines) = do
   tags <- asks useTags
@@ -331,15 +341,15 @@ imageToMediaWiki attr = do
       toPx = fmap (showInPixel opts) . checkPct
       checkPct (Just (Percent _)) = Nothing
       checkPct maybeDim           = maybeDim
-      go (Just w) Nothing  = '|':w ++ "px"
-      go (Just w) (Just h) = '|':w ++ "x" ++ h ++ "px"
-      go Nothing  (Just h) = "|x" ++ h ++ "px"
+      go (Just w) Nothing  = w ++ "px"
+      go (Just w) (Just h) = w ++ "x" ++ h ++ "px"
+      go Nothing  (Just h) = "x" ++ h ++ "px"
       go Nothing  Nothing  = ""
       dims = go (toPx $ dimension Width attr) (toPx $ dimension Height attr)
       classes = if null cls
                    then ""
-                   else "|class=" ++ unwords cls
-  return $ dims ++ classes
+                   else "class=" ++ unwords cls
+  return $ intercalate "|" $ filter (not . null) [dims, classes]
 
 -- | Convert list of Pandoc block elements to MediaWiki.
 blockListToMediaWiki :: PandocMonad m
@@ -436,12 +446,18 @@ inlineToMediaWiki (Link _ txt (src, _)) = do
 inlineToMediaWiki (Image attr alt (source, tit)) = do
   img  <- imageToMediaWiki attr
   alt' <- inlineListToMediaWiki alt
-  let txt = if null tit
-               then if null alt
+  let txt = if null alt'
+               then if null tit
                        then ""
-                       else '|' : alt'
-               else '|' : tit
-  return $ "[[File:" ++ source ++ img ++ txt ++ "]]"
+                       else tit
+               else alt'
+  return $ "[[" ++
+           intercalate "|"
+           (filter (not . null)
+            [ "File:" ++ source
+            , img
+            , txt
+            ]) ++ "]]"
 
 inlineToMediaWiki (Note contents) = do
   contents' <- blockListToMediaWiki contents
diff --git a/test/writer.mediawiki b/test/writer.mediawiki
index 968eef388..29686f3e7 100644
--- a/test/writer.mediawiki
+++ b/test/writer.mediawiki
@@ -620,7 +620,7 @@ Auto-links should not occur here: <code>&lt;http://example.com/&gt;</code>
 
 From “Voyage dans la Lune” by Georges Melies (1902):
 
-[[File:lalune.jpg|frame|none|alt=Voyage dans la Lune|caption lalune]]
+[[File:lalune.jpg|thumb|none|alt=Voyage dans la Lune|lalune]]
 
 Here is a movie [[File:movie.jpg|movie]] icon.