From a400d0dc623a2f08d7470788855e880291d2f222 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Sat, 12 Sep 2020 20:44:15 +0200 Subject: [PATCH] HTML writer: render table footers if present Part of: #6314 --- pandoc.cabal | 3 ++ src/Text/Pandoc/Writers/HTML.hs | 73 ++++++++++++++++++++++----------- test/Tests/Old.hs | 13 +++--- test/tables/nordics.html4 | 59 ++++++++++++++++++++++++++ test/tables/nordics.html5 | 59 ++++++++++++++++++++++++++ test/tables/nordics.native | 46 +++++++++++++++++++++ 6 files changed, 224 insertions(+), 29 deletions(-) create mode 100644 test/tables/nordics.html4 create mode 100644 test/tables/nordics.html5 create mode 100644 test/tables/nordics.native diff --git a/pandoc.cabal b/pandoc.cabal index e46149b5a..a23f1fb91 100644 --- a/pandoc.cabal +++ b/pandoc.cabal @@ -288,6 +288,9 @@ extra-source-files: test/tables.muse test/tables.custom test/tables.xwiki + test/tables/*.html4 + test/tables/*.html5 + test/tables/*.native test/testsuite.txt test/writer.latex test/writer.context diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index 4fd671da8..6bb708c37 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -910,7 +910,7 @@ tableToHtml :: PandocMonad m => WriterOptions -> Ann.Table -> StateT WriterState m Html -tableToHtml opts (Ann.Table attr caption colspecs thead tbodies _tfoot) = do +tableToHtml opts (Ann.Table attr caption colspecs thead tbodies tfoot) = do captionDoc <- case caption of Caption _ [] -> return mempty Caption _ longCapt -> do @@ -921,11 +921,11 @@ tableToHtml opts (Ann.Table attr caption colspecs thead tbodies _tfoot) = do coltags <- colSpecListToHtml opts colspecs head' <- tableHeadToHtml opts thead body' <- mconcat <$> mapM (tableBodyToHtml opts) tbodies + foot' <- tableFootToHtml opts tfoot let (ident,classes,kvs) = attr -- When widths of columns are < 100%, we need to set width for the whole -- table, or some browsers give us skinny columns with lots of space -- between: - -- let totalWidth = sum widths let colWidth = \case ColWidth d -> d ColWidthDefault -> 0 @@ -936,8 +936,14 @@ tableToHtml opts (Ann.Table attr caption colspecs thead tbodies _tfoot) = do T.pack (show (round (totalWidth * 100) :: Int)) <> "%;"):kvs) _ -> attr - addAttrs opts attr' $ H.table $ - nl opts *> captionDoc *> coltags *> head' *> body' *> nl opts + addAttrs opts attr' $ H.table $ do + nl opts + captionDoc + coltags + head' + body' + foot' + nl opts tableBodyToHtml :: PandocMonad m => WriterOptions @@ -951,36 +957,57 @@ tableHeadToHtml :: PandocMonad m -> Ann.TableHead -> StateT WriterState m Html tableHeadToHtml opts (Ann.TableHead attr rows) = + tablePartToHtml opts Thead attr rows + +tableFootToHtml :: PandocMonad m + => WriterOptions + -> Ann.TableFoot + -> StateT WriterState m Html +tableFootToHtml opts (Ann.TableFoot attr rows) = + tablePartToHtml opts Tfoot attr rows + +tablePartToHtml :: PandocMonad m + => WriterOptions + -> TablePart + -> Attr + -> [Ann.HeaderRow] + -> StateT WriterState m Html +tablePartToHtml opts tblpart attr rows = if null rows || all isEmptyRow rows then return mempty else do - contents <- headerRowsToHtml opts rows - headElement <- addAttrs opts attr $ H.thead contents + let tag' = case tblpart of + Thead -> H.thead + Tfoot -> H.tfoot + Tbody -> H.tbody -- this would be unexpected + contents <- headerRowsToHtml opts tblpart rows + tablePartElement <- addAttrs opts attr $ tag' contents return $ do - headElement + tablePartElement nl opts where isEmptyRow (Ann.HeaderRow _attr _rownum cells) = all isEmptyCell cells isEmptyCell (Ann.Cell _colspecs _colnum cell) = cell == Cell nullAttr AlignDefault (RowSpan 1) (ColSpan 1) [] - -data RowType = HeaderRow | FooterRow | BodyRow +-- | The part of a table; header, footer, or body. +data TablePart = Thead | Tfoot | Tbody deriving (Eq) data CellType = HeaderCell | BodyCell -data TableRow = TableRow RowType Attr Ann.RowNumber Ann.RowHead Ann.RowBody +data TableRow = TableRow TablePart Attr Ann.RowNumber Ann.RowHead Ann.RowBody headerRowsToHtml :: PandocMonad m - => WriterOptions - -> [Ann.HeaderRow] - -> StateT WriterState m Html -headerRowsToHtml opts = + => WriterOptions + -> TablePart + -> [Ann.HeaderRow] + -> StateT WriterState m Html +headerRowsToHtml opts tablepart = rowListToHtml opts . map toTableRow where toTableRow (Ann.HeaderRow attr rownum rowbody) = - TableRow HeaderRow attr rownum [] rowbody + TableRow tablepart attr rownum [] rowbody bodyRowsToHtml :: PandocMonad m => WriterOptions @@ -990,7 +1017,7 @@ bodyRowsToHtml opts = rowListToHtml opts . zipWith toTableRow [1..] where toTableRow rownum (Ann.BodyRow attr _rownum rowhead rowbody) = - TableRow BodyRow attr rownum rowhead rowbody + TableRow Tbody attr rownum rowhead rowbody rowListToHtml :: PandocMonad m @@ -1034,14 +1061,14 @@ tableRowToHtml :: PandocMonad m => WriterOptions -> TableRow -> StateT WriterState m Html -tableRowToHtml opts (TableRow rowtype _attr rownum rowhead rowbody) = do +tableRowToHtml opts (TableRow tblpart _attr rownum rowhead rowbody) = do let rowclass = A.class_ $ case rownum of - Ann.RowNumber x | x `rem` 2 == 1 -> "odd" - _ | rowtype /= HeaderRow -> "even" - _ -> "header" - let celltype = case rowtype of - HeaderRow -> HeaderCell - _ -> BodyCell + Ann.RowNumber x | x `rem` 2 == 1 -> "odd" + _ | tblpart /= Thead -> "even" + _ -> "header" + let celltype = case tblpart of + Thead -> HeaderCell + _ -> BodyCell head' <- mapM (cellToHtml opts HeaderCell) rowhead body <- mapM (cellToHtml opts celltype) rowbody return $ do diff --git a/test/Tests/Old.hs b/test/Tests/Old.hs index 9ae10261e..ba6947eda 100644 --- a/test/Tests/Old.hs +++ b/test/Tests/Old.hs @@ -267,12 +267,13 @@ writerTests pandocPath format extendedWriterTests :: FilePath -> String -> [TestTree] extendedWriterTests pandocPath format = writerTests pandocPath format ++ - [ test pandocPath - "tables" - opts - ("tables" "planets.native") - ("tables" "planets" <.> format) - ] + let testForTable name = + test pandocPath + (name ++ " table") + opts + ("tables" name <.> "native") + ("tables" name <.> format) + in map testForTable ["planets", "nordics"] where opts = ["-r", "native", "-w", format, "--columns=78", "--variable", "pandoc-version="] diff --git a/test/tables/nordics.html4 b/test/tables/nordics.html4 new file mode 100644 index 000000000..13fa1976d --- /dev/null +++ b/test/tables/nordics.html4 @@ -0,0 +1,59 @@ + + ++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

