git.iko.soy/typeable/pandoc
on master help sign in
Docx writer: Add table numbering for captioned tables.

The numbers are added using fields, so that Word can create a list of tables that will update automatically.

commit0948af9cc549f0ea3b85fa760aa521b8deaad2c0 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 29 Jun 2021, 11:15 (5 years ago) parenta01ba44
4 files changed+33−4
src/Text/Pandoc/Writers/Docx/Table.hs+28−3 src/Text/Pandoc/Writers/Docx/Types.hs+2 test/Tests/Writers/OOXML.hs+3−1 test/docx/golden/image.docx
src/Text/Pandoc/Writers/Docx/Table.hs+28−3
@@ -17,7 +17,7 @@ import Control.Monad.State.Strict
1717import Data.Array
1818import Data.Text (Text)
1919import Text.Pandoc.Definition
20−import Text.Pandoc.Class.PandocMonad (PandocMonad)
20+import Text.Pandoc.Class.PandocMonad (PandocMonad, translateTerm)
2121import Text.Pandoc.Writers.Docx.Types
2222import Text.Pandoc.Shared
2323import Text.Printf (printf)
@@ -25,6 +25,7 @@ import Text.Pandoc.Writers.GridTable hiding (Table)
2525import Text.Pandoc.Writers.OOXML
2626import Text.Pandoc.XML.Light as XML hiding (Attr)
2727import qualified Data.Text as T
28+import qualified Text.Pandoc.Translations as Term
2829import qualified Text.Pandoc.Writers.GridTable as Grid
2930
3031tableToOpenXML :: PandocMonad m
@@ -33,15 +34,23 @@ tableToOpenXML :: PandocMonad m
3334 -> WS m [Content]
3435tableToOpenXML blocksToOpenXML gridTable = do
3536 setFirstPara
36− let (Grid.Table _attr caption colspecs _rowheads thead tbodies tfoot) =
37+ let (Grid.Table (ident,_,_) caption colspecs _rowheads thead tbodies tfoot) =
3738 gridTable
3839 let (Caption _maybeShortCaption captionBlocks) = caption
40+ tablenum <- gets stNextTableNum
41+ unless (null captionBlocks) $
42+ modify $ \st -> st{ stNextTableNum = tablenum + 1 }
43+ let tableid = if T.null ident
44+ then "table" <> tshow tablenum
45+ else ident
46+ tablename <- translateTerm Term.Table
3947 let captionStr = stringify captionBlocks
4048 let aligns = map fst $ elems colspecs
4149 captionXml <- if null captionBlocks
4250 then return []
4351 else withParaPropM (pStyleM "Table Caption")
44− $ blocksToOpenXML captionBlocks
52+ $ blocksToOpenXML
53+ $ addLabel tableid tablename tablenum captionBlocks
4554 -- We set "in table" after processing the caption, because we don't
4655 -- want the "Table Caption" style to be overwritten with "Compact".
4756 modify $ \s -> s { stInTable = True }
@@ -81,6 +90,22 @@ tableToOpenXML blocksToOpenXML gridTable = do
8190 modify $ \s -> s { stInTable = False }
8291 return $ captionXml ++ [Elem tbl]
8392
93+addLabel :: Text -> Text -> Int -> [Block] -> [Block]
94+addLabel tableid tablename tablenum bs =
95+ case bs of
96+ (Para ils : rest) -> Para (label : Space : ils) : rest
97+ (Plain ils : rest) -> Plain (label : Space : ils) : rest
98+ _ -> Para [label] : bs
99+ where
100+ label = Span (tableid,[],[])
101+ [Str (tablename <> "\160"),
102+ RawInline (Format "openxml")
103+ ("<w:fldSimple w:instr=\"SEQ Table"
104+ <> " \\* ARABIC \"><w:r><w:t>"
105+ <> tshow tablenum
106+ <> "</w:t></w:r></w:fldSimple>"),
107+ Str ":"]
108+
84109-- | Parts of a table
85110data RowType = HeadRow | BodyRow | FootRow
86111
src/Text/Pandoc/Writers/Docx/Types.hs+2
@@ -118,6 +118,7 @@ data WriterState = WriterState{
118118 , stDynamicTextProps :: Set.Set CharStyleName
119119 , stCurId :: Int
120120 , stNextFigureNum :: Int
121+ , stNextTableNum :: Int
121122 }
122123
123124defaultWriterState :: WriterState
@@ -139,6 +140,7 @@ defaultWriterState = WriterState{
139140 , stDynamicTextProps = Set.empty
140141 , stCurId = 20
141142 , stNextFigureNum = 1
143+ , stNextTableNum = 1
142144 }
143145
144146setFirstPara :: PandocMonad m => WS m ()
test/Tests/Writers/OOXML.hs+3−1
@@ -55,7 +55,9 @@ testArchive :: (WriterOptions -> Pandoc -> PandocIO BL.ByteString)
5555 -> IO Archive
5656testArchive writerFn opts fp = do
5757 txt <- T.readFile fp
58− bs <- runIOorExplode $ readNative def txt >>= writerFn opts
58+ bs <- runIOorExplode $ do
59+ setTranslations "en-US"
60+ readNative def txt >>= writerFn opts
5961 return $ toArchive bs
6062
6163compareFileList :: FilePath -> Archive -> Archive -> Maybe String
test/docx/golden/image.docx

Binary file not shown.