git.iko.soy/typeable/pandoc
on master help sign in
Docx writer: support figure numbers.

These are set up in such a way that they will work with Word's automatic table of figures. Closes #7392.

commita3d745e48560a55d9a9ea9fa43ffdd5a8b84987f authorJohn MacFarlane <jgm@berkeley.edu> dateTue 29 Jun 2021, 09:44 (5 years ago) parentb7572db
3 files changed+21−3
src/Text/Pandoc/Writers/Docx.hs+19−3 src/Text/Pandoc/Writers/Docx/Types.hs+2 test/docx/golden/image.docx
src/Text/Pandoc/Writers/Docx.hs+19−3
@@ -36,7 +36,8 @@ import Data.Time.Clock.POSIX
3636import Data.Digest.Pure.SHA (sha1, showDigest)
3737import Skylighting
3838import Text.Collate.Lang (renderLang)
39−import Text.Pandoc.Class.PandocMonad (PandocMonad, report, toLang)
39+import Text.Pandoc.Class.PandocMonad (PandocMonad, report, toLang, translateTerm)
40+import qualified Text.Pandoc.Translations as Term
4041import qualified Text.Pandoc.Class.PandocMonad as P
4142import Data.Time
4243import Text.Pandoc.UTF8 (fromTextLazy)
@@ -854,14 +855,29 @@ blockToOpenXML' opts (Plain lst) = do
854855-- title beginning with fig: indicates that the image is a figure
855856blockToOpenXML' opts (Para [Image attr alt (src,T.stripPrefix "fig:" -> Just tit)]) = do
856857 setFirstPara
858+ fignum <- gets stNextFigureNum
859+ modify $ \st -> st{ stNextFigureNum = fignum + 1 }
860+ let figid = "fig" <> tshow fignum
861+ figname <- translateTerm Term.Figure
857862 prop <- pStyleM $
858863 if null alt
859864 then "Figure"
860865 else "Captioned Figure"
861866 paraProps <- local (\env -> env { envParaProperties = EnvProps (Just prop) [] <> envParaProperties env }) (getParaProps False)
862867 contents <- inlinesToOpenXML opts [Image attr alt (src,tit)]
863− captionNode <- withParaPropM (pStyleM "Image Caption")
864− $ blockToOpenXML opts (Para alt)
868+ captionNode <- if null alt
869+ then return []
870+ else withParaPropM (pStyleM "Image Caption")
871+ $ blockToOpenXML opts
872+ (Para $ Span (figid,[],[])
873+ [Str "Figure\160",
874+ RawInline (Format "openxml")
875+ ("<w:fldSimple w:instr=\"SEQ "
876+ <> figname
877+ <> " \\* ARABIC \"><w:r><w:t>"
878+ <> tshow fignum
879+ <> "</w:t></w:r></w:fldSimple>"),
880+ Str ":", Space] : alt)
865881 return $
866882 Elem (mknode "w:p" [] (map Elem paraProps ++ contents))
867883 : captionNode
src/Text/Pandoc/Writers/Docx/Types.hs+2
@@ -117,6 +117,7 @@ data WriterState = WriterState{
117117 , stDynamicParaProps :: Set.Set ParaStyleName
118118 , stDynamicTextProps :: Set.Set CharStyleName
119119 , stCurId :: Int
120+ , stNextFigureNum :: Int
120121 }
121122
122123defaultWriterState :: WriterState
@@ -137,6 +138,7 @@ defaultWriterState = WriterState{
137138 , stDynamicParaProps = Set.empty
138139 , stDynamicTextProps = Set.empty
139140 , stCurId = 20
141+ , stNextFigureNum = 1
140142 }
141143
142144setFirstPara :: PandocMonad m => WS m ()
test/docx/golden/image.docx

Binary file not shown.