git.iko.soy/typeable/pandoc
on master help sign in
Docx writer: support figure numbers.
a3d745eJohn MacFarlane
src/Text/Pandoc/Writers/Docx.hs+19−3
@@ -36,7 +36,8 @@ import Data.Time.Clock.POSIX
36import Data.Digest.Pure.SHA (sha1, showDigest)
36import Data.Digest.Pure.SHA (sha1, showDigest)
37import Skylighting
37import Skylighting
38import Text.Collate.Lang (renderLang)
38import 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
40import qualified Text.Pandoc.Class.PandocMonad as P
41import qualified Text.Pandoc.Class.PandocMonad as P
41import Data.Time
42import Data.Time
42import Text.Pandoc.UTF8 (fromTextLazy)
43import Text.Pandoc.UTF8 (fromTextLazy)
@@ -854,14 +855,29 @@ blockToOpenXML' opts (Plain lst) = do
854-- title beginning with fig: indicates that the image is a figure
855-- title beginning with fig: indicates that the image is a figure
855blockToOpenXML' opts (Para [Image attr alt (src,T.stripPrefix "fig:" -> Just tit)]) = do
856blockToOpenXML' opts (Para [Image attr alt (src,T.stripPrefix "fig:" -> Just tit)]) = do
856 setFirstPara
857 setFirstPara
858+ fignum <- gets stNextFigureNum
859+ modify $ \st -> st{ stNextFigureNum = fignum + 1 }
860+ let figid = "fig" <> tshow fignum
861+ figname <- translateTerm Term.Figure
857 prop <- pStyleM $
862 prop <- pStyleM $
858 if null alt
863 if null alt
859 then "Figure"
864 then "Figure"
860 else "Captioned Figure"
865 else "Captioned Figure"
861 paraProps <- local (\env -> env { envParaProperties = EnvProps (Just prop) [] <> envParaProperties env }) (getParaProps False)
866 paraProps <- local (\env -> env { envParaProperties = EnvProps (Just prop) [] <> envParaProperties env }) (getParaProps False)
862 contents <- inlinesToOpenXML opts [Image attr alt (src,tit)]
867 contents <- inlinesToOpenXML opts [Image attr alt (src,tit)]
868+ captionNode <- if null alt
869+ then return []
863− captionNode <- withParaPropM (pStyleM "Image Caption")
870+ else withParaPropM (pStyleM "Image Caption")
864− $ blockToOpenXML opts (Para alt)
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)
865 return $
881 return $
866 Elem (mknode "w:p" [] (map Elem paraProps ++ contents))
882 Elem (mknode "w:p" [] (map Elem paraProps ++ contents))
867 : captionNode
883 : captionNode
src/Text/Pandoc/Writers/Docx/Types.hs+2
@@ -117,6 +117,7 @@ data WriterState = WriterState{
117 , stDynamicParaProps :: Set.Set ParaStyleName
117 , stDynamicParaProps :: Set.Set ParaStyleName
118 , stDynamicTextProps :: Set.Set CharStyleName
118 , stDynamicTextProps :: Set.Set CharStyleName
119 , stCurId :: Int
119 , stCurId :: Int
120+ , stNextFigureNum :: Int
120 }
121 }
121
122
122defaultWriterState :: WriterState
123defaultWriterState :: WriterState
@@ -137,6 +138,7 @@ defaultWriterState = WriterState{
137 , stDynamicParaProps = Set.empty
138 , stDynamicParaProps = Set.empty
138 , stDynamicTextProps = Set.empty
139 , stDynamicTextProps = Set.empty
139 , stCurId = 20
140 , stCurId = 20
141+ , stNextFigureNum = 1
140 }
142 }
141
143
142setFirstPara :: PandocMonad m => WS m ()
144setFirstPara :: PandocMonad m => WS m ()
test/docx/golden/image.docx

Binary file not shown.