From 26fb63e2946da932662127bddde5005048f10855 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sat, 15 Sep 2012 14:20:50 -0400
Subject: [PATCH] MediaWiki reader: Got alignments working.

They only work on headers, because pandoc forces same
alignment for all cells in a column.
---
 src/Text/Pandoc/Readers/MediaWiki.hs | 18 ++++++++++++------
 tests/mediawiki-reader.native        | 12 +++++++++++-
 tests/mediawiki-reader.wiki          | 10 ++++++++++
 3 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/src/Text/Pandoc/Readers/MediaWiki.hs b/src/Text/Pandoc/Readers/MediaWiki.hs
index 751326bb6..0c2566faf 100644
--- a/src/Text/Pandoc/Readers/MediaWiki.hs
+++ b/src/Text/Pandoc/Readers/MediaWiki.hs
@@ -190,15 +190,15 @@ table = do
   caption <- option mempty tableCaption
   optional rowsep
   hasheader <- option False $ True <$ (lookAhead (char '!'))
-  hdr <- tableRow
-  rows' <- many $ try $ rowsep *> tableRow
+  (aligns,hdr) <- unzip <$> tableRow
+  rows' <- many $ try $ rowsep *> (map snd <$> tableRow)
   tableEnd
   -- TODO handle cellspecs from styles and aligns...
   let cols = length hdr
   let (headers,rows) = if hasheader
                           then (hdr, rows')
                           else (replicate cols mempty, hdr:rows')
-  let cellspecs = replicate cols (AlignDefault, 0.0)
+  let cellspecs = zip aligns (repeat 0.0)
   return $ B.table caption cellspecs headers rows
 
 parseAttrs :: String -> [(String,String)]
@@ -238,10 +238,10 @@ tableCaption = try $ do
   res <- manyTill anyChar newline >>= parseFromString (many inline)
   return $ trimInlines $ mconcat res
 
-tableRow :: MWParser [Blocks]
+tableRow :: MWParser [(Alignment, Blocks)]
 tableRow = try $ many tableCell
 
-tableCell :: MWParser Blocks
+tableCell :: MWParser (Alignment, Blocks)
 tableCell = try $ do
   cellsep
   skipMany spaceChar
@@ -249,7 +249,13 @@ tableCell = try $ do
        manyTill (satisfy (/='\n')) (char '|' <* notFollowedBy (char '|'))
   skipMany spaceChar
   ls <- many (notFollowedBy (cellsep <|> rowsep <|> tableEnd) *> anyChar)
-  parseFromString (mconcat <$> many block) ls
+  bs <- parseFromString (mconcat <$> many block) ls
+  let align = case lookup "align" attrs of
+                    Just "left"   -> AlignLeft
+                    Just "right"  -> AlignRight
+                    Just "center" -> AlignCenter
+                    _             -> AlignDefault
+  return (align, bs)
 
 template :: MWParser String
 template = try $ do
diff --git a/tests/mediawiki-reader.native b/tests/mediawiki-reader.native
index 25c0eaa72..3ca7b6b79 100644
--- a/tests/mediawiki-reader.native
+++ b/tests/mediawiki-reader.native
@@ -213,4 +213,14 @@ Pandoc (Meta {docTitle = [], docAuthors = [], docDate = []})
   ,[Para [Str "more"]]]
  ,[[Para [Str "Butter"]]
   ,[Para [Str "Ice",Space,Str "cream"]]
-  ,[Para [Str "and",Space,Str "more"]]]]]
+  ,[Para [Str "and",Space,Str "more"]]]]
+,Table [] [AlignLeft,AlignRight,AlignCenter] [0.0,0.0,0.0]
+ [[Para [Str "Left"]]
+ ,[Para [Str "Right"]]
+ ,[Para [Str "Center"]]]
+ [[[Para [Str "left"]]
+  ,[Para [Str "15.00"]]
+  ,[Para [Str "centered"]]]
+ ,[[Para [Str "more"]]
+  ,[Para [Str "2.0"]]
+  ,[Para [Str "more"]]]]]
diff --git a/tests/mediawiki-reader.wiki b/tests/mediawiki-reader.wiki
index 154248e54..10deebec8 100644
--- a/tests/mediawiki-reader.wiki
+++ b/tests/mediawiki-reader.wiki
@@ -330,3 +330,13 @@ and cheese
 |   Butter   || Ice cream ||  and more
 |}
 
+{|
+! align="left"| Left
+! align="right"|Right
+! align="center"|Center
+|-
+| left || 15.00 || centered
+|-
+| more || 2.0 || more
+|}
+