git.iko.soy/typeable/pandoc
on master help sign in
Moved more gridTable calculations to Writers.Shared.
e6cdf21John MacFarlane
src/Text/Pandoc/Writers/Markdown.hs+3−24
@@ -565,30 +565,9 @@ blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
565 pandocTable opts (all null headers) aligns' widths'
565 pandocTable opts (all null headers) aligns' widths'
566 rawHeaders rawRows
566 rawHeaders rawRows
567 | isEnabled Ext_grid_tables opts &&
567 | isEnabled Ext_grid_tables opts &&
568− writerColumns opts >= 8 * numcols -> do
568+ writerColumns opts >= 8 * numcols -> (id,) <$>
569− let widths'' = if all (==0) widths'
569+ gridTable opts blockListToMarkdown
570− then replicate numcols
571− (1.0 / fromIntegral numcols)
572− else widths'
573− let widthsInChars = map ((\x -> x - 3) . floor .
574− (fromIntegral (writerColumns opts) *)) widths''
575− rawHeaders' <- zipWithM
576− blockListToMarkdown
577− (map (\w -> opts{writerColumns =
578− min (w - 2) (writerColumns opts)})
579− widthsInChars)
580− headers
581− rawRows' <- mapM
582− (\cs -> zipWithM
583− blockListToMarkdown
584− (map (\w -> opts{writerColumns =
585− min (w - 2) (writerColumns opts)})
586− widthsInChars)
587− cs)
588− rows
589− fmap (id,) $
590− gridTable (all null headers) aligns' widthsInChars
570+ (all null headers) aligns' widths' headers rows
591− rawHeaders' rawRows'
592 | isEnabled Ext_raw_html opts -> fmap (id,) $
571 | isEnabled Ext_raw_html opts -> fmap (id,) $
593 text <$>
572 text <$>
594 (writeHtml5String def $ Pandoc nullMeta [t])
573 (writeHtml5String def $ Pandoc nullMeta [t])
src/Text/Pandoc/Writers/Shared.hs+30−5
@@ -42,7 +42,7 @@ module Text.Pandoc.Writers.Shared (
42 , gridTable
42 , gridTable
43 )
43 )
44where
44where
45−import Control.Monad (liftM)
45+import Control.Monad (liftM, zipWithM)
46import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (Object),
46import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (Object),
47 encode, fromJSON)
47 encode, fromJSON)
48import qualified Data.HashMap.Strict as H
48import qualified Data.HashMap.Strict as H
@@ -217,9 +217,34 @@ unsmartify opts ('\8212':xs)
217unsmartify opts (x:xs) = x : unsmartify opts xs
217unsmartify opts (x:xs) = x : unsmartify opts xs
218unsmartify _ [] = []
218unsmartify _ [] = []
219
219
220−gridTable :: Monad m => Bool -> [Alignment] -> [Int]
220+gridTable :: Monad m
221+ => WriterOptions
222+ -> (WriterOptions -> [Block] -> m Doc)
223+ -> Bool -- ^ headless
224+ -> [Alignment]
225+ -> [Double]
226+ -> [[Block]]
227+ -> [[[Block]]]
221− -> [Doc] -> [[Doc]] -> m Doc
228+ -> m Doc
222−gridTable headless aligns widthsInChars headers' rawRows = do
229+gridTable opts blocksToDoc headless aligns widths headers rows = do
230+ let numcols = maximum (length aligns : length widths :
231+ map length (headers:rows))
232+ let widths' = if all (==0) widths
233+ then replicate numcols
234+ (1.0 / fromIntegral numcols)
235+ else widths
236+ let widthsInChars = map ((\x -> x - 3) . floor .
237+ (fromIntegral (writerColumns opts) *)) widths'
238+ rawHeaders <- zipWithM blocksToDoc
239+ (map (\w -> opts{writerColumns =
240+ min (w - 2) (writerColumns opts)}) widthsInChars)
241+ headers
242+ rawRows <- mapM
243+ (\cs -> zipWithM blocksToDoc
244+ (map (\w -> opts{writerColumns =
245+ min (w - 2) (writerColumns opts)}) widthsInChars)
246+ cs)
247+ rows
223 let hpipeBlocks blocks = hcat [beg, middle, end]
248 let hpipeBlocks blocks = hcat [beg, middle, end]
224 where h = maximum (1 : map height blocks)
249 where h = maximum (1 : map height blocks)
225 sep' = lblock 3 $ vcat (map text $ replicate h " | ")
250 sep' = lblock 3 $ vcat (map text $ replicate h " | ")
@@ -227,7 +252,7 @@ gridTable headless aligns widthsInChars headers' rawRows = do
227 end = lblock 2 $ vcat (map text $ replicate h " |")
252 end = lblock 2 $ vcat (map text $ replicate h " |")
228 middle = chomp $ hcat $ intersperse sep' blocks
253 middle = chomp $ hcat $ intersperse sep' blocks
229 let makeRow = hpipeBlocks . zipWith lblock widthsInChars
254 let makeRow = hpipeBlocks . zipWith lblock widthsInChars
230− let head' = makeRow headers'
255+ let head' = makeRow rawHeaders
231 let rows' = map (makeRow . map chomp) rawRows
256 let rows' = map (makeRow . map chomp) rawRows
232 let borderpart ch align widthInChars =
257 let borderpart ch align widthInChars =
233 let widthInChars' = if widthInChars < 1 then 1 else widthInChars
258 let widthInChars' = if widthInChars < 1 then 1 else widthInChars