git.iko.soy/typeable/pandoc
on master help sign in
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.

commit345b33762eb4cc6d57d74c76c4757a6166ee5c13 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 5 Sep 2019, 22:22 (7 years ago) parent0534258
2 files changed+69−4
src/Text/Pandoc/Shared.hs+15−4 test/command/3057.md+54
src/Text/Pandoc/Shared.hs+15−4
@@ -523,10 +523,21 @@ hierarchicalizeWithIds (Header level attr@(_,classes,_) title':xs) = do
523523 sectionContents' <- hierarchicalizeWithIds sectionContents
524524 rest' <- hierarchicalizeWithIds rest
525525 return $ Sec level newnum attr title' sectionContents' : rest'
526−hierarchicalizeWithIds (Div ("refs",classes',kvs')
527− (Header level (ident,classes,kvs) title' : xs):ys) =
528− hierarchicalizeWithIds (Header level (ident,"references":classes,kvs)
529− title' : Div ("refs",classes',kvs') xs : ys)
526+hierarchicalizeWithIds (Div attr@(_,classes',_)
527+ (Header level (_,classes,_) title' : xs):ys)
528+ | not ("columns" `elem` classes')
529+ , not ("column" `elem` classes') = do
530+ lastnum <- S.get
531+ let lastnum' = take level lastnum
532+ let newnum = case length lastnum' of
533+ x | "unnumbered" `elem` classes -> []
534+ | x >= level -> init lastnum' ++ [last lastnum' + 1]
535+ | otherwise -> lastnum ++
536+ replicate (level - length lastnum - 1) 0 ++ [1]
537+ unless (null newnum) $ S.put newnum
538+ sectionContents' <- hierarchicalizeWithIds xs
539+ rest' <- hierarchicalizeWithIds ys
540+ return $ Sec level newnum attr title' sectionContents' : rest'
530541hierarchicalizeWithIds (x:rest) = do
531542 rest' <- hierarchicalizeWithIds rest
532543 return $ Blk x : rest'
test/command/3057.mdnew file+54
@@ -0,0 +1,54 @@
1+```
2+% pandoc -f markdown -t docbook
3+<div>
4+# one
5+<div>
6+## two
7+</div>
8+</div>
9+^D
10+<section>
11+ <title>one</title>
12+ <section>
13+ <title>two</title>
14+ <para>
15+ </para>
16+ </section>
17+</section>
18+```
19+
20+```
21+% pandoc -f markdown -t docbook
22+<div>
23+# one
24+<div>
25+# two
26+</div>
27+</div>
28+^D
29+<section>
30+ <title>one</title>
31+ <section>
32+ <title>two</title>
33+ <para>
34+ </para>
35+ </section>
36+</section>
37+```
38+
39+```
40+% pandoc -f markdown -t docbook
41+# one
42+<div>
43+# two
44+</div>
45+^D
46+<section xml:id="one">
47+ <title>one</title>
48+ <section>
49+ <title>two</title>
50+ <para>
51+ </para>
52+ </section>
53+</section>
54+```