aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/RST.hs
diff options
context:
space:
mode:
authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>2007-09-05 23:55:38 +0000
committerfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>2007-09-05 23:55:38 +0000
commit4b4060b8ef64da355070ee016235ad3f1d4b1f9f (patch)
treef16da87914ecc7e091bd03a652e48106900df5d3 /src/Text/Pandoc/Readers/RST.hs
parentd5e8ab71a49477db9e887499ed9513c7276ebbad (diff)
downloadpandoc-4b4060b8ef64da355070ee016235ad3f1d4b1f9f.tar.gz
Simplified parsing of reference keys and notes in markdown and RST
readers: + The Reference data structure from Text.Pandoc.Shared is no longer needed, since + referenceKey and noteBlock parses return strings (as many blank lines as are occuried by the key or note) and update state themselves. + getPosition and setPosition are now used to ensure that error messages will give the correct line number. + This yields cleaner (and slightly faster) code, with more accurate parsing error messages. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1012 788f1e2b-df1e-0410-8736-df70ead52e1b
Diffstat (limited to 'src/Text/Pandoc/Readers/RST.hs')
-rw-r--r--src/Text/Pandoc/Readers/RST.hs39
1 files changed, 24 insertions, 15 deletions
diff --git a/src/Text/Pandoc/Readers/RST.hs b/src/Text/Pandoc/Readers/RST.hs
index be55ae4c3..0103087a5 100644
--- a/src/Text/Pandoc/Readers/RST.hs
+++ b/src/Text/Pandoc/Readers/RST.hs
@@ -83,15 +83,17 @@ titleTransform ((Header 1 head1):rest) = -- title, no subtitle
titleTransform blocks = (blocks, [])
parseRST = do
- -- first pass: get keys
- refs <- manyTill (referenceKey <|> (lineClump >>= return . LineClump)) eof
- let keys = map (\(KeyBlock label target) -> (label, target)) $
- filter isKeyBlock refs
- -- second pass, with keys stripped out
- let rawlines = map (\(LineClump ln) -> ln) $ filter isLineClump refs
- setInput $ concat rawlines
- updateState (\state -> state { stateKeys = keys })
- blocks <- parseBlocks
+ startPos <- getPosition
+ -- go through once just to get list of reference keys
+ -- docMinusKeys is the raw document with blanks where the keys were...
+ docMinusKeys <- many (referenceKey <|> lineClump) >>= return . concat
+ setInput docMinusKeys
+ setPosition startPos
+ st <- getState
+ let reversedKeys = stateKeys st
+ updateState $ \st -> st { stateKeys = reverse reversedKeys }
+ -- now parse it for real...
+ blocks <- parseBlocks
let blocks' = filter (/= Null) blocks
state <- getState
let (blocks'', title) = if stateStandalone state
@@ -429,9 +431,16 @@ unknownDirective = try $ do
-- reference key
--
-referenceKey =
- choice [imageKey, anonymousKey, regularKeyQuoted, regularKey] >>~
+referenceKey = do
+ startPos <- getPosition
+ key <- choice [imageKey, anonymousKey, regularKeyQuoted, regularKey]
+ st <- getState
+ let oldkeys = stateKeys st
+ updateState $ \st -> st { stateKeys = key : oldkeys }
optional blanklines
+ endPos <- getPosition
+ -- return enough blanks to replace key
+ return $ replicate (sourceLine endPos - sourceLine startPos) '\n'
targetURI = do
skipSpaces
@@ -447,26 +456,26 @@ imageKey = try $ do
skipSpaces
string "image::"
src <- targetURI
- return $ KeyBlock (normalizeSpaces ref) (removeLeadingTrailingSpace src, "")
+ return (normalizeSpaces ref, (removeLeadingTrailingSpace src, ""))
anonymousKey = try $ do
oneOfStrings [".. __:", "__"]
src <- targetURI
state <- getState
- return $ KeyBlock [Str "_"] (removeLeadingTrailingSpace src, "")
+ return ([Str "_"], (removeLeadingTrailingSpace src, ""))
regularKeyQuoted = try $ do
string ".. _`"
ref <- manyTill inline (char '`')
char ':'
src <- targetURI
- return $ KeyBlock (normalizeSpaces ref) (removeLeadingTrailingSpace src, "")
+ return (normalizeSpaces ref, (removeLeadingTrailingSpace src, ""))
regularKey = try $ do
string ".. _"
ref <- manyTill inline (char ':')
src <- targetURI
- return $ KeyBlock (normalizeSpaces ref) (removeLeadingTrailingSpace src, "")
+ return (normalizeSpaces ref, (removeLeadingTrailingSpace src, ""))
--
-- inline