@@ -80,12 +80,14 @@ data WriterEnv =
8080
8181data WriterState =
8282 WriterState { stNotes :: Notes
83+ , stNoteNum :: Int
8384 , stIds :: Set.Set String
8485 , stUseTags :: Bool -- ^ Use tags for emphasis, for example because previous character is a letter
8586 }
8687
8788instance Default WriterState
8889 where def = WriterState { stNotes = []
90+ , stNoteNum = 1
8991 , stIds = Set.empty
9092 , stUseTags = False
9193 }
@@ -127,7 +129,7 @@ pandocToMuse (Pandoc meta blocks) = do
127129 (fmap render' . inlineListToMuse)
128130 meta
129131 body <- blockListToMuse blocks
130− notes <- fmap (reverse . stNotes) get >>= notesToMuse
132+ notes <- currentNotesToMuse
131133 let main = render colwidth $ body $+$ notes
132134 let context = defField "body" main metadata
133135 case writerTemplate opts of
@@ -141,7 +143,7 @@ catWithBlankLines :: PandocMonad m
141143 -> Int -- ^ Number of blank lines
142144 -> Muse m Doc
143145catWithBlankLines (b : bs) n = do
144− b' <- blockToMuse b
146+ b' <- blockToMuseWithNotes b
145147 bs' <- flatBlockListToMuse bs
146148 return $ b' <> blanklines n <> bs'
147149catWithBlankLines _ _ = error "Expected at least one block"
@@ -290,26 +292,61 @@ blockToMuse (Table caption aligns widths headers rows) =
290292blockToMuse (Div _ bs) = flatBlockListToMuse bs
291293blockToMuse Null = return empty
292294
295+-- | Return Muse representation of notes collected so far.
296+currentNotesToMuse :: PandocMonad m
297+ => Muse m Doc
298+currentNotesToMuse = do
299+ notes <- reverse <$> gets stNotes
300+ modify $ \st -> st { stNotes = mempty }
301+ notesToMuse notes
302+
293303-- | Return Muse representation of notes.
294304notesToMuse :: PandocMonad m
295305 => Notes
296306 -> Muse m Doc
297−notesToMuse notes = vsep <$> zipWithM noteToMuse [1 ..] notes
307+notesToMuse notes = do
308+ n <- gets stNoteNum
309+ modify $ \st -> st { stNoteNum = stNoteNum st + length notes }
310+ vsep <$> zipWithM noteToMuse [n ..] notes
298311
299312-- | Return Muse representation of a note.
300313noteToMuse :: PandocMonad m
301314 => Int
302315 -> [Block]
303316 -> Muse m Doc
304−noteToMuse num note =
305− hang (length marker) (text marker) <$>
317+noteToMuse num note = do
318+ res <- hang (length marker) (text marker) <$>
306319 local (\env -> env { envInsideBlock = True
307320 , envInlineStart = True
308321 , envAfterSpace = True
309322 }) (blockListToMuse note)
323+ return $ res <> blankline
310324 where
311325 marker = "[" ++ show num ++ "] "
312326
327+-- | Return Muse representation of block and accumulated notes.
328+blockToMuseWithNotes :: PandocMonad m
329+ => Block
330+ -> Muse m Doc
331+blockToMuseWithNotes blk = do
332+ topLevel <- asks envTopLevel
333+ opts <- asks envOptions
334+ let hdrToMuse hdr@(Header{}) = do
335+ b <- blockToMuse hdr
336+ if topLevel && writerReferenceLocation opts == EndOfSection
337+ then do
338+ notes <- currentNotesToMuse
339+ return $ notes $+$ b
340+ else
341+ return b
342+ hdrToMuse b = blockToMuse b
343+ b <- hdrToMuse blk
344+ if topLevel && writerReferenceLocation opts == EndOfBlock
345+ then do
346+ notes <- currentNotesToMuse
347+ return $ b $+$ notes <> blankline
348+ else return b
349+
313350-- | Escape special characters for Muse.
314351escapeString :: String -> String
315352escapeString s =
@@ -659,7 +696,8 @@ inlineToMuse (Note contents) = do
659696 modify $ \st -> st { stNotes = contents:notes
660697 , stUseTags = False
661698 }
662− let ref = show $ length notes + 1
699+ n <- gets stNoteNum
700+ let ref = show $ n + length notes
663701 return $ "[" <> text ref <> "]"
664702inlineToMuse (Span (anchor,names,_) inlines) = do
665703 contents <- inlineListToMuse inlines