git.iko.soy/typeable/pandoc
on master help sign in
T.P.W.Shared: Changed gridTables so it does better at...

...keeping the widths of columns. See #4320. Adjust test case for #4320.

commit63cfd4540621ccb11a383a66f756d1f681323130 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 21 Oct 2019, 08:53 (7 years ago) parent1fe9742
2 files changed+26−22
src/Text/Pandoc/Writers/Shared.hs+20−16 test/command/4320.md+6−6
src/Text/Pandoc/Writers/Shared.hs+20−16
@@ -226,20 +226,21 @@ gridTable opts blocksToDoc headless aligns widths headers rows = do
226226 -- the number of columns will be used in case of even widths
227227 let numcols = maximum (length aligns : length widths :
228228 map length (headers:rows))
229+ let officialWidthsInChars widths' = map (
230+ (\x -> if x < 1 then 1 else x) .
231+ (\x -> x - 3) . floor .
232+ (fromIntegral (writerColumns opts) *)
233+ ) widths'
229234 -- handleGivenWidths wraps the given blocks in order for them to fit
230235 -- in cells with given widths. the returned content can be
231236 -- concatenated with borders and frames
232237 let handleGivenWidths widths' = do
233− let widthsInChars' = map (
234− (\x -> if x < 1 then 1 else x) .
235− (\x -> x - 3) . floor .
236− (fromIntegral (writerColumns opts) *)
237− ) widths'
238− -- replace page width (in columns) in the options with a
239− -- given width if smaller (adjusting by two)
240− useWidth w = opts{writerColumns = min (w - 2) (writerColumns opts)}
241− -- prepare options to use with header and row cells
242− columnOptions = map useWidth widthsInChars'
238+ let widthsInChars' = officialWidthsInChars widths'
239+ -- replace page width (in columns) in the options with a
240+ -- given width if smaller (adjusting by two)
241+ let useWidth w = opts{writerColumns = min (w - 2) (writerColumns opts)}
242+ -- prepare options to use with header and row cells
243+ let columnOptions = map useWidth widthsInChars'
243244 rawHeaders' <- zipWithM blocksToDoc columnOptions headers
244245 rawRows' <- mapM
245246 (\cs -> zipWithM blocksToDoc columnOptions cs)
@@ -248,20 +249,23 @@ gridTable opts blocksToDoc headless aligns widths headers rows = do
248249 -- handleFullWidths tries to wrap cells to the page width or even
249250 -- more in cases where `--wrap=none`. thus the content here is left
250251 -- as wide as possible
251− let handleFullWidths = do
252+ let handleFullWidths widths' = do
252253 rawHeaders' <- mapM (blocksToDoc opts) headers
253254 rawRows' <- mapM (mapM (blocksToDoc opts)) rows
254255 let numChars [] = 0
255256 numChars xs = maximum . map offset $ xs
256− let widthsInChars' =
257+ let minWidthsInChars =
257258 map numChars $ transpose (rawHeaders' : rawRows')
259+ let widthsInChars' = zipWith max
260+ minWidthsInChars
261+ (officialWidthsInChars widths')
258262 return (widthsInChars', rawHeaders', rawRows')
259263 -- handleZeroWidths calls handleFullWidths to check whether a wide
260264 -- table would fit in the page. if the produced table is too wide,
261265 -- it calculates even widths and passes the content to
262266 -- handleGivenWidths
263− let handleZeroWidths = do
264− (widthsInChars', rawHeaders', rawRows') <- handleFullWidths
267+ let handleZeroWidths widths' = do
268+ (widthsInChars', rawHeaders', rawRows') <- handleFullWidths widths'
265269 if foldl' (+) 0 widthsInChars' > writerColumns opts
266270 then -- use even widths
267271 handleGivenWidths
@@ -271,8 +275,8 @@ gridTable opts blocksToDoc headless aligns widths headers rows = do
271275 -- on command line options, widths given in this specific table, and
272276 -- cells' contents
273277 let handleWidths
274− | writerWrapText opts == WrapNone = handleFullWidths
275− | all (== 0) widths = handleZeroWidths
278+ | writerWrapText opts == WrapNone = handleFullWidths widths
279+ | all (== 0) widths = handleZeroWidths widths
276280 | otherwise = handleGivenWidths widths
277281 (widthsInChars, rawHeaders, rawRows) <- handleWidths
278282 let hpipeBlocks blocks = hcat [beg, middle, end]
test/command/4320.md+6−6
@@ -1,15 +1,15 @@
11```
22% pandoc -f native -t rst --wrap=none
3−[Table [] [AlignDefault,AlignDefault] [0.47368421052631576,0.5263157894736842]
3+[Table [] [AlignDefault,AlignDefault] [0.3,0.3]
44 [[Plain [Str "one"]]
55 ,[Plain [Str "two"]]]
66 [[[Plain [Str "ports"]]
77 ,[BlockQuote
88 [Para [Strong [Str "thisIsGoingToBeTooLongAnyway"]]]]]]]
99^D
10−+-------+-------------------------------------+
11−| one | two |
12−+=======+=====================================+
13−| ports | **thisIsGoingToBeTooLongAnyway** |
14−+-------+-------------------------------------+
10++--------------------+-------------------------------------+
11+| one | two |
12++====================+=====================================+
13+| ports | **thisIsGoingToBeTooLongAnyway** |
14++--------------------+-------------------------------------+
1515```