git.iko.soy/typeable/pandoc
on master help sign in
RST writer: tweaks to header normalization.

These changes are intended to make the writer more useful to people who are processing small fragments, which may for example look like this: ### third level header from previous section ## second level header Previously such fragments got turned into two headers of the same level. The new algorithm avoids doing any normalization until we hit the minimal-level header in the fragment (here, the second level header). Closes #2394.

commit476b383c578699567ac4630391a15855521ab3d4 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 12 Oct 2015, 22:04 (11 years ago) parent0715c5c
1 file changed+22−4
src/Text/Pandoc/Writers/RST.hs+22−4
src/Text/Pandoc/Writers/RST.hs+22−4
@@ -82,7 +82,7 @@ pandocToRST (Pandoc meta blocks) = do
8282 (fmap (trimr . render colwidth) . inlineListToRST)
8383 $ deleteMeta "title" $ deleteMeta "subtitle" meta
8484 let minLev = findMinHeadingLevel Nothing blocks
85− body <- blockListToRST' True $ normalizeHeadings minLev blocks
85+ body <- blockListToRST' True $ normalizeHeadings minLev 0 blocks
8686 notes <- liftM (reverse . stNotes) get >>= notesToRST
8787 -- note that the notes may contain refs, so we do them first
8888 refs <- liftM (reverse . stLinks) get >>= refsToRST
@@ -102,12 +102,30 @@ pandocToRST (Pandoc meta blocks) = do
102102 then return $ renderTemplate' (writerTemplate opts) context
103103 else return main
104104 where
105− normalizeHeadings lev (Header l a i:bs) = Header lev a i:normalizeHeadings (lev+1) cont ++ normalizeHeadings lev bs'
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
114+ -- (see #2394).
115+ normalizeHeadings minLev lev (Header l a i:bs) =
116+ let lev' = if lev == 0 && l == minLev
117+ then minLev
118+ else lev
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'
106124 where (cont,bs') = break (headerLtEq l) bs
107125 headerLtEq level (Header l' _ _) = l' <= level
108126 headerLtEq _ _ = False
109− normalizeHeadings lev (b:bs) = b:normalizeHeadings lev bs
110− normalizeHeadings _ [] = []
127+ normalizeHeadings minLev lev (b:bs) = b : normalizeHeadings minLev lev bs
128+ normalizeHeadings _ _ [] = []
111129 findMinHeadingLevel Nothing (Header l _a _i:bs) = findMinHeadingLevel (Just l) bs
112130 findMinHeadingLevel (Just ol) (Header l _a _i:bs) =
113131 findMinHeadingLevel (Just $ if ol>l then l else ol) bs