git.iko.soy/typeable/pandoc
on master help sign in
Fix round-trip bug with HTML tables....

introduced by parsing of table attributes. (The writer always added a style with width, and we would get multiple such styles through successive round trips.)

commitf0c6411ede11b6282f70a668aad434e1420303f1 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 18 Apr 2020, 12:06 (7 years ago) parent37cadd5
1 file changed+13−9
src/Text/Pandoc/Writers/HTML.hs+13−9
src/Text/Pandoc/Writers/HTML.hs+13−9
@@ -885,7 +885,7 @@ blockToHtml opts (DefinitionList lst) = do
885885 return $ mconcat $ nl opts : term' : nl opts :
886886 intersperse (nl opts) defs') lst
887887 defList opts contents
888−blockToHtml opts (Table _ blkCapt specs thead tbody tfoot) = do
888+blockToHtml opts (Table attr blkCapt specs thead tbody tfoot) = do
889889 let (capt, aligns, widths, headers, rows') = toLegacyTable blkCapt specs thead tbody tfoot
890890 captionDoc <- if null capt
891891 then return mempty
@@ -913,15 +913,19 @@ blockToHtml opts (Table _ blkCapt specs thead tbody tfoot) = do
913913 return $ H.thead (nl opts >> contents) >> nl opts
914914 body' <- liftM (\x -> H.tbody (nl opts >> mconcat x)) $
915915 zipWithM (tableRowToHtml opts aligns) [1..] rows'
916− let tbl = H.table $
917− nl opts >> captionDoc >> coltags >> head' >> body' >> nl opts
918− let totalWidth = sum widths
916+ let (ident,classes,kvs) = attr
919917 -- When widths of columns are < 100%, we need to set width for the whole
920− -- table, or some browsers give us skinny columns with lots of space between:
921− return $ if totalWidth == 0 || totalWidth == 1
922− then tbl
923− else tbl ! A.style (toValue $ "width:" <>
924− show (round (totalWidth * 100) :: Int) <> "%;")
918+ -- table, or some browsers give us skinny columns with lots of space
919+ -- between:
920+ let totalWidth = sum widths
921+ let attr' = case lookup "style" kvs of
922+ Nothing | totalWidth < 1 && totalWidth > 0
923+ -> (ident,classes, ("style","width:" <>
924+ T.pack (show (round (totalWidth * 100) :: Int))
925+ <> "%;"):kvs)
926+ _ -> attr
927+ addAttrs opts attr' $ H.table $
928+ nl opts >> captionDoc >> coltags >> head' >> body' >> nl opts
925929
926930tableRowToHtml :: PandocMonad m
927931 => WriterOptions