@@ -140,26 +140,24 @@ titleBlock = try $ do
140parseMarkdown = do
140parseMarkdown = do
141 -- markdown allows raw HTML
141 -- markdown allows raw HTML
142 updateState (\state -> state { stateParseRaw = True })
142 updateState (\state -> state { stateParseRaw = True })
143− (title, author, date) <- option ([],[],"") titleBlock
143+ startPos <- getPosition
144 -- go through once just to get list of reference keys
144 -- go through once just to get list of reference keys
145+ -- docMinusKeys is the raw document with blanks where the keys were...
145− refs <- manyTill (referenceKey <|> (lineClump >>= return . LineClump)) eof
146+ docMinusKeys <- many (referenceKey <|> lineClump) >>= return . concat
146− let keys = map (\(KeyBlock label target) -> (label, target)) $
147+ setInput docMinusKeys
147− filter isKeyBlock refs
148+ setPosition startPos
149− setInput $ concatMap (\(LineClump ln) -> ln) $ filter isLineClump refs
150− updateState (\state -> state { stateKeys = keys })
151 st <- getState
149 st <- getState
150+ -- go through again for notes unless strict...
152 if stateStrict st
151 if stateStrict st
153 then return ()
152 then return ()
154− else do -- go through for notes (which may contain refs - hence 2nd pass)
153+ else do docMinusNotes <- many (noteBlock <|> lineClump) >>= return . concat
155− refs' <- manyTill (noteBlock <|>
154+ st <- getState
156− (lineClump >>= return . LineClump)) eof
155+ let reversedNotes = stateNotes st
157− let notes = map (\(NoteBlock label blocks) -> (label, blocks)) $
158− filter isNoteBlock refs'
159− updateState (\state -> state { stateNotes = notes })
156+ updateState $ \st -> st { stateNotes = reverse reversedNotes }
160− setInput $ concatMap (\(LineClump ln) -> ln) $
157+ setInput docMinusNotes
161− filter isLineClump refs'
158+ setPosition startPos
162− -- go through again, with note blocks and keys stripped out
159+ -- now parse it for real...
160+ (title, author, date) <- option ([],[],"") titleBlock
163 blocks <- parseBlocks
161 blocks <- parseBlocks
164 return $ Pandoc (Meta title author date) $ filter (/= Null) blocks
162 return $ Pandoc (Meta title author date) $ filter (/= Null) blocks
@@ -168,6 +166,7 @@ parseMarkdown = do
170referenceKey = try $ do
168referenceKey = try $ do
169+ startPos <- getPosition
171 nonindentSpaces
170 nonindentSpaces
172 label <- reference
171 label <- reference
@@ -177,7 +176,13 @@ referenceKey = try $ do
177 optional (char '>')
176 optional (char '>')
178 tit <- option "" referenceTitle
177 tit <- option "" referenceTitle
179 blanklines
178 blanklines
179+ endPos <- getPosition
180− return $ KeyBlock label (removeTrailingSpace src, tit)
180+ let newkey = (label, (removeTrailingSpace src, tit))
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'
182referenceTitle = try $ do
187referenceTitle = try $ do
183 skipSpaces
188 skipSpaces
@@ -201,15 +206,22 @@ rawLine = do
201rawLines = many1 rawLine >>= return . concat
206rawLines = many1 rawLine >>= return . concat
203noteBlock = try $ do
208noteBlock = try $ do
209+ startPos <- getPosition
204 ref <- noteMarker
210 ref <- noteMarker
206 optional blankline
212 optional blankline
207 optional indentSpaces
213 optional indentSpaces
208 raw <- sepBy rawLines (try (blankline >> indentSpaces))
214 raw <- sepBy rawLines (try (blankline >> indentSpaces))
209 optional blanklines
215 optional blanklines
216+ endPos <- getPosition
210 -- parse the extracted text, which may contain various block elements:
217 -- parse the extracted text, which may contain various block elements:
211 contents <- parseFromString parseBlocks $ (joinWithSep "\n" raw) ++ "\n\n"
218 contents <- parseFromString parseBlocks $ (joinWithSep "\n" raw) ++ "\n\n"
212− return $ NoteBlock ref contents
219+ let newnote = (ref, contents)
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'
215-- parsing blocks
227-- parsing blocks