git.iko.soy/typeable/pandoc
on master help sign in
Introduce class HasLastStrPosition, generalize functions
9df589bAlbert Krewinkel
src/Text/Pandoc/Parsing.hs+23−9
@@ -54,7 +54,6 @@ module Text.Pandoc.Parsing ( (>>~),
54 withRaw,
54 withRaw,
55 escaped,
55 escaped,
56 characterReference,
56 characterReference,
57− updateLastStrPos,
58 anyOrderedListMarker,
57 anyOrderedListMarker,
59 orderedListMarker,
58 orderedListMarker,
60 charRef,
59 charRef,
@@ -66,11 +65,14 @@ module Text.Pandoc.Parsing ( (>>~),
66 testStringWith,
65 testStringWith,
67 guardEnabled,
66 guardEnabled,
68 guardDisabled,
67 guardDisabled,
68+ updateLastStrPos,
69+ notAfterString,
69 ParserState (..),
70 ParserState (..),
70 HasReaderOptions (..),
71 HasReaderOptions (..),
71 HasHeaderMap (..),
72 HasHeaderMap (..),
72 HasIdentifierList (..),
73 HasIdentifierList (..),
73 HasMacros (..),
74 HasMacros (..),
75+ HasLastStrPosition (..),
74 defaultParserState,
76 defaultParserState,
75 HeaderType (..),
77 HeaderType (..),
76 ParserContext (..),
78 ParserContext (..),
@@ -904,6 +906,14 @@ instance HasMacros ParserState where
904 extractMacros = stateMacros
906 extractMacros = stateMacros
905 updateMacros f st = st{ stateMacros = f $ stateMacros st }
907 updateMacros f st = st{ stateMacros = f $ stateMacros st }
906
908
909+class HasLastStrPosition st where
910+ setLastStrPos :: SourcePos -> st -> st
911+ getLastStrPos :: st -> Maybe SourcePos
912+
913+instance HasLastStrPosition ParserState where
914+ setLastStrPos pos st = st{ stateLastStrPos = Just pos }
915+ getLastStrPos st = stateLastStrPos st
916+
907defaultParserState :: ParserState
917defaultParserState :: ParserState
908defaultParserState =
918defaultParserState =
909 ParserState { stateOptions = def,
919 ParserState { stateOptions = def,
@@ -938,6 +948,17 @@ guardEnabled ext = getOption readerExtensions >>= guard . Set.member ext
938guardDisabled :: HasReaderOptions st => Extension -> Parser s st ()
948guardDisabled :: HasReaderOptions st => Extension -> Parser s st ()
939guardDisabled ext = getOption readerExtensions >>= guard . not . Set.member ext
949guardDisabled ext = getOption readerExtensions >>= guard . not . Set.member ext
940
950
951+-- | Update the position on which the last string ended.
952+updateLastStrPos :: HasLastStrPosition st => Parser s st ()
953+updateLastStrPos = getPosition >>= updateState . setLastStrPos
954+
955+-- | Whether we are right after the end of a string.
956+notAfterString :: HasLastStrPosition st => Parser s st Bool
957+notAfterString = do
958+ pos <- getPosition
959+ st <- getState
960+ return $ getLastStrPos st /= Just pos
961+
941data HeaderType
962data HeaderType
942 = SingleHeader Char -- ^ Single line of characters underneath
963 = SingleHeader Char -- ^ Single line of characters underneath
943 | DoubleHeader Char -- ^ Lines of characters above and below
964 | DoubleHeader Char -- ^ Lines of characters above and below
@@ -1049,17 +1070,11 @@ charOrRef cs =
1049 guard (c `elem` cs)
1070 guard (c `elem` cs)
1050 return c)
1071 return c)
1051
1072
1052−updateLastStrPos :: Parser [Char] ParserState ()
1053−updateLastStrPos = getPosition >>= \p ->
1054− updateState $ \s -> s{ stateLastStrPos = Just p }
1055−
1056singleQuoteStart :: Parser [Char] ParserState ()
1073singleQuoteStart :: Parser [Char] ParserState ()
1057singleQuoteStart = do
1074singleQuoteStart = do
1058 failIfInQuoteContext InSingleQuote
1075 failIfInQuoteContext InSingleQuote
1059− pos <- getPosition
1060− st <- getState
1061 -- single quote start can't be right after str
1076 -- single quote start can't be right after str
1062− guard $ stateLastStrPos st /= Just pos
1077+ guard =<< notAfterString
1063 () <$ charOrRef "'\8216\145"
1078 () <$ charOrRef "'\8216\145"
1064
1079
1065singleQuoteEnd :: Parser [Char] st ()
1080singleQuoteEnd :: Parser [Char] st ()
@@ -1156,4 +1171,3 @@ applyMacros' target = do
1156 then do macros <- extractMacros `fmap` getState
1171 then do macros <- extractMacros `fmap` getState
1157 return $ applyMacros macros target
1172 return $ applyMacros macros target
1158 else return target
1173 else return target
1159−
src/Text/Pandoc/Readers/Markdown.hs+3−8
@@ -1474,9 +1474,7 @@ strongOrEmph = enclosure '*' <|> (checkIntraword >> enclosure '_')
1474 where checkIntraword = do
1474 where checkIntraword = do
1475 exts <- getOption readerExtensions
1475 exts <- getOption readerExtensions
1476 when (Ext_intraword_underscores `Set.member` exts) $ do
1476 when (Ext_intraword_underscores `Set.member` exts) $ do
1477− pos <- getPosition
1477+ guard =<< notAfterString
1478− lastStrPos <- stateLastStrPos <$> getState
1479− guard $ lastStrPos /= Just pos
1480
1478
1481-- | Parses a list of inlines between start and end delimiters.
1479-- | Parses a list of inlines between start and end delimiters.
1482inlinesBetween :: (Show b)
1480inlinesBetween :: (Show b)
@@ -1518,8 +1516,7 @@ nonEndline = satisfy (/='\n')
1518str :: MarkdownParser (F Inlines)
1516str :: MarkdownParser (F Inlines)
1519str = do
1517str = do
1520 result <- many1 alphaNum
1518 result <- many1 alphaNum
1521− pos <- getPosition
1519+ updateLastStrPos
1522− updateState $ \s -> s{ stateLastStrPos = Just pos }
1523 let spacesToNbr = map (\c -> if c == ' ' then '\160' else c)
1520 let spacesToNbr = map (\c -> if c == ' ' then '\160' else c)
1524 isSmart <- getOption readerSmart
1521 isSmart <- getOption readerSmart
1525 if isSmart
1522 if isSmart
@@ -1821,9 +1818,7 @@ citeKey :: MarkdownParser (Bool, String)
1821citeKey = try $ do
1818citeKey = try $ do
1822 -- make sure we're not right after an alphanumeric,
1819 -- make sure we're not right after an alphanumeric,
1823 -- since foo@bar.baz is probably an email address
1820 -- since foo@bar.baz is probably an email address
1824− lastStrPos <- stateLastStrPos <$> getState
1821+ guard =<< notAfterString
1825− pos <- getPosition
1826− guard $ lastStrPos /= Just pos
1827 suppress_author <- option False (char '-' >> return True)
1822 suppress_author <- option False (char '-' >> return True)
1828 char '@'
1823 char '@'
1829 first <- letter <|> char '_'
1824 first <- letter <|> char '_'
src/Text/Pandoc/Readers/Org.hs+4−7
@@ -105,6 +105,10 @@ instance HasMeta OrgParserState where
105 deleteMeta field st =
105 deleteMeta field st =
106 st{ orgStateMeta = deleteMeta field $ orgStateMeta st }
106 st{ orgStateMeta = deleteMeta field $ orgStateMeta st }
107
107
108+instance HasLastStrPosition OrgParserState where
109+ getLastStrPos = orgStateLastStrPos
110+ setLastStrPos pos st = st{ orgStateLastStrPos = Just pos }
111+
108instance Default OrgParserState where
112instance Default OrgParserState where
109 def = defaultOrgParserState
113 def = defaultOrgParserState
110
114
@@ -1274,13 +1278,6 @@ afterEmphasisPreChar = do
1274 lastPrePos <- orgStateLastPreCharPos <$> getState
1278 lastPrePos <- orgStateLastPreCharPos <$> getState
1275 return . fromMaybe True $ (== pos) <$> lastPrePos
1279 return . fromMaybe True $ (== pos) <$> lastPrePos
1276
1280
1277−-- | Whether we are right after the end of a string
1278−notAfterString :: OrgParser Bool
1279−notAfterString = do
1280− pos <- getPosition
1281− lastStrPos <- orgStateLastStrPos <$> getState
1282− return $ lastStrPos /= Just pos
1283−
1284-- | Whether the parser is right after a forbidden border char
1281-- | Whether the parser is right after a forbidden border char
1285notAfterForbiddenBorderChar :: OrgParser Bool
1282notAfterForbiddenBorderChar :: OrgParser Bool
1286notAfterForbiddenBorderChar = do
1283notAfterForbiddenBorderChar = do