git.iko.soy/typeable/pandoc
on master help sign in
LaTEX reader: properly resolve section numbers with \ref and chapters.
d77e8f4John MacFarlane
src/Text/Pandoc/Readers/LaTeX.hs+7−1
@@ -164,6 +164,7 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
164 , sInTableCell :: Bool
164 , sInTableCell :: Bool
165 , sLastHeaderNum :: HeaderNum
165 , sLastHeaderNum :: HeaderNum
166 , sLabels :: M.Map String [Inline]
166 , sLabels :: M.Map String [Inline]
167+ , sHasChapters :: Bool
167 , sToggles :: M.Map String Bool
168 , sToggles :: M.Map String Bool
168 }
169 }
169 deriving Show
170 deriving Show
@@ -183,6 +184,7 @@ defaultLaTeXState = LaTeXState{ sOptions = def
183 , sInTableCell = False
184 , sInTableCell = False
184 , sLastHeaderNum = HeaderNum []
185 , sLastHeaderNum = HeaderNum []
185 , sLabels = M.empty
186 , sLabels = M.empty
187+ , sHasChapters = False
186 , sToggles = M.empty
188 , sToggles = M.empty
187 }
189 }
188
190
@@ -1984,9 +1986,13 @@ section starred (ident, classes, kvs) lvl = do
1984 try (spaces >> controlSeq "label"
1986 try (spaces >> controlSeq "label"
1985 >> spaces >> toksToString <$> braced)
1987 >> spaces >> toksToString <$> braced)
1986 let classes' = if starred then "unnumbered" : classes else classes
1988 let classes' = if starred then "unnumbered" : classes else classes
1989+ when (lvl == 0) $
1990+ updateState $ \st -> st{ sHasChapters = True }
1987 unless starred $ do
1991 unless starred $ do
1988 hn <- sLastHeaderNum <$> getState
1992 hn <- sLastHeaderNum <$> getState
1993+ hasChapters <- sHasChapters <$> getState
1994+ let lvl' = lvl + if hasChapters then 1 else 0
1989− let num = incrementHeaderNum lvl hn
1995+ let num = incrementHeaderNum lvl' hn
1990 updateState $ \st -> st{ sLastHeaderNum = num }
1996 updateState $ \st -> st{ sLastHeaderNum = num }
1991 updateState $ \st -> st{ sLabels = M.insert lab
1997 updateState $ \st -> st{ sLabels = M.insert lab
1992 [Str (renderHeaderNum num)]
1998 [Str (renderHeaderNum num)]
test/command/4529.mdnew file+36
@@ -0,0 +1,36 @@
1+```
2+% pandoc -f latex -t plain
3+\chapter{First chapter}\label{sec:chp1}
4+The next chapter is Chapter~\ref{sec:chp2}.
5+\section{First section}\label{sec:chp1sec1}
6+The next section is Section~\ref{sec:chp2sec1}.
7+
8+\chapter{Second chapter}\label{sec:chp2}
9+The previous chapter is Chapter~\ref{sec:chp1}.
10+\section{First section}\label{sec:chp2sec1}
11+The previous section is Section~\ref{sec:chp1sec1}.
12+^D
13+
14+
15+FIRST CHAPTER
16+
17+
18+The next chapter is Chapter 2.
19+
20+
21+First section
22+
23+The next section is Section 2.1.
24+
25+
26+
27+SECOND CHAPTER
28+
29+
30+The previous chapter is Chapter 1.
31+
32+
33+First section
34+
35+The previous section is Section 1.1.
36+```