git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: improve caption and label parsing.

- Don't emit empty Span elements for labels. - Put tables with labels in a surrounding Div.

commit3a79f37d8814d3870f7243a23b54f899a1e67065 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 12 Feb 2020, 17:43 (7 years ago) parent1433aaa
5 files changed+53−39
src/Text/Pandoc/Readers/LaTeX.hs+32−21 src/Text/Pandoc/Readers/LaTeX/Parsing.hs+4−2 test/command/2118.md+1−1 test/command/6137.md+12−11 test/command/refs.md+4−4
src/Text/Pandoc/Readers/LaTeX.hs+32−21
@@ -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
src/Text/Pandoc/Readers/LaTeX/Parsing.hs+4−2
@@ -115,12 +115,13 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
115115 , sLogMessages :: [LogMessage]
116116 , sIdentifiers :: Set.Set Text
117117 , sVerbatimMode :: Bool
118− , sCaption :: (Maybe Inlines, Maybe Text)
118+ , sCaption :: Maybe Inlines
119119 , sInListItem :: Bool
120120 , sInTableCell :: Bool
121121 , sLastHeaderNum :: DottedNum
122122 , sLastFigureNum :: DottedNum
123123 , sLastTableNum :: DottedNum
124+ , sLastLabel :: Maybe Text
124125 , sLabels :: M.Map Text [Inline]
125126 , sHasChapters :: Bool
126127 , sToggles :: M.Map Text Bool
@@ -137,12 +138,13 @@ defaultLaTeXState = LaTeXState{ sOptions = def
137138 , sLogMessages = []
138139 , sIdentifiers = Set.empty
139140 , sVerbatimMode = False
140− , sCaption = (Nothing, Nothing)
141+ , sCaption = Nothing
141142 , sInListItem = False
142143 , sInTableCell = False
143144 , sLastHeaderNum = DottedNum []
144145 , sLastFigureNum = DottedNum []
145146 , sLastTableNum = DottedNum []
147+ , sLastLabel = Nothing
146148 , sLabels = M.empty
147149 , sHasChapters = False
148150 , sToggles = M.empty
test/command/2118.md+1−1
@@ -7,5 +7,5 @@
77 \label{fig:setminus}
88\end{figure}
99^D
10−[Para [Image ("fig:setminus",[],[("width","80%")]) [Str "Set",Space,Str "subtraction",Span ("",[],[("label","fig:setminus")]) []] ("setminus.png","fig:")]]
10+[Para [Image ("fig:setminus",[],[("width","80%")]) [Str "Set",Space,Str "subtraction"] ("setminus.png","fig:")]]
1111```
test/command/6137.md+12−11
@@ -16,16 +16,17 @@ This reference to Figure \ref{fig:label} works fine.
1616\end{figure}
1717^D
1818[Para [Str "This",Space,Str "reference",Space,Str "to",Space,Str "Table",Space,Link ("",[],[("reference-type","ref"),("reference","tbl:label")]) [Str "1"] ("#tbl:label",""),Space,Str "doesn\8217t",Space,Str "work."]
19−,Table [Str "This",Space,Str "caption",Space,Str "has",Space,Str "no",Space,Str "number.",Span ("",[],[("label","tbl:label")]) []] [AlignLeft,AlignCenter,AlignRight] [0.0,0.0,0.0]
20− [[]
21− ,[]
22− ,[]]
23− [[[Plain [Str "\8212\8212\8211"]]
24− ,[Plain [Str "\8212\8212\8211"]]
25− ,[Plain [Str "\8212\8212\8211"]]]
26− ,[[Plain [Str "\8212\8212\8211"]]
27− ,[Plain [Str "\8212\8212\8211"]]
28− ,[Plain [Str "\8212\8212\8211"]]]]
19+,Div ("tbl:label",[],[])
20+ [Table [Str "This",Space,Str "caption",Space,Str "has",Space,Str "no",Space,Str "number."] [AlignLeft,AlignCenter,AlignRight] [0.0,0.0,0.0]
21+ [[]
22+ ,[]
23+ ,[]]
24+ [[[Plain [Str "\8212\8212\8211"]]
25+ ,[Plain [Str "\8212\8212\8211"]]
26+ ,[Plain [Str "\8212\8212\8211"]]]
27+ ,[[Plain [Str "\8212\8212\8211"]]
28+ ,[Plain [Str "\8212\8212\8211"]]
29+ ,[Plain [Str "\8212\8212\8211"]]]]]
2930,Para [Str "This",Space,Str "reference",Space,Str "to",Space,Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:label")]) [Str "1"] ("#fig:label",""),Space,Str "works",Space,Str "fine."]
30−,Para [Image ("fig:label",[],[("width","\\textwidth")]) [Str "A",Space,Str "numbered",Space,Str "caption,",Space,Str "if",Space,Str "I",Space,Str "use",Space,Str "pandoc-crossref.",Span ("",[],[("label","fig:label")]) []] ("example.png","fig:")]]
31+,Para [Image ("fig:label",[],[("width","\\textwidth")]) [Str "A",Space,Str "numbered",Space,Str "caption,",Space,Str "if",Space,Str "I",Space,Str "use",Space,Str "pandoc-crossref."] ("example.png","fig:")]]
3132```
test/command/refs.md+4−4
@@ -42,7 +42,7 @@ Accuracy~\eqref{eq:Accuracy} is the proportion, measuring true results among all
4242
4343Figure \ref{fig:Logo} illustrated the SVG logo
4444^D
45−[Para [Image ("fig:Logo",[],[]) [Str "Logo",Span ("",[],[("label","fig:Logo")]) []] ("command/SVG_logo.svg","fig:")]
45+[Para [Image ("fig:Logo",[],[]) [Str "Logo"] ("command/SVG_logo.svg","fig:")]
4646,Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:Logo")]) [Str "1"] ("#fig:Logo",""),Space,Str "illustrated",Space,Str "the",Space,Str "SVG",Space,Str "logo"]]
4747```
4848
@@ -78,11 +78,11 @@ Figure \ref{fig:Logo2} illustrated the SVG logo
7878Figure \ref{fig:Logo3} illustrated the SVG logo
7979^D
8080[Header 1 ("one",[],[]) [Str "One"]
81−,Para [Image ("fig:Logo",[],[]) [Str "Logo",Span ("",[],[("label","fig:Logo")]) []] ("command/SVG_logo.svg","fig:")]
82−,Para [Image ("fig:Logo2",[],[]) [Str "Logo2",Span ("",[],[("label","fig:Logo2")]) []] ("command/SVG_logo2.svg","fig:")]
81+,Para [Image ("fig:Logo",[],[]) [Str "Logo"] ("command/SVG_logo.svg","fig:")]
82+,Para [Image ("fig:Logo2",[],[]) [Str "Logo2"] ("command/SVG_logo2.svg","fig:")]
8383,Header 1 ("two",[],[]) [Str "Two"]
8484,Header 2 ("subone",[],[]) [Str "Subone"]
85−,Para [Image ("fig:Logo3",[],[]) [Str "Logo3",Span ("",[],[("label","fig:Logo3")]) []] ("command/SVG_logo3.svg","fig:")]
85+,Para [Image ("fig:Logo3",[],[]) [Str "Logo3"] ("command/SVG_logo3.svg","fig:")]
8686,Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:Logo")]) [Str "1.1"] ("#fig:Logo",""),Space,Str "illustrated",Space,Str "the",Space,Str "SVG",Space,Str "logo"]
8787,Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:Logo2")]) [Str "1.2"] ("#fig:Logo2",""),Space,Str "illustrated",Space,Str "the",Space,Str "SVG",Space,Str "logo"]
8888,Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:Logo3")]) [Str "2.1"] ("#fig:Logo3",""),Space,Str "illustrated",Space,Str "the",Space,Str "SVG",Space,Str "logo"]]