@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings, TupleSections, ScopedTypeVariables #-}
1+{-# LANGUAGE OverloadedStrings, TupleSections, ScopedTypeVariables, MultiWayIf #-}
3Copyright (C) 2006-2015 John MacFarlane <jgm@berkeley.edu>
3Copyright (C) 2006-2015 John MacFarlane <jgm@berkeley.edu>
@@ -204,15 +204,10 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
204 _ -> blocks
204 _ -> blocks
205 else blocks
205 else blocks
206 body <- blockListToMarkdown opts blocks'
206 body <- blockListToMarkdown opts blocks'
207− st <- get
207+ notesAndRefs' <- notesAndRefs opts
208− notes' <- notesToMarkdown opts (reverse $ stNotes st)
209− st' <- get -- note that the notes may contain refs
210− refs' <- refsToMarkdown opts (reverse $ stRefs st')
211 let render' :: Doc -> String
208 let render' :: Doc -> String
212 render' = render colwidth
209 render' = render colwidth
213− let main = render' $ body <>
210+ let main = render' $ body <> notesAndRefs'
214− (if isEmpty notes' then empty else blankline <> notes') <>
215− (if isEmpty refs' then empty else blankline <> refs')
216 let context = defField "toc" (render' toc)
211 let context = defField "toc" (render' toc)
217 $ defField "body" main
212 $ defField "body" main
218 $ (if isNullMeta meta
213 $ (if isNullMeta meta
@@ -337,6 +332,23 @@ beginsWithOrderedListMarker str =
337 Left _ -> False
332 Left _ -> False
338 Right _ -> True
333 Right _ -> True
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 = [] }
343+ if | writerReferenceLocation opts == EndOfDocument -> empty
344+ | isEmpty notes' && isEmpty refs' -> empty
345+ | otherwise -> blankline
348+ (if isEmpty notes' then empty else blankline <> notes') <>
349+ (if isEmpty refs' then empty else blankline <> refs') <>
340-- | Convert Pandoc block element to markdown.
352-- | Convert Pandoc block element to markdown.
341blockToMarkdown :: WriterOptions -- ^ Options
353blockToMarkdown :: WriterOptions -- ^ Options
342 -> Block -- ^ Block element
354 -> Block -- ^ Block element
@@ -346,16 +358,7 @@ blockToMarkdown opts blk =
346 do doc <- blockToMarkdown' opts blk
358 do doc <- blockToMarkdown' opts blk
347 blkLevel <- asks envBlockLevel
359 blkLevel <- asks envBlockLevel
348 if writerReferenceLocation opts == EndOfBlock && blkLevel == 1
360 if writerReferenceLocation opts == EndOfBlock && blkLevel == 1
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 <>
361+ then notesAndRefs opts >>= (\d -> return $ doc <> d)
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)
359 else return doc
362 else return doc
361blockToMarkdown' :: WriterOptions -- ^ Options
364blockToMarkdown' :: WriterOptions -- ^ Options
@@ -418,16 +421,7 @@ blockToMarkdown' opts (Header level attr inlines) = do
418 -- put them here.
421 -- put them here.
419 blkLevel <- asks envBlockLevel
422 blkLevel <- asks envBlockLevel
420 refs <- if writerReferenceLocation opts == EndOfSection && blkLevel == 1
423 refs <- if writerReferenceLocation opts == EndOfSection && blkLevel == 1
421− then do st <- get
424+ then notesAndRefs opts
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 = [] }
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)
431 else return empty
425 else return empty
433 plain <- asks envPlain
427 plain <- asks envPlain