docbook reader: fix nesting of chapters and sections (#5864)

* Set dbBook to true when traversing a chapter too.
  Currently, a `<title/>` in a chapter and in a `<section/>` below that
  chapter have the same level if they're not inside a `<book/>`.

  This can happen in a multi-file book project. Also see the example at
  https://tdg.docbook.org/tdg/4.5/chapter.html

  Co-authored-by: Félix Baylac-Jacqué <felix@alternativebit.fr>

* Add docbook-chapter test

  This tests nested `<section/>` and makes sure `<title/>` in the first
  `<section/>` below `<chapter/>` is one level deeper than the `<chapter/>`'s
  `<title/>`, also when not inside a `<book/>`.

  Co-authored-by: Félix Baylac-Jacqué <felix@alternativebit.fr>
This commit is contained in:
Florian Klink 2019-10-30 16:51:33 +01:00 committed by John MacFarlane
parent 080a3cdaeb
commit c6e936dec2
5 changed files with 51 additions and 1 deletions

View file

@ -215,6 +215,7 @@ extra-source-files:
test/command/3510-export.latex
test/command/3510-src.hs
test/command/3971b.tex
test/docbook-chapter.docbook
test/docbook-reader.docbook
test/docbook-xref.docbook
test/html-reader.html

View file

@ -726,7 +726,7 @@ parseBlock (Elem e) =
mapM parseGlossEntry (filterChildren (named "glossentry") e)
"glosslist" -> definitionList <$>
mapM parseGlossEntry (filterChildren (named "glossentry") e)
"chapter" -> sect 0
"chapter" -> modify (\st -> st{ dbBook = True}) >> sect 0
"appendix" -> sect 0
"preface" -> sect 0
"bridgehead" -> para . strong <$> getInlines e

View file

@ -85,6 +85,8 @@ tests pandocPath =
[ testGroup "writer" $ writerTests' "docbook4"
, test' "reader" ["-r", "docbook", "-w", "native", "-s"]
"docbook-reader.docbook" "docbook-reader.native"
, test' "reader" ["-r", "docbook", "-w", "native", "-s"]
"docbook-chapter.docbook" "docbook-chapter.native"
, test' "reader" ["-r", "docbook", "-w", "native", "-s"]
"docbook-xref.docbook" "docbook-xref.native"
]

View file

@ -0,0 +1,30 @@
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
<!-- This matches the example from
https://tdg.docbook.org/tdg/4.5/section.html, sans sectioninfo and subtitle, as
they're currently not supported -->
<chapter><title>Test Chapter</title>
<para>This chapter uses recursive sections.</para>
<section>
<title>Like a Sect1</title>
<para>This section is like a Sect1.</para>
<section><title>Like a Sect2</title>
<para>This section is like a Sect2.</para>
<section><title>Like a Sect3</title>
<para>This section is like a Sect3.</para>
<section><title>Like a Sect4</title>
<para>This section is like a Sect4.</para>
<section><title>Like a Sect5</title>
<para>This section is like a Sect5.</para>
<section><title>Would be like a Sect6</title>
<para>This section would be like a Sect6, if there was one.</para>
<section><title>Would be like a Sect7</title>
<para>This section would be like a Sect7, if there was one.</para>
</section>
</section>
</section>
</section>
</section>
</section>
</section>
</chapter>

View file

@ -0,0 +1,17 @@
Pandoc (Meta {unMeta = fromList []})
[Header 1 ("",[],[]) [Str "Test",Space,Str "Chapter"]
,Para [Str "This",Space,Str "chapter",Space,Str "uses",Space,Str "recursive",Space,Str "sections."]
,Header 2 ("",[],[]) [Str "Like",Space,Str "a",Space,Str "Sect1"]
,Para [Str "This",Space,Str "section",Space,Str "is",Space,Str "like",Space,Str "a",Space,Str "Sect1."]
,Header 3 ("",[],[]) [Str "Like",Space,Str "a",Space,Str "Sect2"]
,Para [Str "This",Space,Str "section",Space,Str "is",Space,Str "like",Space,Str "a",Space,Str "Sect2."]
,Header 4 ("",[],[]) [Str "Like",Space,Str "a",Space,Str "Sect3"]
,Para [Str "This",Space,Str "section",Space,Str "is",Space,Str "like",Space,Str "a",Space,Str "Sect3."]
,Header 5 ("",[],[]) [Str "Like",Space,Str "a",Space,Str "Sect4"]
,Para [Str "This",Space,Str "section",Space,Str "is",Space,Str "like",Space,Str "a",Space,Str "Sect4."]
,Header 6 ("",[],[]) [Str "Like",Space,Str "a",Space,Str "Sect5"]
,Para [Str "This",Space,Str "section",Space,Str "is",Space,Str "like",Space,Str "a",Space,Str "Sect5."]
,Header 7 ("",[],[]) [Str "Would",Space,Str "be",Space,Str "like",Space,Str "a",Space,Str "Sect6"]
,Para [Str "This",Space,Str "section",Space,Str "would",Space,Str "be",Space,Str "like",Space,Str "a",Space,Str "Sect6,",Space,Str "if",Space,Str "there",Space,Str "was",Space,Str "one."]
,Header 8 ("",[],[]) [Str "Would",Space,Str "be",Space,Str "like",Space,Str "a",Space,Str "Sect7"]
,Para [Str "This",Space,Str "section",Space,Str "would",Space,Str "be",Space,Str "like",Space,Str "a",Space,Str "Sect7,",Space,Str "if",Space,Str "there",Space,Str "was",Space,Str "one."]]