@@ -15,6 +15,7 @@ Conversion of 'Pandoc' documents to Docbook XML.
1515module Text.Pandoc.Writers.Docbook ( writeDocbook4, writeDocbook5 ) where
1616import Control.Monad.Reader
1717import Data.Generics (everywhere, mkT)
18+import Data.Maybe (isNothing)
1819import Data.Monoid (Any (..))
1920import Data.Text (Text)
2021import qualified Data.Text as T
@@ -40,6 +41,18 @@ data DocBookVersion = DocBook4 | DocBook5
4041
4142type DB = ReaderT DocBookVersion
4243
44+-- | Get level of the top-level headers based on the configured top-level division.
45+-- The header level can then be used to determine appropriate DocBook element
46+-- for each subdivision associated with a header.
47+-- The numbering here follows LaTeX's internal numbering
48+getStartLvl :: WriterOptions -> Int
49+getStartLvl opts =
50+ case writerTopLevelDivision opts of
51+ TopLevelPart -> -1
52+ TopLevelChapter -> 0
53+ TopLevelSection -> 1
54+ TopLevelDefault -> 1
55+
4356-- | Convert list of authors to a docbook <author> section
4457authorToDocbook :: PandocMonad m => WriterOptions -> [Inline] -> DB m B.Inlines
4558authorToDocbook opts name' = do
@@ -79,12 +92,7 @@ writeDocbook opts (Pandoc meta blocks) = do
7992 let colwidth = if writerWrapText opts == WrapAuto
8093 then Just $ writerColumns opts
8194 else Nothing
82− -- The numbering here follows LaTeX's internal numbering
83− let startLvl = case writerTopLevelDivision opts of
84− TopLevelPart -> -1
85− TopLevelChapter -> 0
86− TopLevelSection -> 1
87− TopLevelDefault -> 1
95+ let startLvl = getStartLvl opts
8896 let fromBlocks = blocksToDocbook opts .
8997 makeSections False (Just startLvl)
9098 auths' <- mapM (authorToDocbook opts) $ docAuthors meta
@@ -170,8 +178,12 @@ blockToDocbook opts (Div (id',"section":_,_) (Header lvl _ ils : xs)) = do
170178 then "xml:id"
171179 else "id"
172180 idAttr = [(idName, writerIdentifierPrefix opts <> id') | not (T.null id')]
173− nsAttr = if version == DocBook5 && lvl == 0 then [("xmlns", "http://docbook.org/ns/docbook"),("xmlns:xlink", "http://www.w3.org/1999/xlink")]
174− else []
181+ -- We want to add namespaces to the root (top-level) element.
182+ nsAttr = if version == DocBook5 && lvl == getStartLvl opts && isNothing (writerTemplate opts)
183+ -- Though, DocBook 4 does not support namespaces and
184+ -- standalone documents will include them in the template.
185+ then [("xmlns", "http://docbook.org/ns/docbook"),("xmlns:xlink", "http://www.w3.org/1999/xlink")]
186+ else []
175187 attribs = nsAttr <> idAttr
176188 title' <- inlinesToDocbook opts ils
177189 contents <- blocksToDocbook opts bs