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 | |
| 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.
| -rw-r--r-- | MANUAL.txt | 19 | ||||
| -rw-r--r-- | src/Text/Pandoc/Shared.hs | 15 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/HTML.hs | 28 | ||||
| -rw-r--r-- | test/command/5071.md | 8 |
4 files changed, 29 insertions, 41 deletions
diff --git a/MANUAL.txt b/MANUAL.txt index 2edef40d9..865fa8cb1 100644 --- a/MANUAL.txt +++ b/MANUAL.txt @@ -1107,14 +1107,17 @@ header when requesting a document from a URL: `--number-offset=`*NUMBER*[`,`*NUMBER*`,`*...*] -: Offset for section headings in HTML output (ignored in other - output formats). The first number is added to the section number for - top-level headings, the second for second-level headings, and so on. - So, for example, if you want the first top-level heading in your - document to be numbered "6", specify `--number-offset=5`. - If your document starts with a level-2 heading which you want to - be numbered "1.5", specify `--number-offset=1,4`. - Offsets are 0 by default. Implies `--number-sections`. +: Offsets for section heading numbers. The first number is added + to the section number for level-1 headings, the second for + level-2 headings, and so on. So, for example, if you + want the first level-1 heading in your document to be + numbered "6" instead of "1", specify `--number-offset=5`. + If your document starts with a level-2 heading which you want + to be numbered "1.5", specify `--number-offset=1,4`. + `--number-offset` only directly affects the number of the + first section heading in a document; subsequent numbers + increment in the normal way. Implies `--number-sections`. + Currently this feature only affects HTML output. `--listings[=true|false]` 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 diff --git a/test/command/5071.md b/test/command/5071.md index 6bbeb85fc..0347d3c46 100644 --- a/test/command/5071.md +++ b/test/command/5071.md @@ -49,8 +49,8 @@ giving numbers like 0.1, when `--number-offset` is used: ^D <h2 data-number="2.3" id="first-section"><span class="header-section-number">2.3</span> First section</h2> -<h3 data-number="2.3.3" id="subhead"><span -class="header-section-number">2.3.3</span> Subhead</h3> +<h3 data-number="2.3.1" id="subhead"><span +class="header-section-number">2.3.1</span> Subhead</h3> ``` @@ -62,7 +62,7 @@ class="header-section-number">2.3.3</span> Subhead</h3> ^D <h2 data-number="0.3" id="first-section"><span class="header-section-number">0.3</span> First section</h2> -<h3 data-number="0.3.3" id="subhead"><span -class="header-section-number">0.3.3</span> Subhead</h3> +<h3 data-number="0.3.1" id="subhead"><span +class="header-section-number">0.3.1</span> Subhead</h3> ``` |
