git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: track header numbers and correlate with labels.
commit253a7c620136bcba1a0134898b6a8cf3dcf47eca authorJohn MacFarlane <jgm@berkeley.edu> dateSun 13 Aug 2017, 11:30 (9 years ago) parent2845ab5
1 file changed+49−22
src/Text/Pandoc/Readers/LaTeX.hs+49−22
src/Text/Pandoc/Readers/LaTeX.hs+49−22
@@ -119,6 +119,19 @@ parseLaTeX = do
119119-- Left e -> error (show e)
120120-- Right r -> return r
121121
122+newtype HeaderNum = HeaderNum [Int]
123+ deriving (Show)
124+
125+renderHeaderNum :: HeaderNum -> String
126+renderHeaderNum (HeaderNum xs) =
127+ intercalate "." (map show xs)
128+
129+incrementHeaderNum :: Int -> HeaderNum -> HeaderNum
130+incrementHeaderNum level (HeaderNum ns) = HeaderNum $
131+ case reverse (take level (ns ++ repeat 0)) of
132+ (x:xs) -> reverse (x+1 : xs)
133+ [] -> [] -- shouldn't happen
134+
122135data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
123136 , sMeta :: Meta
124137 , sQuoteContext :: QuoteContext
@@ -131,6 +144,8 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
131144 , sCaption :: Maybe Inlines
132145 , sInListItem :: Bool
133146 , sInTableCell :: Bool
147+ , sLastHeaderNum :: HeaderNum
148+ , sLabels :: M.Map String Inlines
134149 }
135150 deriving Show
136151
@@ -147,6 +162,8 @@ defaultLaTeXState = LaTeXState{ sOptions = def
147162 , sCaption = Nothing
148163 , sInListItem = False
149164 , sInTableCell = False
165+ , sLastHeaderNum = HeaderNum []
166+ , sLabels = M.empty
150167 }
151168
152169instance PandocMonad m => HasQuoteContext LaTeXState m where
@@ -1448,14 +1465,16 @@ treatAsInline = Set.fromList
14481465dolabel :: PandocMonad m => LP m Inlines
14491466dolabel = do
14501467 v <- braced
1451− return $ spanWith ("",[],[("label", toksToString v)])
1468+ return $ spanWith ("",[],[("label", toksToString v)])
14521469 $ inBrackets $ str $ toksToString v
14531470
14541471doref :: PandocMonad m => String -> LP m Inlines
14551472doref cls = do
14561473 v <- braced
1457− return $ spanWith ("",[],[("reference-type", cls), ("reference", toksToString v)])
1458− $ inBrackets $ str $ toksToString v
1474+ let refstr = toksToString v
1475+ return $ spanWith ("",[],[ ("reference-type", cls)
1476+ , ("reference", refstr)])
1477+ $ inBrackets $ str refstr
14591478
14601479lookupListDefault :: (Show k, Ord k) => v -> [k] -> M.Map k v -> v
14611480lookupListDefault d = (fromMaybe d .) . lookupList
@@ -1688,14 +1707,22 @@ looseItem = do
16881707resetCaption :: PandocMonad m => LP m ()
16891708resetCaption = updateState $ \st -> st{ sCaption = Nothing }
16901709
1691−section :: PandocMonad m => Attr -> Int -> LP m Blocks
1692−section (ident, classes, kvs) lvl = do
1710+section :: PandocMonad m => Bool -> Attr -> Int -> LP m Blocks
1711+section starred (ident, classes, kvs) lvl = do
16931712 skipopts
16941713 contents <- grouped inline
16951714 lab <- option ident $
16961715 try (spaces >> controlSeq "label"
16971716 >> spaces >> toksToString <$> braced)
1698− attr' <- registerHeader (lab, classes, kvs) contents
1717+ let classes' = if starred then "unnumbered" : classes else classes
1718+ unless starred $ do
1719+ hn <- sLastHeaderNum <$> getState
1720+ let num = incrementHeaderNum lvl hn
1721+ updateState $ \st -> st{ sLastHeaderNum = num }
1722+ updateState $ \st -> st{ sLabels = M.insert lab
1723+ (str (renderHeaderNum num))
1724+ (sLabels st) }
1725+ attr' <- registerHeader (lab, classes', kvs) contents
16991726 return $ headerWith attr' lvl contents
17001727
17011728blockCommand :: PandocMonad m => LP m Blocks
@@ -1756,23 +1783,23 @@ blockCommands = M.fromList $
17561783 -- Koma-script metadata commands
17571784 , ("dedication", mempty <$ (skipopts *> tok >>= addMeta "dedication"))
17581785 -- sectioning
1759− , ("part", section nullAttr (-1))
1760− , ("part*", section nullAttr (-1))
1761− , ("chapter", section nullAttr 0)
1762− , ("chapter*", section ("",["unnumbered"],[]) 0)
1763− , ("section", section nullAttr 1)
1764− , ("section*", section ("",["unnumbered"],[]) 1)
1765− , ("subsection", section nullAttr 2)
1766− , ("subsection*", section ("",["unnumbered"],[]) 2)
1767− , ("subsubsection", section nullAttr 3)
1768− , ("subsubsection*", section ("",["unnumbered"],[]) 3)
1769− , ("paragraph", section nullAttr 4)
1770− , ("paragraph*", section ("",["unnumbered"],[]) 4)
1771− , ("subparagraph", section nullAttr 5)
1772− , ("subparagraph*", section ("",["unnumbered"],[]) 5)
1786+ , ("part", section False nullAttr (-1))
1787+ , ("part*", section True nullAttr (-1))
1788+ , ("chapter", section False nullAttr 0)
1789+ , ("chapter*", section True ("",["unnumbered"],[]) 0)
1790+ , ("section", section False nullAttr 1)
1791+ , ("section*", section True ("",["unnumbered"],[]) 1)
1792+ , ("subsection", section False nullAttr 2)
1793+ , ("subsection*", section True ("",["unnumbered"],[]) 2)
1794+ , ("subsubsection", section False nullAttr 3)
1795+ , ("subsubsection*", section True ("",["unnumbered"],[]) 3)
1796+ , ("paragraph", section False nullAttr 4)
1797+ , ("paragraph*", section True ("",["unnumbered"],[]) 4)
1798+ , ("subparagraph", section False nullAttr 5)
1799+ , ("subparagraph*", section True ("",["unnumbered"],[]) 5)
17731800 -- beamer slides
1774− , ("frametitle", section nullAttr 3)
1775− , ("framesubtitle", section nullAttr 4)
1801+ , ("frametitle", section False nullAttr 3)
1802+ , ("framesubtitle", section False nullAttr 4)
17761803 -- letters
17771804 , ("opening", (para . trimInlines) <$> (skipopts *> tok))
17781805 , ("closing", skipopts *> closing)