@@ -1402,10 +1402,18 @@ treatAsInline = Set.fromList
14021402 , "pagebreak"
14031403 ]
14041404
1405+label :: PandocMonad m => LP m ()
1406+label = do
1407+ controlSeq "label"
1408+ t <- untokenize <$> braced
1409+ updateState $ \st -> st{ sLastLabel = Just t }
1410+
14051411dolabel :: PandocMonad m => LP m Inlines
14061412dolabel = do
14071413 v <- braced
14081414 let refstr = untokenize v
1415+ updateState $ \st ->
1416+ st{ sLastLabel = Just refstr }
14091417 return $ spanWith (refstr,[],[("label", refstr)])
14101418 $ inBrackets $ str $ untokenize v
14111419
@@ -1682,19 +1690,12 @@ bracketedNum = do
16821690 Just i -> return i
16831691 _ -> return 0
16841692
1685−setCaption :: PandocMonad m => LP m Blocks
1686−setCaption = do
1693+setCaption :: PandocMonad m => LP m ()
1694+setCaption = try $ do
1695+ skipopts
16871696 ils <- tok
1688− mblabel <- option Nothing $
1689− try $ spaces >> controlSeq "label" >> (Just <$> tok)
1690− let capt = case mblabel of
1691− Just lab -> let slab = stringify lab
1692− ils' = ils <> spanWith
1693− ("",[],[("label", slab)]) mempty
1694− in (Just ils', Just slab)
1695− Nothing -> (Just ils, Nothing)
1696− updateState $ \st -> st{ sCaption = capt }
1697− return mempty
1697+ optional $ try $ spaces *> label
1698+ updateState $ \st -> st{ sCaption = Just ils }
16981699
16991700looseItem :: PandocMonad m => LP m Blocks
17001701looseItem = do
@@ -1710,7 +1711,8 @@ epigraph = do
17101711 return $ divWith ("", ["epigraph"], []) (p1 <> p2)
17111712
17121713resetCaption :: PandocMonad m => LP m ()
1713−resetCaption = updateState $ \st -> st{ sCaption = (Nothing, Nothing) }
1714+resetCaption = updateState $ \st -> st{ sCaption = Nothing
1715+ , sLastLabel = Nothing }
17141716
17151717section :: PandocMonad m => Attr -> Int -> LP m Blocks
17161718section (ident, classes, kvs) lvl = do
@@ -1850,7 +1852,7 @@ blockCommands = M.fromList
18501852 , ("item", looseItem)
18511853 , ("documentclass", skipopts *> braced *> preamble)
18521854 , ("centerline", (para . trimInlines) <$> (skipopts *> tok))
1853− , ("caption", skipopts *> setCaption)
1855+ , ("caption", mempty <$ setCaption)
18541856 , ("bibliography", mempty <$ (skipopts *> braced >>=
18551857 addMeta "bibliography" . splitBibs . untokenize))
18561858 , ("addbibresource", mempty <$ (skipopts *> braced >>=
@@ -1901,7 +1903,7 @@ environments = M.fromList
19011903 , ("longtable", env "longtable" $
19021904 resetCaption *> simpTable "longtable" False >>= addTableCaption)
19031905 , ("table", env "table" $
1904− resetCaption *> skipopts *> blocks >>= addTableCaption)
1906+ skipopts *> resetCaption *> blocks >>= addTableCaption)
19051907 , ("tabular*", env "tabular*" $ simpTable "tabular*" True)
19061908 , ("tabularx", env "tabularx" $ simpTable "tabularx" True)
19071909 , ("tabular", env "tabular" $ simpTable "tabular" False)
@@ -2068,11 +2070,12 @@ addImageCaption :: PandocMonad m => Blocks -> LP m Blocks
20682070addImageCaption = walkM go
20692071 where go (Image attr@(_, cls, kvs) alt (src,tit))
20702072 | not ("fig:" `T.isPrefixOf` tit) = do
2071− (mbcapt, mblab) <- sCaption <$> getState
2073+ mbcapt <- sCaption <$> getState
2074+ mblabel <- sLastLabel <$> getState
20722075 let (alt', tit') = case mbcapt of
20732076 Just ils -> (toList ils, "fig:" <> tit)
20742077 Nothing -> (alt, tit)
2075− attr' = case mblab of
2078+ attr' = case mblabel of
20762079 Just lab -> (lab, cls, kvs)
20772080 Nothing -> attr
20782081 case attr' of
@@ -2363,7 +2366,10 @@ simpTable envname hasWidthParameter = try $ do
23632366 colspecs <- parseAligns
23642367 let (aligns, widths, prefsufs) = unzip3 colspecs
23652368 let cols = length colspecs
2366− optional $ controlSeq "caption" *> skipopts *> setCaption
2369+ optional $ controlSeq "caption" *> setCaption
2370+ spaces
2371+ optional label
2372+ spaces
23672373 optional lbreak
23682374 spaces
23692375 skipMany hline
@@ -2374,7 +2380,10 @@ simpTable envname hasWidthParameter = try $ do
23742380 rows <- sepEndBy (parseTableRow envname prefsufs)
23752381 (lbreak <* optional (skipMany hline))
23762382 spaces
2377− optional $ controlSeq "caption" *> skipopts *> setCaption
2383+ optional $ controlSeq "caption" *> setCaption
2384+ spaces
2385+ optional label
2386+ spaces
23782387 optional lbreak
23792388 spaces
23802389 let header'' = if null header'
@@ -2386,7 +2395,8 @@ simpTable envname hasWidthParameter = try $ do
23862395addTableCaption :: PandocMonad m => Blocks -> LP m Blocks
23872396addTableCaption = walkM go
23882397 where go (Table c als ws hs rs) = do
2389− (mbcapt, mblabel) <- sCaption <$> getState
2398+ mbcapt <- sCaption <$> getState
2399+ mblabel <- sLastLabel <$> getState
23902400 capt <- case (mbcapt, mblabel) of
23912401 (Just ils, Nothing) -> return $ toList ils
23922402 (Just ils, Just lab) -> do
@@ -2398,7 +2408,8 @@ addTableCaption = walkM go
23982408 (sLabels st) }
23992409 return $ toList ils -- add number??
24002410 (Nothing, _) -> return c
2401− return $ Table capt als ws hs rs
2411+ return $ maybe id (\ident -> Div (ident, [], []) . (:[])) mblabel $
2412+ Table capt als ws hs rs
24022413 go x = return x
24032414
24042415