OpenDocument writer: Implement table caption numbering (#4949)

Implement table caption numbering with a format
"Table 1: <caption>".

Translations are enabled and numbering is consecutive for
captioned tables, uncaptioned tables are not enumerated.

Captioned figures are now also numbered consecutively
and uncaptioned figures are not enumerated.
This commit is contained in:
Nils Carlson 2018-10-05 16:58:23 +00:00 committed by John MacFarlane
parent 90a4d693ef
commit 6304720d8d
2 changed files with 59 additions and 40 deletions

View file

@ -52,7 +52,7 @@ import Text.Pandoc.Options
import Text.Pandoc.Pretty
import Text.Pandoc.Shared (linesToPara)
import Text.Pandoc.Templates (renderTemplate')
import Text.Pandoc.Translations (Term(Figure))
import qualified Text.Pandoc.Translations as Term (Term(Figure, Table))
import Text.Pandoc.Writers.Math
import Text.Pandoc.Writers.Shared
import Text.Pandoc.XML
@ -70,32 +70,36 @@ plainToPara x = x
type OD m = StateT WriterState m
data WriterState =
WriterState { stNotes :: [Doc]
, stTableStyles :: [Doc]
, stParaStyles :: [Doc]
, stListStyles :: [(Int, [Doc])]
, stTextStyles :: Map.Map (Set.Set TextStyle) (String, Doc)
, stTextStyleAttr :: Set.Set TextStyle
, stIndentPara :: Int
, stInDefinition :: Bool
, stTight :: Bool
, stFirstPara :: Bool
, stImageId :: Int
WriterState { stNotes :: [Doc]
, stTableStyles :: [Doc]
, stParaStyles :: [Doc]
, stListStyles :: [(Int, [Doc])]
, stTextStyles :: Map.Map (Set.Set TextStyle) (String, Doc)
, stTextStyleAttr :: Set.Set TextStyle
, stIndentPara :: Int
, stInDefinition :: Bool
, stTight :: Bool
, stFirstPara :: Bool
, stImageId :: Int
, stTableCaptionId :: Int
, stImageCaptionId :: Int
}
defaultWriterState :: WriterState
defaultWriterState =
WriterState { stNotes = []
, stTableStyles = []
, stParaStyles = []
, stListStyles = []
, stTextStyles = Map.empty
, stTextStyleAttr = Set.empty
, stIndentPara = 0
, stInDefinition = False
, stTight = False
, stFirstPara = False
, stImageId = 1
WriterState { stNotes = []
, stTableStyles = []
, stParaStyles = []
, stListStyles = []
, stTextStyles = Map.empty
, stTextStyleAttr = Set.empty
, stIndentPara = 0
, stInDefinition = False
, stTight = False
, stFirstPara = False
, stImageId = 1
, stTableCaptionId = 1
, stImageCaptionId = 1
}
when :: Bool -> Doc -> Doc
@ -408,7 +412,7 @@ blockToOpenDocument o bs
mapM_ addParaStyle . newPara $ paraHStyles ++ paraStyles
captionDoc <- if null c
then return empty
else withParagraphStyle o "Table" [Para c]
else inlinesToOpenDocument o c >>= numberedTableCaption
th <- if all null h
then return empty
else colHeadsToOpenDocument o (map fst paraHStyles) h
@ -419,22 +423,35 @@ blockToOpenDocument o bs
figure attr caption source title | null caption =
withParagraphStyle o "Figure" [Para [Image attr caption (source,title)]]
| otherwise = do
id' <- gets stImageId
imageDoc <- withParagraphStyle o "FigureWithCaption" [Para [Image attr caption (source,title)]]
captionDoc <- inlinesToOpenDocument o caption >>= numberedFigureCaption id'
captionDoc <- inlinesToOpenDocument o caption >>= numberedFigureCaption
return $ imageDoc $$ captionDoc
numberedFigureCaption :: PandocMonad m => Int -> Doc -> OD m Doc
numberedFigureCaption num caption = do
figterm <- translateTerm Figure
let t = text figterm
let r = num - 1
let s = inTags False "text:sequence" [ ("text:ref-name", "refIllustration" ++ show r),
("text:name", "Illustration"),
("text:formula", "ooow:Illustration+1"),
numberedTableCaption :: PandocMonad m => Doc -> OD m Doc
numberedTableCaption caption = do
id' <- gets stTableCaptionId
modify (\st -> st{ stTableCaptionId = id' + 1 })
capterm <- translateTerm Term.Table
return $ numberedCaption "Table" capterm "Table" id' caption
numberedFigureCaption :: PandocMonad m => Doc -> OD m Doc
numberedFigureCaption caption = do
id' <- gets stImageCaptionId
modify (\st -> st{ stImageCaptionId = id' + 1 })
capterm <- translateTerm Term.Figure
return $ numberedCaption "FigureCaption" capterm "Illustration" id' caption
numberedCaption :: String -> String -> String -> Int -> Doc -> Doc
numberedCaption style term name num caption =
let t = text term
r = num - 1
s = inTags False "text:sequence" [ ("text:ref-name", "ref" ++ name ++ show r),
("text:name", name),
("text:formula", "ooow:" ++ name ++ "+1"),
("style:num-format", "1") ] $ text $ show num
let c = text ": "
return $ inParagraphTagsWithStyle "FigureCaption" $ hcat [ t, text " ", s, c, caption ]
c = text ": "
in inParagraphTagsWithStyle style $ hcat [ t, text " ", s, c, caption ]
colHeadsToOpenDocument :: PandocMonad m
=> WriterOptions -> [String] -> [[Block]]

View file

@ -63,7 +63,8 @@
</table:table-cell>
</table:table-row>
</table:table>
<text:p text:style-name="Table">Demonstration of simple table syntax.</text:p>
<text:p text:style-name="Table">Table <text:sequence text:ref-name="refTable0" text:name="Table" text:formula="ooow:Table+1" style:num-format="1">1</text:sequence>: Demonstration
of simple table syntax.</text:p>
<text:p text:style-name="First_20_paragraph">Simple table without
caption:</text:p>
<table:table table:name="Table2" table:style-name="Table2">
@ -196,7 +197,8 @@ spaces:</text:p>
</table:table-cell>
</table:table-row>
</table:table>
<text:p text:style-name="Table">Demonstration of simple table syntax.</text:p>
<text:p text:style-name="Table">Table <text:sequence text:ref-name="refTable1" text:name="Table" text:formula="ooow:Table+1" style:num-format="1">2</text:sequence>: Demonstration
of simple table syntax.</text:p>
<text:p text:style-name="First_20_paragraph">Multiline table with
caption:</text:p>
<table:table table:name="Table4" table:style-name="Table4">
@ -251,8 +253,8 @@ caption:</text:p>
</table:table-cell>
</table:table-row>
</table:table>
<text:p text:style-name="Table">Heres the caption. It may span multiple
lines.</text:p>
<text:p text:style-name="Table">Table <text:sequence text:ref-name="refTable2" text:name="Table" text:formula="ooow:Table+1" style:num-format="1">3</text:sequence>: Heres
the caption. It may span multiple lines.</text:p>
<text:p text:style-name="First_20_paragraph">Multiline table without
caption:</text:p>
<table:table table:name="Table5" table:style-name="Table5">