Merge pull request #2405 from lierdakil/rest-normalize-amend

[RST Writer] Don't normalize heading levels below input minimum
This commit is contained in:
John MacFarlane 2015-09-19 23:32:23 -07:00
commit 54526665c3
2 changed files with 32 additions and 1 deletions

View file

@ -81,7 +81,8 @@ pandocToRST (Pandoc meta blocks) = do
(fmap (render colwidth) . blockListToRST) (fmap (render colwidth) . blockListToRST)
(fmap (trimr . render colwidth) . inlineListToRST) (fmap (trimr . render colwidth) . inlineListToRST)
$ deleteMeta "title" $ deleteMeta "subtitle" meta $ deleteMeta "title" $ deleteMeta "subtitle" meta
body <- blockListToRST' True $ normalizeHeadings 1 blocks let minLev = findMinHeadingLevel Nothing blocks
body <- blockListToRST' True $ normalizeHeadings minLev blocks
notes <- liftM (reverse . stNotes) get >>= notesToRST notes <- liftM (reverse . stNotes) get >>= notesToRST
-- note that the notes may contain refs, so we do them first -- note that the notes may contain refs, so we do them first
refs <- liftM (reverse . stLinks) get >>= refsToRST refs <- liftM (reverse . stLinks) get >>= refsToRST
@ -107,6 +108,11 @@ pandocToRST (Pandoc meta blocks) = do
headerLtEq _ _ = False headerLtEq _ _ = False
normalizeHeadings lev (b:bs) = b:normalizeHeadings lev bs normalizeHeadings lev (b:bs) = b:normalizeHeadings lev bs
normalizeHeadings _ [] = [] normalizeHeadings _ [] = []
findMinHeadingLevel Nothing (Header l _a _i:bs) = findMinHeadingLevel (Just l) bs
findMinHeadingLevel (Just ol) (Header l _a _i:bs) =
findMinHeadingLevel (Just $ if ol>l then l else ol) bs
findMinHeadingLevel l (_:bs) = findMinHeadingLevel l bs
findMinHeadingLevel l [] = fromMaybe 1 l
-- | Return RST representation of reference key table. -- | Return RST representation of reference key table.
refsToRST :: Refs -> State WriterState Doc refsToRST :: Refs -> State WriterState Doc

View file

@ -75,5 +75,30 @@ tests = [ testGroup "rubrics"
, "" , ""
, "Header 2" , "Header 2"
, "--------"] , "--------"]
, "minimal heading levels" =:
header 2 (text "Header 1") <>
header 3 (text "Header 2") <>
header 2 (text "Header 1") <>
header 4 (text "Header 2") <>
header 5 (text "Header 3") <>
header 3 (text "Header 2") =?>
unlines
[ "Header 1"
, "--------"
, ""
, "Header 2"
, "~~~~~~~~"
, ""
, "Header 1"
, "--------"
, ""
, "Header 2"
, "~~~~~~~~"
, ""
, "Header 3"
, "^^^^^^^^"
, ""
, "Header 2"
, "~~~~~~~~"]
] ]
] ]