@@ -82,7 +82,7 @@ pandocToRST (Pandoc meta blocks) = do
82 (fmap (trimr . render colwidth) . inlineListToRST)
82 (fmap (trimr . render colwidth) . inlineListToRST)
83 $ deleteMeta "title" $ deleteMeta "subtitle" meta
83 $ deleteMeta "title" $ deleteMeta "subtitle" meta
84 let minLev = findMinHeadingLevel Nothing blocks
84 let minLev = findMinHeadingLevel Nothing blocks
85− body <- blockListToRST' True $ normalizeHeadings minLev blocks
85+ body <- blockListToRST' True $ normalizeHeadings minLev 0 blocks
86 notes <- liftM (reverse . stNotes) get >>= notesToRST
86 notes <- liftM (reverse . stNotes) get >>= notesToRST
87 -- note that the notes may contain refs, so we do them first
87 -- note that the notes may contain refs, so we do them first
88 refs <- liftM (reverse . stLinks) get >>= refsToRST
88 refs <- liftM (reverse . stLinks) get >>= refsToRST
@@ -102,12 +102,30 @@ pandocToRST (Pandoc meta blocks) = do
102 then return $ renderTemplate' (writerTemplate opts) context
102 then return $ renderTemplate' (writerTemplate opts) context
103 else return main
103 else return main
105+ -- Partial normalization:
106+ -- We use the underline scheme appropriate for the lowest
107+ -- header level found, and we don't normalize headers
108+ -- until we've hit the first header with minimum header
109+ -- level in the fragment.
110+ -- This is intended to help with the problems noted in
111+ -- #2079, while still being flexible enough for use
112+ -- with fragments that might not include the base
113+ -- header level, and might start halfway through a section
105− normalizeHeadings lev (Header l a i:bs) = Header lev a i:normalizeHeadings (lev+1) cont ++ normalizeHeadings lev bs'
115+ normalizeHeadings minLev lev (Header l a i:bs) =
116+ let lev' = if lev == 0 && l == minLev
119+ in if lev' == 0 -- we haven't hit minLev yet
120+ then Header l a i : normalizeHeadings minLev 0 cont ++
121+ normalizeHeadings minLev 0 bs'
122+ else Header lev' a i : normalizeHeadings minLev (lev' + 1) cont ++
123+ normalizeHeadings minLev lev' bs'
106 where (cont,bs') = break (headerLtEq l) bs
124 where (cont,bs') = break (headerLtEq l) bs
107 headerLtEq level (Header l' _ _) = l' <= level
125 headerLtEq level (Header l' _ _) = l' <= level
108 headerLtEq _ _ = False
126 headerLtEq _ _ = False
109− normalizeHeadings lev (b:bs) = b:normalizeHeadings lev bs
127+ normalizeHeadings minLev lev (b:bs) = b : normalizeHeadings minLev lev bs
110− normalizeHeadings _ [] = []
128+ normalizeHeadings _ _ [] = []
111 findMinHeadingLevel Nothing (Header l _a _i:bs) = findMinHeadingLevel (Just l) bs
129 findMinHeadingLevel Nothing (Header l _a _i:bs) = findMinHeadingLevel (Just l) bs
112 findMinHeadingLevel (Just ol) (Header l _a _i:bs) =
130 findMinHeadingLevel (Just ol) (Header l _a _i:bs) =
113 findMinHeadingLevel (Just $ if ol>l then l else ol) bs
131 findMinHeadingLevel (Just $ if ol>l then l else ol) bs