aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2024-02-03 22:19:29 -0800
committerJohn MacFarlane <[email protected]>2024-02-03 22:19:29 -0800
commit03bb426a4a65918bfd6fbd63d5bb32a657df5568 (patch)
treeebd0629088e86f25c35467c46ba19ac1ce37ec36 /src
parent6fc8a80c2cdeaf71b7fe6d20c35b46ef562ca67c (diff)
Shared: `makeSections` behavior changes.
+ When the optional base level parameter is provided, we no longer ensure that the sequence of heading levels is gapless [behavior change]. Instead, we set the lowest heading level to the specified base level, and adjust the others accordingly. If an author wants to skip a level, e.g. from level 1 to level 3, they can do that. In general, the heading levels specified in the source document are preserved; `makeSections` only puts them into a hierarchical structure. Closes #9398. + Section numbers are now assigned so that the top level gets `1`, no matter what heading level is used. So, even if the top heading level is 2, numbers will be `1`, `2`, etc. rather than `0.1`, `0.2`, as in the past. Closes #5071. + We revert to the old behavior when the `--number-offset` option is used. So, for example, if a document begins with a level-3 heading, and `--number-offset=1,2` is used, the top-level section numbers will be `1.2.1`, `1.2.2`, etc. This is mainly for backwards-compatibility.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Shared.hs29
-rw-r--r--src/Text/Pandoc/Writers/HTML.hs17
2 files changed, 27 insertions, 19 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index b18ec98c5..1c26e143f 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -96,8 +96,9 @@ import Data.Char (isAlpha, isLower, isSpace, isUpper, toLower, isAlphaNum,
import Data.List (find, foldl', groupBy, intercalate, intersperse,
union, sortOn)
import qualified Data.Map as M
-import Data.Maybe (mapMaybe, fromMaybe)
-import Data.Monoid (Any (..))
+import Data.Maybe (mapMaybe)
+import Data.Monoid (Any (..) )
+import Data.Semigroup (Min (..))
import Data.Sequence (ViewL (..), ViewR (..), viewl, viewr)
import qualified Data.Set as Set
import qualified Data.Text as T
@@ -507,26 +508,28 @@ textToIdentifier exts =
-- element a Header). If the 'numbering' parameter is True, Header
-- numbers are added via the number attribute on the header.
-- If the baseLevel parameter is Just n, Header levels are
--- adjusted to be gapless starting at level n.
+-- 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) (mbBaseLevel, [])
+ S.evalState (go bs) []
where
- go :: [Block] -> S.State (Maybe Int, [Int]) [Block]
+ getLevel (Header level _ _) = Min level
+ getLevel _ = Min 99
+ minLevel = getMin $ query getLevel bs
+ go :: [Block] -> S.State [Int] [Block]
go (Header level (ident,classes,kvs) title':xs) = do
- (mbLevel, lastnum) <- S.get
- let level' = fromMaybe level mbLevel
+ lastnum <- S.get
+ let level' = maybe level (\n -> n + level - minLevel) mbBaseLevel
let adjustNum lev numComponent
- | lev < level' = numComponent
- | lev == level' = numComponent + 1
+ | lev < level = numComponent
+ | lev == level = numComponent + 1
| otherwise = 0
- let newnum = zipWith adjustNum [(fromMaybe 1 mbBaseLevel)..level']
+ let newnum = zipWith adjustNum [minLevel..level]
(lastnum ++ repeat 0)
- unless (null newnum) $ S.modify $ \(mbl, _) -> (mbl, newnum)
+ unless (null newnum) $ S.put newnum
let (sectionContents, rest) = break (headerLtEq level) xs
- S.modify $ \(_, ln) -> (fmap (+ 1) mbLevel, ln)
sectionContents' <- go sectionContents
- S.modify $ \(_, ln) -> (mbLevel, ln)
rest' <- go rest
let kvs' = -- don't touch number if already present
case lookup "number" kvs of
diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs
index 18e10f102..c7a4703ae 100644
--- a/src/Text/Pandoc/Writers/HTML.hs
+++ b/src/Text/Pandoc/Writers/HTML.hs
@@ -721,17 +721,22 @@ adjustNumbers opts doc =
then doc
else walk go doc
where
- go (Div (ident,"section":classes,kvs) lst) =
- Div (ident,"section":classes,map fixnum kvs) lst
+ 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 kvs) lst
+ Header level (ident,classes,map (fixnum level) kvs) lst
go x = x
- fixnum ("number",num) = ("number",
+ fixnum level ("number",num) = ("number",
showSecNum $ zipWith (+)
(writerNumberOffset opts ++ repeat 0)
- (map (fromMaybe 0 . safeRead) $
+ (padTo level $
+ map (fromMaybe 0 . safeRead) $
T.split (=='.') num))
- fixnum x = x
+ 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