git.iko.soy/typeable/pandoc
on master help sign in
Don't wrap lines in grid tables when `--wrap=none` (#4320)

* Annotate gridTable code with comments and abstract small functions * Don't wrap lines in tables when `--wrap=none`. Instead, expand cells, even if it results in cells that don't respect relative widths or surpass page column width. * This change affects RST, Markdown, and Haddock writers.

commite5845f33ad071dbea142ef1ff96d689fe7a71b86 authorFrancesco Occhipinti <focchi.pinti@gmail.com> dateSun 18 Mar 2018, 04:31 (9 years ago) parent2ddf225
2 files changed+45−12
src/Text/Pandoc/Writers/Shared.hs+30−12 test/command/4320.md+15
src/Text/Pandoc/Writers/Shared.hs+30−12
@@ -241,40 +241,58 @@ gridTable :: Monad m
241241 -> [[[Block]]]
242242 -> m Doc
243243gridTable opts blocksToDoc headless aligns widths headers rows = do
244+ -- the number of columns will be used in case of even widths
244245 let numcols = maximum (length aligns : length widths :
245246 map length (headers:rows))
247+ -- handleGivenWidths wraps the given blocks in order for them to fit
248+ -- in cells with given widths. the returned content can be
249+ -- concatenated with borders and frames
246250 let handleGivenWidths widths' = do
247251 let widthsInChars' = map (
248252 (\x -> if x < 1 then 1 else x) .
249253 (\x -> x - 3) . floor .
250254 (fromIntegral (writerColumns opts) *)
251255 ) widths'
252− rawHeaders' <- zipWithM blocksToDoc
253− (map (\w -> opts{writerColumns =
254− min (w - 2) (writerColumns opts)}) widthsInChars')
255− headers
256+ -- replace page width (in columns) in the options with a
257+ -- given width if smaller (adjusting by two)
258+ useWidth w = opts{writerColumns = min (w - 2) (writerColumns opts)}
259+ -- prepare options to use with header and row cells
260+ columnOptions = map useWidth widthsInChars'
261+ rawHeaders' <- zipWithM blocksToDoc columnOptions headers
256262 rawRows' <- mapM
257− (\cs -> zipWithM blocksToDoc
258− (map (\w -> opts{writerColumns =
259− min (w - 2) (writerColumns opts)}) widthsInChars')
260− cs)
263+ (\cs -> zipWithM blocksToDoc columnOptions cs)
261264 rows
262265 return (widthsInChars', rawHeaders', rawRows')
263− let handleZeroWidths = do
266+ -- handleFullWidths tries to wrap cells to the page width or even
267+ -- more in cases where `--wrap=none`. thus the content here is left
268+ -- as wide as possible
269+ let handleFullWidths = do
264270 rawHeaders' <- mapM (blocksToDoc opts) headers
265271 rawRows' <- mapM (mapM (blocksToDoc opts)) rows
266272 let numChars [] = 0
267273 numChars xs = maximum . map offset $ xs
268274 let widthsInChars' =
269275 map numChars $ transpose (rawHeaders' : rawRows')
276+ return (widthsInChars', rawHeaders', rawRows')
277+ -- handleZeroWidths calls handleFullWidths to check whether a wide
278+ -- table would fit in the page. if the produced table is too wide,
279+ -- it calculates even widths and passes the content to
280+ -- handleGivenWidths
281+ let handleZeroWidths = do
282+ (widthsInChars', rawHeaders', rawRows') <- handleFullWidths
270283 if sum widthsInChars' > writerColumns opts
271284 then -- use even widths
272285 handleGivenWidths
273286 (replicate numcols (1.0 / fromIntegral numcols) :: [Double])
274287 else return (widthsInChars', rawHeaders', rawRows')
275− (widthsInChars, rawHeaders, rawRows) <- if all (== 0) widths
276− then handleZeroWidths
277− else handleGivenWidths widths
288+ -- render the contents of header and row cells differently depending
289+ -- on command line options, widths given in this specific table, and
290+ -- cells' contents
291+ let handleWidths
292+ | (writerWrapText opts) == WrapNone = handleFullWidths
293+ | all (== 0) widths = handleZeroWidths
294+ | otherwise = handleGivenWidths widths
295+ (widthsInChars, rawHeaders, rawRows) <- handleWidths
278296 let hpipeBlocks blocks = hcat [beg, middle, end]
279297 where h = maximum (1 : map height blocks)
280298 sep' = lblock 3 $ vcat (replicate h (text " | "))
test/command/4320.mdnew file+15
@@ -0,0 +1,15 @@
1+```
2+% pandoc -f native -t rst --wrap=none
3+[Table [] [AlignDefault,AlignDefault] [0.47368421052631576,0.5263157894736842]
4+ [[Plain [Str "one"]]
5+ ,[Plain [Str "two"]]]
6+ [[[Plain [Str "ports"]]
7+ ,[BlockQuote
8+ [Para [Strong [Str "thisIsGoingToBeTooLongAnyway"]]]]]]]
9+^D
10++-------+--------------------------------------+
11+| one | two |
12++=======+======================================+
13+| ports | **thisIsGoingToBeTooLongAnyway** |
14++-------+--------------------------------------+
15+```