git.iko.soy/typeable/pandoc
on master help sign in
MediaWiki reader: Got alignments working.
26fb63eJohn MacFarlane
src/Text/Pandoc/Readers/MediaWiki.hs+12−6
@@ -190,15 +190,15 @@ table = do
190 caption <- option mempty tableCaption
190 caption <- option mempty tableCaption
191 optional rowsep
191 optional rowsep
192 hasheader <- option False $ True <$ (lookAhead (char '!'))
192 hasheader <- option False $ True <$ (lookAhead (char '!'))
193− hdr <- tableRow
193+ (aligns,hdr) <- unzip <$> tableRow
194− rows' <- many $ try $ rowsep *> tableRow
194+ rows' <- many $ try $ rowsep *> (map snd <$> tableRow)
195 tableEnd
195 tableEnd
196 -- TODO handle cellspecs from styles and aligns...
196 -- TODO handle cellspecs from styles and aligns...
197 let cols = length hdr
197 let cols = length hdr
198 let (headers,rows) = if hasheader
198 let (headers,rows) = if hasheader
199 then (hdr, rows')
199 then (hdr, rows')
200 else (replicate cols mempty, hdr:rows')
200 else (replicate cols mempty, hdr:rows')
201− let cellspecs = replicate cols (AlignDefault, 0.0)
201+ let cellspecs = zip aligns (repeat 0.0)
202 return $ B.table caption cellspecs headers rows
202 return $ B.table caption cellspecs headers rows
203
203
204parseAttrs :: String -> [(String,String)]
204parseAttrs :: String -> [(String,String)]
@@ -238,10 +238,10 @@ tableCaption = try $ do
238 res <- manyTill anyChar newline >>= parseFromString (many inline)
238 res <- manyTill anyChar newline >>= parseFromString (many inline)
239 return $ trimInlines $ mconcat res
239 return $ trimInlines $ mconcat res
240
240
241−tableRow :: MWParser [Blocks]
241+tableRow :: MWParser [(Alignment, Blocks)]
242tableRow = try $ many tableCell
242tableRow = try $ many tableCell
243
243
244−tableCell :: MWParser Blocks
244+tableCell :: MWParser (Alignment, Blocks)
245tableCell = try $ do
245tableCell = try $ do
246 cellsep
246 cellsep
247 skipMany spaceChar
247 skipMany spaceChar
@@ -249,7 +249,13 @@ tableCell = try $ do
249 manyTill (satisfy (/='\n')) (char '|' <* notFollowedBy (char '|'))
249 manyTill (satisfy (/='\n')) (char '|' <* notFollowedBy (char '|'))
250 skipMany spaceChar
250 skipMany spaceChar
251 ls <- many (notFollowedBy (cellsep <|> rowsep <|> tableEnd) *> anyChar)
251 ls <- many (notFollowedBy (cellsep <|> rowsep <|> tableEnd) *> anyChar)
252− parseFromString (mconcat <$> many block) ls
252+ bs <- parseFromString (mconcat <$> many block) ls
253+ let align = case lookup "align" attrs of
254+ Just "left" -> AlignLeft
255+ Just "right" -> AlignRight
256+ Just "center" -> AlignCenter
257+ _ -> AlignDefault
258+ return (align, bs)
253
259
254template :: MWParser String
260template :: MWParser String
255template = try $ do
261template = try $ do
tests/mediawiki-reader.native+11−1
@@ -213,4 +213,14 @@ Pandoc (Meta {docTitle = [], docAuthors = [], docDate = []})
213 ,[Para [Str "more"]]]
213 ,[Para [Str "more"]]]
214 ,[[Para [Str "Butter"]]
214 ,[[Para [Str "Butter"]]
215 ,[Para [Str "Ice",Space,Str "cream"]]
215 ,[Para [Str "Ice",Space,Str "cream"]]
216− ,[Para [Str "and",Space,Str "more"]]]]]
216+ ,[Para [Str "and",Space,Str "more"]]]]
217+,Table [] [AlignLeft,AlignRight,AlignCenter] [0.0,0.0,0.0]
218+ [[Para [Str "Left"]]
219+ ,[Para [Str "Right"]]
220+ ,[Para [Str "Center"]]]
221+ [[[Para [Str "left"]]
222+ ,[Para [Str "15.00"]]
223+ ,[Para [Str "centered"]]]
224+ ,[[Para [Str "more"]]
225+ ,[Para [Str "2.0"]]
226+ ,[Para [Str "more"]]]]]
tests/mediawiki-reader.wiki+10
@@ -330,3 +330,13 @@ and cheese
330| Butter || Ice cream || and more
330| Butter || Ice cream || and more
331|}
331|}
332
332
333+{|
334+! align="left"| Left
335+! align="right"|Right
336+! align="center"|Center
337+|-
338+| left || 15.00 || centered
339+|-
340+| more || 2.0 || more
341+|}
342+