@@ -1,5 +1,5 @@
11module Text.Pandoc.Readers.DocBook ( readDocBook ) where
2−import Text.Pandoc.Parsing (ParserState(..), defaultParserState)
2+import Text.Pandoc.Parsing (ParserState(..))
33import Text.Pandoc.Definition
44import Text.Pandoc.Builder
55import Text.XML.Light
@@ -10,16 +10,13 @@ import Control.Applicative ((<$>))
1010
1111type DB = State DBState
1212
13−data DBState = DBState{ dbSectionLevel :: Int }
14− deriving (Read, Show)
15−
16−defaultDBState :: DBState
17−defaultDBState = DBState { dbSectionLevel = 0 }
13+data DBState = DBState{ dbSectionLevel :: Int
14+ } deriving Show
1815
1916readDocBook :: ParserState -> String -> Pandoc
2017readDocBook st inp = Pandoc (Meta [] [] []) $ toList blocks
2118 where blocks = mconcat $ evalState (mapM parseBlock $ parseXML inp)
22− defaultDBState
19+ DBState{ dbSectionLevel = 0 }
2320
2421parseBlock :: Content -> DB Blocks
2522parseBlock (Text (CData _ s _)) = if all isSpace s
@@ -34,6 +31,7 @@ parseBlock (Elem e) =
3431 "sect4" -> sect 4
3532 "sect5" -> sect 5
3633 "sect6" -> sect 6
34+ "section" -> gets dbSectionLevel >>= sect . (+1)
3735 "title" -> return $ mempty
3836 _ -> innerBlocks
3937 where innerBlocks = mconcat <$> (mapM parseBlock $ elContent e)
@@ -47,9 +45,16 @@ parseBlock (Elem e) =
4745 ((Elem t):body)
4846 | isTitle t -> do
4947 h <- header n <$> (getInlines t)
48+ modify $ \st -> st{ dbSectionLevel = n }
49+ b <- mconcat <$> (mapM parseBlock body)
50+ modify $ \st -> st{ dbSectionLevel = n - 1 }
51+ return $ h <> b
52+ body -> do
53+ let h = header n mempty
54+ modify $ \st -> st{ dbSectionLevel = n }
5055 b <- mconcat <$> (mapM parseBlock body)
56+ modify $ \st -> st{ dbSectionLevel = n - 1 }
5157 return $ h <> b
52− _ -> (header n mempty <>) <$> innerBlocks
5358parseBlock (CRef _) = return mempty
5459
5560parseInline :: Content -> DB Inlines