From d28dca57db5a72e2ebb17dccc6c279d962f6711f Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sat, 27 Oct 2018 23:34:09 +0300 Subject: Muse reader: forbid whitespace after opening and before closing markup elements See https://github.com/melmothx/text-amuse/issues/44 for discussion on these rules --- src/Text/Pandoc/Readers/Muse.hs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/Readers/Muse.hs b/src/Text/Pandoc/Readers/Muse.hs index 29a6882dd..d2f403f4b 100644 --- a/src/Text/Pandoc/Readers/Muse.hs +++ b/src/Text/Pandoc/Readers/Muse.hs @@ -78,6 +78,7 @@ data MuseState = MuseState { museMeta :: F Meta -- ^ Document metadata , museOptions :: ReaderOptions , museHeaders :: M.Map Inlines String -- ^ List of headers and ids (used for implicit ref links) , museIdentifierList :: Set.Set String + , museLastSpacePos :: Maybe SourcePos -- ^ Position after last space or newline parsed , museLastStrPos :: Maybe SourcePos -- ^ Position after last str parsed , museLogMessages :: [LogMessage] , museNotes :: M.Map String (SourcePos, F Blocks) @@ -89,6 +90,7 @@ instance Default MuseState where , museHeaders = M.empty , museIdentifierList = Set.empty , museLastStrPos = Nothing + , museLastSpacePos = Nothing , museLogMessages = [] , museNotes = M.empty } @@ -124,6 +126,10 @@ instance HasLogMessages MuseState where addLogMessage m s = s{ museLogMessages = m : museLogMessages s } getLogMessages = reverse . museLogMessages +updateLastSpacePos :: Monad m => MuseParser m () +updateLastSpacePos = getPosition >>= \pos -> + updateState $ \s -> s { museLastSpacePos = Just pos } + -- | Parse Muse document parseMuse :: PandocMonad m => MuseParser m Pandoc parseMuse = do @@ -159,6 +165,12 @@ atStart = do st <- getState guard $ museLastStrPos st /= Just pos +noSpaceBefore :: PandocMonad m => MuseParser m () +noSpaceBefore = do + pos <- getPosition + st <- getState + guard $ museLastSpacePos st /= Just pos + firstColumn :: PandocMonad m => MuseParser m () firstColumn = getPosition >>= \pos -> guard (sourceColumn pos == 1) @@ -738,7 +750,7 @@ inline = endline <|> inline' -- | Parse a soft break. endline :: PandocMonad m => MuseParser m (F Inlines) -endline = try $ pure B.softbreak <$ newline <* notFollowedBy blankline +endline = try $ pure B.softbreak <$ newline <* notFollowedBy blankline <* updateLastSpacePos parseAnchor :: PandocMonad m => MuseParser m String parseAnchor = try $ (:) @@ -769,7 +781,7 @@ footnote = try $ do return $ B.note contents' whitespace :: PandocMonad m => MuseParser m (F Inlines) -whitespace = try $ pure B.space <$ skipMany1 spaceChar +whitespace = try $ pure B.space <$ skipMany1 spaceChar <* updateLastSpacePos -- | Parse @\
@ tag. br :: PandocMonad m => MuseParser m (F Inlines) @@ -781,8 +793,8 @@ emphasisBetween :: (PandocMonad m, Show a) emphasisBetween p = try $ trimInlinesF . mconcat <$ atStart <* p - <* notFollowedBy spaceChar - <*> many1Till inline p + <* notFollowedBy space + <*> many1Till inline (try $ noSpaceBefore *> p) <* notFollowedBy alphaNum -- | Parse an inline tag, such as @\@ and @\@. -- cgit v1.2.3