git.iko.soy/typeable/pandoc
on master help sign in
Added stateSubstitutions to ParserState, use for RST substitutions.
commit1be27ffb3a9ff471febca95361761c326f8e0537 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 27 Sep 2012, 15:20 (14 years ago) parent606b5e8
2 files changed+23−12
src/Text/Pandoc/Parsing.hs+5 src/Text/Pandoc/Readers/RST.hs+18−12
src/Text/Pandoc/Parsing.hs+5
@@ -71,6 +71,7 @@ module Text.Pandoc.Parsing ( (>>~),
7171 NoteTable,
7272 NoteTable',
7373 KeyTable,
74+ SubstTable,
7475 Key (..),
7576 toKey,
7677 smartPunctuation,
@@ -720,6 +721,7 @@ data ParserState = ParserState
720721 stateMaxNestingLevel :: Int, -- ^ Max # of nested Strong/Emph
721722 stateLastStrPos :: Maybe SourcePos, -- ^ Position after last str parsed
722723 stateKeys :: KeyTable, -- ^ List of reference keys (with fallbacks)
724+ stateSubstitutions :: SubstTable, -- ^ List of substitution references
723725 stateNotes :: NoteTable, -- ^ List of notes (raw bodies)
724726 stateNotes' :: NoteTable', -- ^ List of notes (parsed bodies)
725727 stateTitle :: [Inline], -- ^ Title of document
@@ -745,6 +747,7 @@ defaultParserState =
745747 stateMaxNestingLevel = 6,
746748 stateLastStrPos = Nothing,
747749 stateKeys = M.empty,
750+ stateSubstitutions = M.empty,
748751 stateNotes = [],
749752 stateNotes' = [],
750753 stateTitle = [],
@@ -795,6 +798,8 @@ toKey = Key . map toLower . unwords . words
795798
796799type KeyTable = M.Map Key Target
797800
801+type SubstTable = M.Map Key Inline
802+
798803-- | Fail unless we're in "smart typography" mode.
799804failUnlessSmart :: Parser [tok] ParserState ()
800805failUnlessSmart = getOption readerSmart >>= guard
src/Text/Pandoc/Readers/RST.hs+18−12
@@ -95,8 +95,8 @@ parseRST = do
9595 startPos <- getPosition
9696 -- go through once just to get list of reference keys and notes
9797 -- docMinusKeys is the raw document with blanks where the keys were...
98− docMinusKeys <- manyTill (referenceKey <|> noteBlock <|> lineClump) eof >>=
99− return . concat
98+ docMinusKeys <- manyTill (referenceKey <|> imageKey <|>
99+ noteBlock <|> lineClump) eof >>= return . concat
100100 setInput docMinusKeys
101101 setPosition startPos
102102 st' <- getState
@@ -652,7 +652,7 @@ referenceName = quotedReferenceName <|>
652652referenceKey :: Parser [Char] ParserState [Char]
653653referenceKey = do
654654 startPos <- getPosition
655− (key, target) <- choice [imageKey, anonymousKey, regularKey]
655+ (key, target) <- choice [anonymousKey, regularKey]
656656 st <- getState
657657 let oldkeys = stateKeys st
658658 updateState $ \s -> s { stateKeys = M.insert key target oldkeys }
@@ -670,14 +670,21 @@ targetURI = do
670670 blanklines
671671 return $ escapeURI $ removeLeadingTrailingSpace $ contents
672672
673−imageKey :: Parser [Char] ParserState (Key, Target)
673+imageKey :: Parser [Char] ParserState [Char]
674674imageKey = try $ do
675+ startPos <- getPosition
675676 string ".. |"
676− (_,ref) <- withRaw (manyTill inline (char '|'))
677+ (alt,ref) <- withRaw (manyTill inline (char '|'))
677678 skipSpaces
678679 string "image::"
679680 src <- targetURI
680− return (toKey $ init ref, (src, ""))
681+ let img = Image alt (src,"")
682+ let key = toKey $ init ref
683+ updateState $ \s -> s{ stateSubstitutions = M.insert key img $ stateSubstitutions s }
684+ optional blanklines
685+ endPos <- getPosition
686+ -- return enough blanks to replace key
687+ return $ replicate (sourceLine endPos - sourceLine startPos) '\n'
681688
682689anonymousKey :: Parser [Char] st (Key, Target)
683690anonymousKey = try $ do
@@ -965,13 +972,12 @@ autoLink = autoURI <|> autoEmail
965972image :: Parser [Char] ParserState Inline
966973image = try $ do
967974 char '|'
968− (alt,ref) <- withRaw (manyTill inline (char '|'))
975+ (_,ref) <- withRaw (manyTill inline (char '|'))
969976 state <- getState
970− let keyTable = stateKeys state
971− (src,tit) <- case M.lookup (toKey $ init ref) keyTable of
972− Nothing -> fail "no corresponding key"
973− Just target -> return target
974− return $ Image (normalizeSpaces alt) (src, tit)
977+ let substTable = stateSubstitutions state
978+ case M.lookup (toKey $ init ref) substTable of
979+ Nothing -> fail "no corresponding key"
980+ Just target -> return target
975981
976982note :: Parser [Char] ParserState Inline
977983note = try $ do