Shared.hierarchicalize: improve handling of div and section structure.

Previously Divs were opaque to hierarchicalize, so headings
inside divs didn't get into the table of contents, for
example (#3057).

Now hierarchicalize treats Divs as sections when appropriate.
For example, these structures both yield a section and a
subsection:

``` html
<div>
<h1>one</h1>
<div>
<h2>two</h2>
</div>
</div>
```
``` html
<div>
<h1>one</h1>
<div>
<h1>two</h1>
</div>
</div>
```

Note that

``` html
<h1>one</h1>
<div>
<h2>two</h2>
</div>
<h1>three</h1>
```

gets parsed as the structure

    one
      two
    three

which may not always be desirable.

Closes #3057.
This commit is contained in:
John MacFarlane 2019-09-05 22:22:07 -07:00
parent 0534258780
commit 345b33762e
2 changed files with 69 additions and 4 deletions

View file

@ -523,10 +523,21 @@ hierarchicalizeWithIds (Header level attr@(_,classes,_) title':xs) = do
sectionContents' <- hierarchicalizeWithIds sectionContents
rest' <- hierarchicalizeWithIds rest
return $ Sec level newnum attr title' sectionContents' : rest'
hierarchicalizeWithIds (Div ("refs",classes',kvs')
(Header level (ident,classes,kvs) title' : xs):ys) =
hierarchicalizeWithIds (Header level (ident,"references":classes,kvs)
title' : Div ("refs",classes',kvs') xs : ys)
hierarchicalizeWithIds (Div attr@(_,classes',_)
(Header level (_,classes,_) title' : xs):ys)
| not ("columns" `elem` classes')
, not ("column" `elem` classes') = do
lastnum <- S.get
let lastnum' = take level lastnum
let newnum = case length lastnum' of
x | "unnumbered" `elem` classes -> []
| x >= level -> init lastnum' ++ [last lastnum' + 1]
| otherwise -> lastnum ++
replicate (level - length lastnum - 1) 0 ++ [1]
unless (null newnum) $ S.put newnum
sectionContents' <- hierarchicalizeWithIds xs
rest' <- hierarchicalizeWithIds ys
return $ Sec level newnum attr title' sectionContents' : rest'
hierarchicalizeWithIds (x:rest) = do
rest' <- hierarchicalizeWithIds rest
return $ Blk x : rest'

54
test/command/3057.md Normal file
View file

@ -0,0 +1,54 @@
```
% pandoc -f markdown -t docbook
<div>
# one
<div>
## two
</div>
</div>
^D
<section>
<title>one</title>
<section>
<title>two</title>
<para>
</para>
</section>
</section>
```
```
% pandoc -f markdown -t docbook
<div>
# one
<div>
# two
</div>
</div>
^D
<section>
<title>one</title>
<section>
<title>two</title>
<para>
</para>
</section>
</section>
```
```
% pandoc -f markdown -t docbook
# one
<div>
# two
</div>
^D
<section xml:id="one">
<title>one</title>
<section>
<title>two</title>
<para>
</para>
</section>
</section>
```