git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: properly handle column prefixes/suffixes.
cfdbe85John MacFarlane
src/Text/Pandoc/Readers/LaTeX.hs+33−12
@@ -47,7 +47,7 @@ import Text.Pandoc.Builder
47import Control.Applicative ((<|>), many, optional)
47import Control.Applicative ((<|>), many, optional)
48import Data.Maybe (fromMaybe, maybeToList)
48import Data.Maybe (fromMaybe, maybeToList)
49import System.FilePath (replaceExtension, takeExtension, addExtension)
49import System.FilePath (replaceExtension, takeExtension, addExtension)
50−import Data.List (intercalate)
50+import Data.List (intercalate, unzip3)
51import qualified Data.Map as M
51import qualified Data.Map as M
52import Text.Pandoc.Highlighting (fromListingsLanguage, languagesByExtension)
52import Text.Pandoc.Highlighting (fromListingsLanguage, languagesByExtension)
53import Text.Pandoc.ImageSize (numUnit, showFl)
53import Text.Pandoc.ImageSize (numUnit, showFl)
@@ -1324,7 +1324,7 @@ complexNatbibCitation mode = try $ do
1324
1324
1325-- tables
1325-- tables
1326
1326
1327−parseAligns :: PandocMonad m => LP m [Alignment]
1327+parseAligns :: PandocMonad m => LP m [(String, Alignment, String)]
1328parseAligns = try $ do
1328parseAligns = try $ do
1329 char '{'
1329 char '{'
1330 let maybeBar = skipMany $ sp <|> () <$ char '|' <|> () <$ (char '@' >> braced)
1330 let maybeBar = skipMany $ sp <|> () <$ char '|' <|> () <$ (char '@' >> braced)
@@ -1334,11 +1334,21 @@ parseAligns = try $ do
1334 let rAlign = AlignRight <$ char 'r'
1334 let rAlign = AlignRight <$ char 'r'
1335 let parAlign = AlignLeft <$ (char 'p' >> braced)
1335 let parAlign = AlignLeft <$ (char 'p' >> braced)
1336 let alignChar = cAlign <|> lAlign <|> rAlign <|> parAlign
1336 let alignChar = cAlign <|> lAlign <|> rAlign <|> parAlign
1337+ let alignPrefix = char '>' >> braced
1338+ let alignSuffix = char '<' >> braced
1339+ let alignSpec = do
1340+ spaces
1341+ pref <- option "" alignPrefix
1342+ spaces
1343+ ch <- alignChar
1344+ spaces
1345+ suff <- option "" alignSuffix
1346+ return (pref, ch, suff)
1337− aligns' <- sepEndBy alignChar maybeBar
1347+ aligns' <- sepEndBy alignSpec maybeBar
1338 spaces
1348 spaces
1339 char '}'
1349 char '}'
1340 spaces
1350 spaces
1341− return aligns'
1351+ return $ aligns'
1342
1352
1343hline :: PandocMonad m => LP m ()
1353hline :: PandocMonad m => LP m ()
1344hline = try $ do
1354hline = try $ do
@@ -1362,16 +1372,25 @@ lbreak = () <$ try (spaces' *>
1362amp :: PandocMonad m => LP m ()
1372amp :: PandocMonad m => LP m ()
1363amp = () <$ try (spaces' *> char '&' <* spaces')
1373amp = () <$ try (spaces' *> char '&' <* spaces')
1364
1374
1365−parseTableRow :: PandocMonad m => Int -- ^ number of columns
1375+parseTableRow :: PandocMonad m
1376+ => Int -- ^ number of columns
1377+ -> [String] -- ^ prefixes
1378+ -> [String] -- ^ suffixes
1366 -> LP m [Blocks]
1379 -> LP m [Blocks]
1367−parseTableRow cols = try $ do
1380+parseTableRow cols prefixes suffixes = try $ do
1368− let tableCellInline = notFollowedBy (amp <|> lbreak) >> inline
1381+ let tableCellRaw = many (notFollowedBy
1382+ (amp <|> lbreak <|>
1383+ (() <$ try (string "\\end"))) >> anyChar)
1369 let minipage = try $ controlSeq "begin" *> string "{minipage}" *>
1384 let minipage = try $ controlSeq "begin" *> string "{minipage}" *>
1370 env "minipage"
1385 env "minipage"
1371 (skipopts *> spaces' *> optional braced *> spaces' *> blocks)
1386 (skipopts *> spaces' *> optional braced *> spaces' *> blocks)
1372 let tableCell = minipage <|>
1387 let tableCell = minipage <|>
1373− ((plain . trimInlines . mconcat) <$> many tableCellInline)
1388+ ((plain . trimInlines . mconcat) <$> many inline)
1374− cells' <- sepBy1 tableCell amp
1389+ rawcells <- sepBy1 tableCellRaw amp
1390+ guard $ length rawcells == cols
1391+ let rawcells' = zipWith3 (\c p s -> p ++ trim c ++ s)
1392+ rawcells prefixes suffixes
1393+ cells' <- mapM (parseFromString tableCell) rawcells'
1375 let numcells = length cells'
1394 let numcells = length cells'
1376 guard $ numcells <= cols && numcells >= 1
1395 guard $ numcells <= cols && numcells >= 1
1377 guard $ cells' /= [mempty]
1396 guard $ cells' /= [mempty]
@@ -1387,16 +1406,18 @@ simpTable :: PandocMonad m => Bool -> LP m Blocks
1387simpTable hasWidthParameter = try $ do
1406simpTable hasWidthParameter = try $ do
1388 when hasWidthParameter $ () <$ (spaces' >> tok)
1407 when hasWidthParameter $ () <$ (spaces' >> tok)
1389 skipopts
1408 skipopts
1390− aligns <- parseAligns
1409+ (prefixes, aligns, suffixes) <- unzip3 <$> parseAligns
1391 let cols = length aligns
1410 let cols = length aligns
1392 optional $ controlSeq "caption" *> skipopts *> setCaption
1411 optional $ controlSeq "caption" *> skipopts *> setCaption
1393 optional lbreak
1412 optional lbreak
1394 spaces'
1413 spaces'
1395 skipMany hline
1414 skipMany hline
1396 spaces'
1415 spaces'
1397− header' <- option [] $ try (parseTableRow cols <* lbreak <* many1 hline)
1416+ header' <- option [] $ try (parseTableRow cols prefixes suffixes <*
1417+ lbreak <* many1 hline)
1398 spaces'
1418 spaces'
1419+ rows <- sepEndBy (parseTableRow cols prefixes suffixes)
1399− rows <- sepEndBy (parseTableRow cols) (lbreak <* optional (skipMany hline))
1420+ (lbreak <* optional (skipMany hline))
1400 spaces'
1421 spaces'
1401 optional $ controlSeq "caption" *> skipopts *> setCaption
1422 optional $ controlSeq "caption" *> skipopts *> setCaption
1402 optional lbreak
1423 optional lbreak
test/command/latex-tabular-column-specs.mdnew file+24
@@ -0,0 +1,24 @@
1+See https://groups.google.com/forum/#!topic/pandoc-discuss/_VXtqihCyDU.
2+
3+```
4+% pandoc -f latex -t native
5+\begin{tabular}{>{$}l<{$}>{$}l<{$} >{$}l<{$}}
6+\toprule
7+& f1 & f2 \\
8+\midrule
9+e & 0.5 & 4 \\
10+f & 0.5 & 5,5 \\
11+\bottomrule
12+\end{tabular}
13+^D
14+[Table [] [AlignLeft,AlignLeft,AlignLeft] [0.0,0.0,0.0]
15+ [[Plain [Math InlineMath ""]]
16+ ,[Plain [Math InlineMath "f1"]]
17+ ,[Plain [Math InlineMath "f2"]]]
18+ [[[Plain [Math InlineMath "e"]]
19+ ,[Plain [Math InlineMath "0.5"]]
20+ ,[Plain [Math InlineMath "4"]]]
21+ ,[[Plain [Math InlineMath "f"]]
22+ ,[Plain [Math InlineMath "0.5"]]
23+ ,[Plain [Math InlineMath "5,5"]]]]]
24+```