Skip to content

Commit

Permalink
Text.Pandoc.Shared: add makeSectionsWithOffsets [API change].
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jgm committed Sep 8, 2024
1 parent a27dad6 commit 3e6eb1c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 41 deletions.
19 changes: 11 additions & 8 deletions MANUAL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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]`

Expand Down
15 changes: 12 additions & 3 deletions src/Text/Pandoc/Shared.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module Text.Pandoc.Shared (
linesToPara,
figureDiv,
makeSections,
makeSectionsWithOffsets,
combineAttr,
uniqueIdent,
inlineListToIdentifier,
Expand Down Expand Up @@ -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
Expand Down
28 changes: 2 additions & 26 deletions src/Text/Pandoc/Writers/HTML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/command/5071.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
```

Expand All @@ -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>
```

0 comments on commit 3e6eb1c

Please sign in to comment.