Add section indexing and update autoref functionality in ChaoDoc module

This commit is contained in:
2026-04-20 15:07:24 +08:00
parent a0affeb5c4
commit 63010da18d
2 changed files with 39 additions and 2 deletions

View File

@@ -20,3 +20,4 @@
\newcommand{\del}{\setminus} \newcommand{\del}{\setminus}
\newcommand{\con}{/} \newcommand{\con}{/}
\newcommand{\minor}{\times} \newcommand{\minor}{\times}
\newcommand{\FF}{\mathbb{F}}

View File

@@ -13,6 +13,7 @@ where
import Control.Monad (guard) import Control.Monad (guard)
import Control.Monad.State import Control.Monad.State
import qualified Control.Monad.State.Strict as Strict
import Data.Char (isSpace) import Data.Char (isSpace)
import Data.Either import Data.Either
import Data.Functor import Data.Functor
@@ -105,7 +106,8 @@ incrementalBlock =
"Proposition", "Proposition",
"Corollary", "Corollary",
"Observation", "Observation",
"Figure" "Figure",
"Table"
] ]
otherBlock :: [Text] otherBlock :: [Text]
@@ -221,7 +223,41 @@ autoref _ y = y
autorefFilter :: Pandoc -> Pandoc autorefFilter :: Pandoc -> Pandoc
autorefFilter x = walk (autoref links) x autorefFilter x = walk (autoref links) x
where where
links = query theoremIndex x links = query theoremIndex x ++ sectionIndex x
sectionIndex :: Pandoc -> [(Text, (Text, Text))]
sectionIndex doc = snd $ Strict.execState (walkM collectSectionIndex doc) ([], [])
collectSectionIndex :: Block -> Strict.State ([Int], [(Text, (Text, Text))]) Block
collectSectionIndex header@(Header level attr@(ident, _, _) _)
| isNumberedSectionHeader attr = do
(curNums, refs) <- Strict.get
let nextNums = nextSectionNumbers level curNums
refs'
| isSectionIdentifier ident = (ident, ("Section", renderSectionNumber nextNums)) : refs
| otherwise = refs
Strict.put (nextNums, refs')
pure header
collectSectionIndex block = pure block
isNumberedSectionHeader :: Attr -> Bool
isNumberedSectionHeader (_, classes, _) = "unnumbered" `notElem` classes
isSectionIdentifier :: Text -> Bool
isSectionIdentifier = T.isPrefixOf "sec:"
nextSectionNumbers :: Int -> [Int] -> [Int]
nextSectionNumbers level nums =
reverse $ bumpLast $ reverse prefix
where
prefix = take level (nums ++ repeat 0)
bumpLast :: [Int] -> [Int]
bumpLast [] = [1]
bumpLast (x : xs) = (x + 1) : xs
renderSectionNumber :: [Int] -> Text
renderSectionNumber = T.intercalate "." . map (pack . show)
equationFilter :: Pandoc -> Pandoc equationFilter :: Pandoc -> Pandoc
equationFilter doc = walk (equationAutoref links) processed equationFilter doc = walk (equationAutoref links) processed