git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: Better handling of figure and table with caption.

We now look for a \caption inside the environment; if one is found, it is attached to the graphic or tabular found there. Closes #1204.

commit69a7c9f634b1e9133f7c6e4e135404bdf8caa3ab authorJohn MacFarlane <jgm@berkeley.edu> dateTue 25 Mar 2014, 23:10 (13 years ago) parent0934c44
1 file changed+34−11
src/Text/Pandoc/Readers/LaTeX.hs+34−11
src/Text/Pandoc/Readers/LaTeX.hs+34−11
@@ -302,6 +302,7 @@ blockCommands = M.fromList $
302302 , ("item", skipopts *> loose_item)
303303 , ("documentclass", skipopts *> braced *> preamble)
304304 , ("centerline", (para . trimInlines) <$> (skipopts *> tok))
305+ , ("caption", tok >>= setCaption)
305306 ] ++ map ignoreBlocks
306307 -- these commands will be ignored unless --parse-raw is specified,
307308 -- in which case they will appear as raw latex blocks
@@ -323,6 +324,14 @@ blockCommands = M.fromList $
323324addMeta :: ToMetaValue a => String -> a -> LP ()
324325addMeta field val = updateState $ setMeta field val
325326
327+setCaption :: Inlines -> LP Blocks
328+setCaption ils = do
329+ updateState $ \st -> st{ stateCaption = Just ils }
330+ return mempty
331+
332+resetCaption :: LP ()
333+resetCaption = updateState $ \st -> st{ stateCaption = Nothing }
334+
326335authors :: LP ()
327336authors = try $ do
328337 char '{'
@@ -523,18 +532,12 @@ inlineCommands = M.fromList $
523532
524533mkImage :: String -> LP Inlines
525534mkImage src = do
526− -- try for a caption
527− (alt, tit) <- option (str "image", "") $ try $ do
528− spaces
529− controlSeq "caption"
530− optional (char '*')
531− ils <- grouped inline
532− return (ils, "fig:")
535+ let alt = str "image"
533536 case takeExtension src of
534537 "" -> do
535538 defaultExt <- getOption readerDefaultImageExtension
536− return $ image (addExtension src defaultExt) tit alt
537− _ -> return $ image src tit alt
539+ return $ image (addExtension src defaultExt) "" alt
540+ _ -> return $ image src "" alt
538541
539542inNote :: Inlines -> Inlines
540543inNote ils =
@@ -888,13 +891,33 @@ rawLaTeXInline = do
888891 raw <- (snd <$> withRaw inlineCommand) <|> (snd <$> withRaw blockCommand)
889892 RawInline "latex" <$> applyMacros' raw
890893
894+addImageCaption :: Blocks -> LP Blocks
895+addImageCaption = walkM go
896+ where go (Image alt (src,tit)) = do
897+ mbcapt <- stateCaption <$> getState
898+ case mbcapt of
899+ Just ils -> return (Image (toList ils) (src, "fig:"))
900+ Nothing -> return (Image alt (src,tit))
901+ go x = return x
902+
903+addTableCaption :: Blocks -> LP Blocks
904+addTableCaption = walkM go
905+ where go (Table c als ws hs rs) = do
906+ mbcapt <- stateCaption <$> getState
907+ case mbcapt of
908+ Just ils -> return (Table (toList ils) als ws hs rs)
909+ Nothing -> return (Table c als ws hs rs)
910+ go x = return x
911+
891912environments :: M.Map String (LP Blocks)
892913environments = M.fromList
893914 [ ("document", env "document" blocks <* skipMany anyChar)
894915 , ("letter", env "letter" letter_contents)
895− , ("figure", env "figure" $ skipopts *> blocks)
916+ , ("figure", env "figure" $
917+ resetCaption *> skipopts *> blocks >>= addImageCaption)
896918 , ("center", env "center" blocks)
897− , ("table", env "table" $ skipopts *> blocks)
919+ , ("table", env "table" $
920+ resetCaption *> skipopts *> blocks >>= addTableCaption)
898921 , ("tabular", env "tabular" simpTable)
899922 , ("quote", blockQuote <$> env "quote" blocks)
900923 , ("quotation", blockQuote <$> env "quotation" blocks)