@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings, TupleSections, ScopedTypeVariables #-}
1+{-# LANGUAGE OverloadedStrings, TupleSections, ScopedTypeVariables, MultiWayIf #-}
22{-
33Copyright (C) 2006-2015 John MacFarlane <jgm@berkeley.edu>
44
@@ -204,15 +204,10 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
204204 _ -> blocks
205205 else blocks
206206 body <- blockListToMarkdown opts blocks'
207− st <- get
208− notes' <- notesToMarkdown opts (reverse $ stNotes st)
209− st' <- get -- note that the notes may contain refs
210− refs' <- refsToMarkdown opts (reverse $ stRefs st')
207+ notesAndRefs' <- notesAndRefs opts
211208 let render' :: Doc -> String
212209 render' = render colwidth
213− let main = render' $ body <>
214− (if isEmpty notes' then empty else blankline <> notes') <>
215− (if isEmpty refs' then empty else blankline <> refs')
210+ let main = render' $ body <> notesAndRefs'
216211 let context = defField "toc" (render' toc)
217212 $ defField "body" main
218213 $ (if isNullMeta meta
@@ -337,6 +332,23 @@ beginsWithOrderedListMarker str =
337332 Left _ -> False
338333 Right _ -> True
339334
335+notesAndRefs :: WriterOptions -> MD Doc
336+notesAndRefs opts = do
337+ notes' <- reverse <$> gets stNotes >>= notesToMarkdown opts
338+ modify $ \s -> s { stNotes = [] }
339+ refs' <- reverse <$> gets stRefs >>= refsToMarkdown opts
340+ modify $ \s -> s { stRefs = [] }
341+
342+ let endSpacing =
343+ if | writerReferenceLocation opts == EndOfDocument -> empty
344+ | isEmpty notes' && isEmpty refs' -> empty
345+ | otherwise -> blankline
346+
347+ return $
348+ (if isEmpty notes' then empty else blankline <> notes') <>
349+ (if isEmpty refs' then empty else blankline <> refs') <>
350+ endSpacing
351+
340352-- | Convert Pandoc block element to markdown.
341353blockToMarkdown :: WriterOptions -- ^ Options
342354 -> Block -- ^ Block element
@@ -346,16 +358,7 @@ blockToMarkdown opts blk =
346358 do doc <- blockToMarkdown' opts blk
347359 blkLevel <- asks envBlockLevel
348360 if writerReferenceLocation opts == EndOfBlock && blkLevel == 1
349− then do st <- get
350− notes' <- notesToMarkdown opts (reverse $ stNotes st)
351− modify $ \s -> s { stNotes = [] }
352− st' <- get -- note that the notes may contain refs
353− refs' <- refsToMarkdown opts (reverse $ stRefs st')
354− modify $ \s -> s { stRefs = [] }
355− return $ doc <>
356− (if isEmpty notes' then empty else blankline <> notes') <>
357− (if isEmpty refs' then empty else blankline <> refs') <>
358− (if (isEmpty notes' && isEmpty refs') then empty else blankline)
361+ then notesAndRefs opts >>= (\d -> return $ doc <> d)
359362 else return doc
360363
361364blockToMarkdown' :: WriterOptions -- ^ Options
@@ -418,16 +421,7 @@ blockToMarkdown' opts (Header level attr inlines) = do
418421 -- put them here.
419422 blkLevel <- asks envBlockLevel
420423 refs <- if writerReferenceLocation opts == EndOfSection && blkLevel == 1
421− then do st <- get
422− notes' <- notesToMarkdown opts (reverse $ stNotes st)
423− modify $ \s -> s { stNotes = [] }
424− st' <- get -- note that the notes may contain refs
425− refs' <- refsToMarkdown opts (reverse $ stRefs st')
426− modify $ \s -> s { stRefs = [] }
427− return $
428− (if isEmpty notes' then empty else blankline <> notes') <>
429− (if isEmpty refs' then empty else blankline <> refs') <>
430− (if (isEmpty notes' && isEmpty refs') then empty else blankline)
424+ then notesAndRefs opts
431425 else return empty
432426
433427 plain <- asks envPlain