diff options
| author | John MacFarlane <[email protected]> | 2024-09-07 17:39:34 -0700 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2024-09-08 08:29:23 -0700 |
| commit | 3e6eb1cde217fe2b7715c46a367fed6b91c9fc96 (patch) | |
| tree | 50f31967b18a197f137b4be7af0496638f51d581 /src | |
| parent | a27dad6f688e4b30615f9619c81b276057078717 (diff) | |
Text.Pandoc.Shared: add `makeSectionsWithOffsets` [API change].
This is like `makeSections` but has an additional parameter
specifying number offsets, for use with the `--number-offset` option.
Use `makeSectionsWithOffsets` in HTML writer instead of ad hoc
and inefficient number-adjusting code.
Clarify MANUAL.txt: the `--number-offset` option should only
directly affect numbering of the first section heading in
a document; subsequent headings will increment normally.
Fix test output for #5071 to reflect this.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Text/Pandoc/Shared.hs | 15 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/HTML.hs | 28 |
2 files changed, 14 insertions, 29 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index a2a39ac43..6aa8601f2 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -50,6 +50,7 @@ module Text.Pandoc.Shared ( linesToPara, figureDiv, makeSections, + makeSectionsWithOffsets, combineAttr, uniqueIdent, inlineListToIdentifier, @@ -512,12 +513,20 @@ textToIdentifier exts = -- adjusted so that the lowest header level is n. -- (There may still be gaps in header level if the author leaves them.) makeSections :: Bool -> Maybe Int -> [Block] -> [Block] -makeSections numbering mbBaseLevel bs = - S.evalState (go bs) [] +makeSections = makeSectionsWithOffsets [] + +-- | Like 'makeSections', but with a parameter for number offsets +-- (a list of 'Int's, the first of which is added to the level 1 +-- section number, the second to the level 2, and so on). +makeSectionsWithOffsets :: [Int] -> Bool -> Maybe Int -> [Block] -> [Block] +makeSectionsWithOffsets numoffsets numbering mbBaseLevel bs = + S.evalState (go bs) numoffsets where getLevel (Header level _ _) = Min level getLevel _ = Min 99 - minLevel = getMin $ query getLevel bs + minLevel = if null numoffsets || all (== 0) numoffsets + then getMin $ query getLevel bs + else 1 -- see #5071, for backwards compatibility go :: [Block] -> S.State [Int] [Block] go (Header level (ident,classes,kvs) title':xs) = do lastnum <- S.get diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index 076f11d7a..916aee675 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -289,8 +289,8 @@ pandocToHtml opts (Pandoc meta blocks) = do lookupMetaString "description" meta slideVariant <- gets stSlideVariant abstractTitle <- translateTerm Abstract - let sects = adjustNumbers opts $ - makeSections (writerNumberSections opts) Nothing $ + let sects = makeSectionsWithOffsets + (writerNumberOffset opts) (writerNumberSections opts) Nothing $ if slideVariant == NoSlides then blocks else prepSlides slideLevel blocks @@ -729,30 +729,6 @@ dimensionsToAttrList attr = go Width ++ go Height (Just x) -> [("style", tshow dir <> ":" <> tshow x)] Nothing -> [] -adjustNumbers :: WriterOptions -> [Block] -> [Block] -adjustNumbers opts doc = - if all (==0) (writerNumberOffset opts) - then doc - else walk go doc - where - go (Div (ident,"section":classes,kvs) lst@(Header level _ _ : _)) = - Div (ident,"section":classes,map (fixnum level) kvs) lst - go (Header level (ident,classes,kvs) lst) = - Header level (ident,classes,map (fixnum level) kvs) lst - go x = x - fixnum level ("number",num) = ("number", - showSecNum $ zipWith (+) - (writerNumberOffset opts ++ repeat 0) - (padTo level $ - map (fromMaybe 0 . safeRead) $ - T.split (=='.') num)) - fixnum _ x = x - padTo n xs = - case n - length xs of - x | x > 0 -> replicate x 0 ++ xs - | otherwise -> xs - showSecNum = T.intercalate "." . map tshow - blockToHtmlInner :: PandocMonad m => WriterOptions -> Block -> StateT WriterState m Html blockToHtmlInner opts (Plain lst) = inlineListToHtml opts lst blockToHtmlInner opts (Para lst) = do |
