git.iko.soy/typeable/pandoc
on master help sign in
MediaWiki reader: Images.
46be934John MacFarlane
src/Text/Pandoc/Readers/MediaWiki.hs+25−3
@@ -88,7 +88,7 @@ nested p = do
88 return res
88 return res
89
89
90specialChars :: [Char]
90specialChars :: [Char]
91−specialChars = "'[]<=&*{}"
91+specialChars = "'[]<=&*{}|"
92
92
93spaceChars :: [Char]
93spaceChars :: [Char]
94spaceChars = " \n\t"
94spaceChars = " \n\t"
@@ -318,6 +318,7 @@ inline = whitespace
318 <|> str
318 <|> str
319 <|> strong
319 <|> strong
320 <|> emph
320 <|> emph
321+ <|> image
321 <|> internalLink
322 <|> internalLink
322 <|> externalLink
323 <|> externalLink
323 <|> inlineTag
324 <|> inlineTag
@@ -374,9 +375,30 @@ endline = () <$ try (newline <*
374 notFollowedBy' hrule <*
375 notFollowedBy' hrule <*
375 notFollowedBy anyListStart)
376 notFollowedBy anyListStart)
376
377
378+image :: MWParser Inlines
379+image = try $ do
380+ sym "[["
381+ sym "File:"
382+ fname <- many1 (noneOf "|]")
383+ _ <- many (try $ char '|' *> imageOption)
384+ caption <- (B.str fname <$ sym "]]")
385+ <|> try (char '|' *> (mconcat <$> manyTill inline (sym "]]")))
386+ return $ B.image fname "image" caption
387+
388+imageOption :: MWParser String
389+imageOption =
390+ try (oneOfStrings [ "border", "thumbnail", "frameless"
391+ , "thumb", "upright", "left", "right"
392+ , "center", "none", "baseline", "sub"
393+ , "super", "top", "text-top", "middle"
394+ , "bottom", "text-bottom" ])
395+ <|> try (string "frame")
396+ <|> try (many1 (oneOf "x0123456789") <* string "px")
397+ <|> try (oneOfStrings ["link=","alt=","page=","class="] <* many (noneOf "|]"))
398+
377internalLink :: MWParser Inlines
399internalLink :: MWParser Inlines
378internalLink = try $ do
400internalLink = try $ do
379− string "[["
401+ sym "[["
380 let addUnderscores x = let (pref,suff) = break (=='#') x
402 let addUnderscores x = let (pref,suff) = break (=='#') x
381 in pref ++ intercalate "_" (words suff)
403 in pref ++ intercalate "_" (words suff)
382 pagename <- unwords . words <$> many (noneOf "|]")
404 pagename <- unwords . words <$> many (noneOf "|]")
@@ -385,7 +407,7 @@ internalLink = try $ do
385 -- the "pipe trick"
407 -- the "pipe trick"
386 -- [[Help:Contents|] -> "Contents"
408 -- [[Help:Contents|] -> "Contents"
387 <|> (return $ B.text $ drop 1 $ dropWhile (/=':') pagename) )
409 <|> (return $ B.text $ drop 1 $ dropWhile (/=':') pagename) )
388− string "]]"
410+ sym "]]"
389 linktrail <- B.text <$> many (char '\'' <|> letter)
411 linktrail <- B.text <$> many (char '\'' <|> letter)
390 return $ B.link (addUnderscores pagename) "wikilink" (label <> linktrail)
412 return $ B.link (addUnderscores pagename) "wikilink" (label <> linktrail)
391
413
tests/mediawiki-reader.native+5
@@ -82,6 +82,11 @@ Pandoc (Meta {docTitle = [], docAuthors = [], docDate = []})
82,Para [Link [Str "Contents"] ("Help:Contents","wikilink")]
82,Para [Link [Str "Contents"] ("Help:Contents","wikilink")]
83,Para [Link [Str "#My",Space,Str "anchor"] ("#My_anchor","wikilink")]
83,Para [Link [Str "#My",Space,Str "anchor"] ("#My_anchor","wikilink")]
84,Para [Link [Str "and",Space,Str "text"] ("Page#with_anchor","wikilink")]
84,Para [Link [Str "and",Space,Str "text"] ("Page#with_anchor","wikilink")]
85+,Header 2 [Str "images"]
86+,Para [Image [Str "caption"] ("example.jpg","image")]
87+,Para [Image [Str "the",Space,Emph [Str "caption"],Space,Str "with",Space,Link [Str "external",Space,Str "link"] ("http://google.com","")] ("example.jpg","image")]
88+,Para [Image [Str "caption"] ("example.jpg","image")]
89+,Para [Image [Str "example.jpg"] ("example.jpg","image")]
85,Header 2 [Str "lists"]
90,Header 2 [Str "lists"]
86,BulletList
91,BulletList
87 [[Plain [Str "Start",Space,Str "each",Space,Str "line"]]
92 [[Plain [Str "Start",Space,Str "each",Space,Str "line"]]
tests/mediawiki-reader.wiki+10
@@ -163,6 +163,16 @@ http://johnmacfarlane.net/pandoc/
163
163
164[[Page#with anchor|and text]]
164[[Page#with anchor|and text]]
165
165
166+== images ==
167+
168+[[File:example.jpg|caption]]
169+
170+[[File:example.jpg|border|the ''caption'' with [http://google.com external link]]]
171+
172+[[File:example.jpg|frameless|border|30x40px|caption]]
173+
174+[[File:example.jpg]]
175+
166== lists ==
176== lists ==
167
177
168* Start each line
178* Start each line