@@ -137,15 +137,15 @@ resolveRefs _ x = x
137137-- Left e -> error (show e)
138138-- Right r -> return r
139139
140−newtype HeaderNum = HeaderNum [Int]
140+newtype DottedNum = DottedNum [Int]
141141 deriving (Show)
142142
143−renderHeaderNum :: HeaderNum -> String
144−renderHeaderNum (HeaderNum xs) =
143+renderDottedNum :: DottedNum -> String
144+renderDottedNum (DottedNum xs) =
145145 intercalate "." (map show xs)
146146
147−incrementHeaderNum :: Int -> HeaderNum -> HeaderNum
148−incrementHeaderNum level (HeaderNum ns) = HeaderNum $
147+incrementDottedNum :: Int -> DottedNum -> DottedNum
148+incrementDottedNum level (DottedNum ns) = DottedNum $
149149 case reverse (take level (ns ++ repeat 0)) of
150150 (x:xs) -> reverse (x+1 : xs)
151151 [] -> [] -- shouldn't happen
@@ -162,7 +162,8 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
162162 , sCaption :: (Maybe Inlines, Maybe String)
163163 , sInListItem :: Bool
164164 , sInTableCell :: Bool
165− , sLastHeaderNum :: HeaderNum
165+ , sLastHeaderNum :: DottedNum
166+ , sLastFigureNum :: DottedNum
166167 , sLabels :: M.Map String [Inline]
167168 , sHasChapters :: Bool
168169 , sToggles :: M.Map String Bool
@@ -182,7 +183,8 @@ defaultLaTeXState = LaTeXState{ sOptions = def
182183 , sCaption = (Nothing, Nothing)
183184 , sInListItem = False
184185 , sInTableCell = False
185− , sLastHeaderNum = HeaderNum []
186+ , sLastHeaderNum = DottedNum []
187+ , sLastFigureNum = DottedNum []
186188 , sLabels = M.empty
187189 , sHasChapters = False
188190 , sToggles = M.empty
@@ -2431,11 +2433,11 @@ section (ident, classes, kvs) lvl = do
24312433 hn <- sLastHeaderNum <$> getState
24322434 hasChapters <- sHasChapters <$> getState
24332435 let lvl' = lvl + if hasChapters then 1 else 0
2434− let num = incrementHeaderNum lvl' hn
2435− updateState $ \st -> st{ sLastHeaderNum = num }
2436− updateState $ \st -> st{ sLabels = M.insert lab
2437− [Str (renderHeaderNum num)]
2438− (sLabels st) }
2436+ let num = incrementDottedNum lvl' hn
2437+ updateState $ \st -> st{ sLastHeaderNum = num
2438+ , sLabels = M.insert lab
2439+ [Str (renderDottedNum num)]
2440+ (sLabels st) }
24392441 attr' <- registerHeader (lab, classes, kvs) contents
24402442 return $ headerWith attr' lvl contents
24412443
@@ -2722,6 +2724,33 @@ addImageCaption = walkM go
27222724 attr' = case mblab of
27232725 Just lab -> (lab, cls, kvs)
27242726 Nothing -> attr
2727+ case attr' of
2728+ ("", _, _) -> return ()
2729+ (ident, _, _) -> do
2730+ st <- getState
2731+ let chapnum =
2732+ case (sHasChapters st, sLastHeaderNum st) of
2733+ (True, DottedNum (n:_)) -> Just n
2734+ _ -> Nothing
2735+ let num = case sLastFigureNum st of
2736+ DottedNum [m,n] ->
2737+ case chapnum of
2738+ Just m' | m' == m -> DottedNum [m, n+1]
2739+ | otherwise -> DottedNum [m', 1]
2740+ Nothing -> DottedNum [1]
2741+ -- shouldn't happen
2742+ DottedNum [n] ->
2743+ case chapnum of
2744+ Just m -> DottedNum [m, 1]
2745+ Nothing -> DottedNum [n + 1]
2746+ _ ->
2747+ case chapnum of
2748+ Just n -> DottedNum [n, 1]
2749+ Nothing -> DottedNum [1]
2750+ setState $
2751+ st{ sLastFigureNum = num
2752+ , sLabels = M.insert ident
2753+ [Str (renderDottedNum num)] (sLabels st) }
27252754 return $ Image attr' alt' (src, tit')
27262755 go x = return x
27272756