Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/Text/Pandoc/Readers/DocBook.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import Text.TeXMath (readMathML, writeTeX)
import qualified Data.Map as M
import Text.Pandoc.XML.Light
import Text.Pandoc.Walk (query)
import Text.Read (readMaybe)

{-

Expand Down Expand Up @@ -1055,9 +1056,8 @@ parseBlock (Elem e) =
cs -> mapMaybe (findAttr (unqual "colname" )) cs
let isRow x = named "row" x || named "tr" x
headrows <- case filterChild (named "thead") e' of
Just h -> case filterChild isRow h of
Just x -> parseRow colnames x
Nothing -> return []
Just h -> mapM (parseRow colnames)
$ filterChildren isRow h
Nothing -> return []
bodyrows <- case filterChild (named "tbody") e' of
Just b -> mapM (parseRow colnames)
Expand All @@ -1071,7 +1071,7 @@ parseBlock (Elem e) =
|| x == '.') w
if n > 0 then Just n else Nothing
let numrows = maybe 0 maximum $ nonEmpty
$ map length bodyrows
$ map length (bodyrows ++ headrows)
let aligns = case colspecs of
[] -> replicate numrows AlignDefault
cs -> map toAlignment cs
Expand All @@ -1093,11 +1093,10 @@ parseBlock (Elem e) =
in ColWidth . scale <$> ws'
Nothing -> replicate numrows ColWidthDefault
let toRow = Row nullAttr
toHeaderRow l = [toRow l | not (null l)]
return $ tableWith (elId,classes,attrs)
(simpleCaption $ plain capt)
(zip aligns widths)
(TableHead nullAttr $ toHeaderRow headrows)
(TableHead nullAttr $ map toRow headrows)
[TableBody nullAttr 0 [] $ map toRow bodyrows]
(TableFoot nullAttr [])
sect n = sectWith(attrValue "id" e) [] [] n
Expand Down Expand Up @@ -1171,7 +1170,7 @@ parseMixed container conts = do

parseRow :: PandocMonad m => [Text] -> Element -> DB m [Cell]
parseRow cn = do
let isEntry x = named "entry" x || named "td" x || named "th" x
let isEntry x = named "entry" x || named "td" x || named "th" x
mapM (parseEntry cn) . filterChildren isEntry

parseEntry :: PandocMonad m => [Text] -> Element -> DB m Cell
Expand All @@ -1188,9 +1187,18 @@ parseEntry cn el = do
case (mStrt, mEnd) of
(Just start, Just end) -> colDistance start end
_ -> 1
let rowDistance mr = do
case readMaybe $ T.unpack mr :: Maybe Int of
Just moreRow -> RowSpan $ moreRow + 1
_ -> 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some linting tools will complain about this wildcard use, suggesting to either use a descriptive name like _notAnInt or to be explicit and match on Nothing. This can help to avoid confusion.

Doesn't need fixing, purely FYI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about that, but figured it may be better to be consistent with how colspan was handled.

let toRowSpan en = do
case findAttr (unqual "morerows") en of
Just moreRow -> rowDistance moreRow
_ -> 1
let colSpan = toColSpan el
let rowSpan = toRowSpan el
let align = toAlignment el
(fmap (cell align 1 colSpan) . parseMixed plain . elContent) el
(fmap (cell align rowSpan colSpan) . parseMixed plain . elContent) el

getInlines :: PandocMonad m => Element -> DB m Inlines
getInlines e' = trimInlines . mconcat <$>
Expand Down
33 changes: 33 additions & 0 deletions test/docbook-reader.docbook
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,39 @@ or here: &lt;http://example.com/&gt;
</tbody>
</tgroup>
</informaltable>
<para>
Multiline table with cells spanning multiple columns and rows without caption.
</para>
<informaltable>
<tgroup cols="3">
<colspec align="left" colname="c1" />
<colspec align="left" colname="c2" />
<colspec align="left" colname="c3" />
<colspec align="left" colname="c4" />
<thead>
<tr>
<th namest="c1" nameend="c3">Columns</th>
</tr>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
</thead>
<tbody>
<tr>
<td namest="c1" nameend="c2">A1 + B1</td>
<td morerows="1">C1 + C2</td>
</tr>
<tr>
<td namest="c1" nameend="c2" morerows="1">A2 + B2 + A3 + B3</td>
</tr>
<tr>
<td>C3</td>
</tr>
</tbody>
</tgroup>
</informaltable>
<procedure><title>An Example Procedure</title>
<step>
<para>
Expand Down
122 changes: 122 additions & 0 deletions test/docbook-reader.native
Original file line number Diff line number Diff line change
Expand Up @@ -3046,6 +3046,128 @@ Pandoc
]
]
(TableFoot ( "" , [] , [] ) [])
, Para
[ Str "Multiline"
, Space
, Str "table"
, Space
, Str "with"
, Space
, Str "cells"
, Space
, Str "spanning"
, Space
, Str "multiple"
, Space
, Str "columns"
, Space
, Str "and"
, Space
, Str "rows"
, Space
, Str "without"
, Space
, Str "caption."
]
, Table
( "" , [] , [] )
(Caption Nothing [])
[ ( AlignLeft , ColWidthDefault )
, ( AlignLeft , ColWidthDefault )
, ( AlignLeft , ColWidthDefault )
]
(TableHead
( "" , [] , [] )
[ Row
( "" , [] , [] )
[ Cell
( "" , [] , [] )
AlignDefault
(RowSpan 1)
(ColSpan 3)
[ Plain [ Str "Columns" ] ]
]
, Row
( "" , [] , [] )
[ Cell
( "" , [] , [] )
AlignDefault
(RowSpan 1)
(ColSpan 1)
[ Plain [ Str "A" ] ]
, Cell
( "" , [] , [] )
AlignDefault
(RowSpan 1)
(ColSpan 1)
[ Plain [ Str "B" ] ]
, Cell
( "" , [] , [] )
AlignDefault
(RowSpan 1)
(ColSpan 1)
[ Plain [ Str "C" ] ]
]
])
[ TableBody
( "" , [] , [] )
(RowHeadColumns 0)
[]
[ Row
( "" , [] , [] )
[ Cell
( "" , [] , [] )
AlignDefault
(RowSpan 1)
(ColSpan 2)
[ Plain
[ Str "A1" , Space , Str "+" , Space , Str "B1" ]
]
, Cell
( "" , [] , [] )
AlignDefault
(RowSpan 2)
(ColSpan 1)
[ Plain
[ Str "C1" , Space , Str "+" , Space , Str "C2" ]
]
]
, Row
( "" , [] , [] )
[ Cell
( "" , [] , [] )
AlignDefault
(RowSpan 2)
(ColSpan 2)
[ Plain
[ Str "A2"
, Space
, Str "+"
, Space
, Str "B2"
, Space
, Str "+"
, Space
, Str "A3"
, Space
, Str "+"
, Space
, Str "B3"
]
]
]
, Row
( "" , [] , [] )
[ Cell
( "" , [] , [] )
AlignDefault
(RowSpan 1)
(ColSpan 1)
[ Plain [ Str "C3" ] ]
]
]
]
(TableFoot ( "" , [] , [] ) [])
, OrderedList
( 1 , DefaultStyle , DefaultDelim )
[ [ Para [ Str "A" , Space , Str "Step" ] ]
Expand Down
Loading