git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1999 from synsem/latex-reader-tables

LaTeX reader: minor table parsing improvements

commitc7c45918dc5cc34fe47186c62027802bcd6b3b9a authorJohn MacFarlane <jgm@berkeley.edu> dateSun 8 Mar 2015, 10:37 (12 years ago) parentefdc16912bf0ff
2 files changed+41−2
src/Text/Pandoc/Readers/LaTeX.hs+2−2 tests/Tests/Readers/LaTeX.hs+39
src/Text/Pandoc/Readers/LaTeX.hs+2−2
@@ -1275,7 +1275,7 @@ complexNatbibCitation mode = try $ do
12751275parseAligns :: LP [Alignment]
12761276parseAligns = try $ do
12771277 char '{'
1278− let maybeBar = skipMany $ sp <|> () <$ char '|' <|> () <$ try (string "@{}")
1278+ let maybeBar = skipMany $ sp <|> () <$ char '|' <|> () <$ (char '@' >> braced)
12791279 maybeBar
12801280 let cAlign = AlignCenter <$ char 'c'
12811281 let lAlign = AlignLeft <$ char 'l'
@@ -1314,7 +1314,7 @@ parseTableRow cols = try $ do
13141314simpTable :: Bool -> LP Blocks
13151315simpTable hasWidthParameter = try $ do
13161316 when hasWidthParameter $ () <$ (spaces >> tok)
1317− spaces
1317+ skipopts
13181318 aligns <- parseAligns
13191319 let cols = length aligns
13201320 optional hline
tests/Tests/Readers/LaTeX.hs+39
@@ -7,6 +7,7 @@ import Tests.Helpers
77import Tests.Arbitrary()
88import Text.Pandoc.Builder
99import Text.Pandoc
10+import Data.Monoid (mempty)
1011
1112latex :: String -> Pandoc
1213latex = readLaTeX def
@@ -16,6 +17,10 @@ infix 4 =:
1617 => String -> (String, c) -> Test
1718(=:) = test latex
1819
20+simpleTable' :: [Alignment] -> [[Blocks]] -> Blocks
21+simpleTable' aligns = table "" (zip aligns (repeat 0.0))
22+ (map (const mempty) aligns)
23+
1924tests :: [Test]
2025tests = [ testGroup "basic"
2126 [ "simple" =:
@@ -62,6 +67,40 @@ tests = [ testGroup "basic"
6267 "\\begin{lstlisting}\\end{lstlisting}" =?> codeBlock ""
6368 ]
6469
70+ , testGroup "tables"
71+ [ "Single cell table" =:
72+ "\\begin{tabular}{|l|}Test\\\\\\end{tabular}" =?>
73+ simpleTable' [AlignLeft] [[plain "Test"]]
74+ , "Multi cell table" =:
75+ "\\begin{tabular}{|rl|}One & Two\\\\ \\end{tabular}" =?>
76+ simpleTable' [AlignRight,AlignLeft] [[plain "One", plain "Two"]]
77+ , "Multi line table" =:
78+ unlines [ "\\begin{tabular}{|c|}"
79+ , "One\\\\"
80+ , "Two\\\\"
81+ , "Three\\\\"
82+ , "\\end{tabular}" ] =?>
83+ simpleTable' [AlignCenter]
84+ [[plain "One"], [plain "Two"], [plain "Three"]]
85+ , "Empty table" =:
86+ "\\begin{tabular}{}\\end{tabular}" =?>
87+ simpleTable' [] []
88+ , "Table with fixed column width" =:
89+ "\\begin{tabular}{|p{5cm}r|}One & Two\\\\ \\end{tabular}" =?>
90+ simpleTable' [AlignLeft,AlignRight] [[plain "One", plain "Two"]]
91+ , "Table with empty column separators" =:
92+ "\\begin{tabular}{@{}r@{}l}One & Two\\\\ \\end{tabular}" =?>
93+ simpleTable' [AlignRight,AlignLeft] [[plain "One", plain "Two"]]
94+ , "Table with custom column separators" =:
95+ unlines [ "\\begin{tabular}{@{($\\to$)}r@{\\hspace{2cm}}l}"
96+ , "One&Two\\\\"
97+ , "\\end{tabular}" ] =?>
98+ simpleTable' [AlignRight,AlignLeft] [[plain "One", plain "Two"]]
99+ , "Table with vertical alignment argument" =:
100+ "\\begin{tabular}[t]{r|r}One & Two\\\\ \\end{tabular}" =?>
101+ simpleTable' [AlignRight,AlignRight] [[plain "One", plain "Two"]]
102+ ]
103+
65104 , testGroup "citations"
66105 [ natbibCitations
67106 , biblatexCitations