States belonging to the Nordics.

NameCapitalPopulation
+(in 2018)
Area
+(in km2)
DenmarkCopenhagen5,809,50243,094
FinlandHelsinki5,537,364338,145
IcelandReykjavik343,518103,000
NorwayOslo5,372,191323,802
SwedenStockholm10,313,447450,295
Total27,376,0221,258,336
diff --git a/test/tables/nordics.html5 b/test/tables/nordics.html5 new file mode 100644 index 000000000..f0dd8b958 --- /dev/null +++ b/test/tables/nordics.html5 @@ -0,0 +1,59 @@ + + ++++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

States belonging to the Nordics.

NameCapitalPopulation
+(in 2018)
Area
+(in km2)
DenmarkCopenhagen5,809,50243,094
FinlandHelsinki5,537,364338,145
IcelandReykjavik343,518103,000
NorwayOslo5,372,191323,802
SwedenStockholm10,313,447450,295
Total27,376,0221,258,336
diff --git a/test/tables/nordics.native b/test/tables/nordics.native new file mode 100644 index 000000000..dc6a33b39 --- /dev/null +++ b/test/tables/nordics.native @@ -0,0 +1,46 @@ +[Table ("",[],[]) (Caption (Just [Str "Nordic countries"]) + [Para [Str "States", Space, Str "belonging", Space, Str "to", Space, Str "the", Space, Emph [Str "Nordics."]]]) + [(AlignCenter,ColWidth 0.3) + ,(AlignLeft,ColWidth 0.3) + ,(AlignLeft,ColWidth 0.2) + ,(AlignLeft,ColWidth 0.2)] + (TableHead ("",[],[]) + [Row ("",[],[]) + [Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1) [Plain [Str "Name"]] + ,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1) [Plain [Str "Capital"]] + ,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1) [Plain [Str "Population", LineBreak, Str "(in", Space, Str "2018)"]] + ,Cell ("",[],[]) AlignCenter (RowSpan 1) (ColSpan 1) [Plain [Str "Area", LineBreak, Str "(in", Space, Str "km", Superscript [Str "2"], Str ")"]]]]) + [(TableBody ("",[],[]) (RowHeadColumns 1) + [] + [Row ("",[],[]) + [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Denmark"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Copenhagen"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "5,809,502"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "43,094"]]] + ,Row ("",[],[]) + [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Finland"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Helsinki"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "5,537,364"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "338,145"]]] + ,Row ("",[],[]) + [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Iceland"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Reykjavik"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "343,518"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "103,000"]]] + ,Row ("",[],[]) + [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Norway"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Oslo"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "5,372,191"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "323,802"]]] + ,Row ("",[],[]) + [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Sweden"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Stockholm"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "10,313,447"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "450,295"]]]])] + (TableFoot ("",[],[]) + [Row ("",[],[]) + [Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "Total"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "27,376,022"]] + ,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1) [Plain [Str "1,258,336"]]]]) +]