git.iko.soy/typeable/pandoc
on master help sign in
Pipe tables with long lines now get relative cell widths.

If a pipe table contains a line longer than the column width (as set by `--columns` or 80 by default), relative widths are computed based on the widths of the separator lines relative to the column width. This should solve persistent problems with long pipe tables in LaTeX/PDF output, and give more flexibility for determining relative column widths in other formats, too. For narrower pipe tables, column widths of 0 are used, telling pandoc not to specify widths explicitly in output formats that permit this. Closes #2471.

commiteb8aee477db045a7449bc752975528263964b8ce authorJohn MacFarlane <jgm@berkeley.edu> dateFri 30 Oct 2015, 12:37 (11 years ago) parent7843b57
3 files changed+39−14
src/Text/Pandoc/Readers/Markdown.hs+20−13 tests/pipe-tables.native+12−1 tests/pipe-tables.txt+7
src/Text/Pandoc/Readers/Markdown.hs+20−13
@@ -1320,7 +1320,7 @@ removeOneLeadingSpace xs =
13201320gridTableFooter :: MarkdownParser [Char]
13211321gridTableFooter = blanklines
13221322
1323−pipeBreak :: MarkdownParser [Alignment]
1323+pipeBreak :: MarkdownParser ([Alignment], [Int])
13241324pipeBreak = try $ do
13251325 nonindentSpaces
13261326 openPipe <- (True <$ char '|') <|> return False
@@ -1330,16 +1330,22 @@ pipeBreak = try $ do
13301330 guard $ not (null rest && not openPipe)
13311331 optional (char '|')
13321332 blankline
1333− return (first:rest)
1333+ return $ unzip (first:rest)
13341334
13351335pipeTable :: MarkdownParser ([Alignment], [Double], F [Blocks], F [[Blocks]])
13361336pipeTable = try $ do
13371337 nonindentSpaces
13381338 lookAhead nonspaceChar
1339− (heads,aligns) <- (,) <$> pipeTableRow <*> pipeBreak
1340− lines' <- sequence <$> many pipeTableRow
1341− let widths = replicate (length aligns) 0.0
1342− return $ (aligns, widths, heads, lines')
1339+ (heads,(aligns, seplengths)) <- (,) <$> pipeTableRow <*> pipeBreak
1340+ (lines', rawRows) <- unzip <$> many (withRaw pipeTableRow)
1341+ let maxlength = maximum $ map length rawRows
1342+ numColumns <- getOption readerColumns
1343+ let widths = if maxlength > numColumns
1344+ then map (\len ->
1345+ fromIntegral (len + 1) / fromIntegral numColumns)
1346+ seplengths
1347+ else replicate (length aligns) 0.0
1348+ return $ (aligns, widths, heads, sequence lines')
13431349
13441350sepPipe :: MarkdownParser ()
13451351sepPipe = try $ do
@@ -1368,19 +1374,20 @@ pipeTableRow = do
13681374 ils' | B.isNull ils' -> mempty
13691375 | otherwise -> B.plain $ ils') cells'
13701376
1371−pipeTableHeaderPart :: Parser [Char] st Alignment
1377+pipeTableHeaderPart :: Parser [Char] st (Alignment, Int)
13721378pipeTableHeaderPart = try $ do
13731379 skipMany spaceChar
13741380 left <- optionMaybe (char ':')
1375− many1 (char '-')
1381+ pipe <- many1 (char '-')
13761382 right <- optionMaybe (char ':')
13771383 skipMany spaceChar
1384+ let len = length pipe + maybe 0 (const 1) left + maybe 0 (const 1) right
13781385 return $
1379− case (left,right) of
1380− (Nothing,Nothing) -> AlignDefault
1381− (Just _,Nothing) -> AlignLeft
1382− (Nothing,Just _) -> AlignRight
1383− (Just _,Just _) -> AlignCenter
1386+ ((case (left,right) of
1387+ (Nothing,Nothing) -> AlignDefault
1388+ (Just _,Nothing) -> AlignLeft
1389+ (Nothing,Just _) -> AlignRight
1390+ (Just _,Just _) -> AlignCenter), len)
13841391
13851392-- Succeed only if current line contains a pipe.
13861393scanForPipe :: Parser [Char] st ()
tests/pipe-tables.native+12−1
@@ -83,4 +83,15 @@
8383 [[[Plain [Str "3"]]
8484 ,[Plain [Str "33"]]]
8585 ,[[Plain [Str "4"]]
86− ,[Plain [Str "44"]]]]]
86+ ,[Plain [Str "44"]]]]
87+,Para [Str "Long",Space,Str "pipe",Space,Str "table",Space,Str "with",Space,Str "relative",Space,Str "widths:"]
88+,Table [] [AlignDefault,AlignDefault,AlignDefault] [0.125,0.1375,0.5]
89+ [[Plain [Str "Default1"]]
90+ ,[Plain [Str "Default2"]]
91+ ,[Plain [Str "Default3"]]]
92+ [[[Plain [Str "123"]]
93+ ,[Plain [Str "this",Space,Str "is",Space,Str "a",Space,Str "table",Space,Str "cell"]]
94+ ,[Plain [Str "and",Space,Str "this",Space,Str "is",Space,Str "a",Space,Str "really",Space,Str "long",Space,Str "table",Space,Str "cell",Space,Str "that",Space,Str "will",Space,Str "probably",Space,Str "need",Space,Str "wrapping"]]]
95+ ,[[Plain [Str "123"]]
96+ ,[Plain [Str "123"]]
97+ ,[Plain [Str "123"]]]]]
tests/pipe-tables.txt+7
@@ -59,3 +59,10 @@ Number of siblings | Salary
5959------------------:|:------
6060 3 | 33
6161 4 | 44
62+
63+Long pipe table with relative widths:
64+
65+| Default1 | Default2 | Default3 |
66+ |---------|----------|---------------------------------------|
67+|123|this is a table cell|and this is a really long table cell that will probably need wrapping|
68+|123|123|123|