@@ -140,26 +140,24 @@ titleBlock = try $ do
140140parseMarkdown = do
141141 -- markdown allows raw HTML
142142 updateState (\state -> state { stateParseRaw = True })
143− (title, author, date) <- option ([],[],"") titleBlock
143+ startPos <- getPosition
144144 -- go through once just to get list of reference keys
145− refs <- manyTill (referenceKey <|> (lineClump >>= return . LineClump)) eof
146− let keys = map (\(KeyBlock label target) -> (label, target)) $
147− filter isKeyBlock refs
148− -- strip out keys
149− setInput $ concatMap (\(LineClump ln) -> ln) $ filter isLineClump refs
150− updateState (\state -> state { stateKeys = keys })
145+ -- docMinusKeys is the raw document with blanks where the keys were...
146+ docMinusKeys <- many (referenceKey <|> lineClump) >>= return . concat
147+ setInput docMinusKeys
148+ setPosition startPos
151149 st <- getState
150+ -- go through again for notes unless strict...
152151 if stateStrict st
153152 then return ()
154− else do -- go through for notes (which may contain refs - hence 2nd pass)
155− refs' <- manyTill (noteBlock <|>
156− (lineClump >>= return . LineClump)) eof
157− let notes = map (\(NoteBlock label blocks) -> (label, blocks)) $
158− filter isNoteBlock refs'
159− updateState (\state -> state { stateNotes = notes })
160− setInput $ concatMap (\(LineClump ln) -> ln) $
161− filter isLineClump refs'
162− -- go through again, with note blocks and keys stripped out
153+ else do docMinusNotes <- many (noteBlock <|> lineClump) >>= return . concat
154+ st <- getState
155+ let reversedNotes = stateNotes st
156+ updateState $ \st -> st { stateNotes = reverse reversedNotes }
157+ setInput docMinusNotes
158+ setPosition startPos
159+ -- now parse it for real...
160+ (title, author, date) <- option ([],[],"") titleBlock
163161 blocks <- parseBlocks
164162 return $ Pandoc (Meta title author date) $ filter (/= Null) blocks
165163
@@ -168,6 +166,7 @@ parseMarkdown = do
168166--
169167
170168referenceKey = try $ do
169+ startPos <- getPosition
171170 nonindentSpaces
172171 label <- reference
173172 char ':'
@@ -177,7 +176,13 @@ referenceKey = try $ do
177176 optional (char '>')
178177 tit <- option "" referenceTitle
179178 blanklines
180− return $ KeyBlock label (removeTrailingSpace src, tit)
179+ endPos <- getPosition
180+ let newkey = (label, (removeTrailingSpace src, tit))
181+ st <- getState
182+ let oldkeys = stateKeys st
183+ updateState $ \st -> st { stateKeys = newkey : oldkeys }
184+ -- return blanks so line count isn't affected
185+ return $ replicate (sourceLine endPos - sourceLine startPos) '\n'
181186
182187referenceTitle = try $ do
183188 skipSpaces
@@ -201,15 +206,22 @@ rawLine = do
201206rawLines = many1 rawLine >>= return . concat
202207
203208noteBlock = try $ do
209+ startPos <- getPosition
204210 ref <- noteMarker
205211 char ':'
206212 optional blankline
207213 optional indentSpaces
208214 raw <- sepBy rawLines (try (blankline >> indentSpaces))
209215 optional blanklines
216+ endPos <- getPosition
210217 -- parse the extracted text, which may contain various block elements:
211218 contents <- parseFromString parseBlocks $ (joinWithSep "\n" raw) ++ "\n\n"
212− return $ NoteBlock ref contents
219+ let newnote = (ref, contents)
220+ st <- getState
221+ let oldnotes = stateNotes st
222+ updateState $ \st -> st { stateNotes = newnote : oldnotes }
223+ -- return blanks so line count isn't affected
224+ return $ replicate (sourceLine endPos - sourceLine startPos) '\n'
213225
214226--
215227-- parsing blocks