LaTeX reader: allow spaces btw \\begin or \\end and {.

This commit is contained in:
John MacFarlane 2011-01-06 09:34:06 -08:00
parent 81ea1a59b4
commit 303ce8a9e5
2 changed files with 28 additions and 11 deletions

View file

@ -86,14 +86,22 @@ command = do
begin :: [Char] -> GenParser Char st [Char] begin :: [Char] -> GenParser Char st [Char]
begin name = try $ do begin name = try $ do
string $ "\\begin{" ++ name ++ "}" string "\\begin"
spaces
char '{'
string name
char '}'
optional commandArgs optional commandArgs
spaces spaces
return name return name
end :: [Char] -> GenParser Char st [Char] end :: [Char] -> GenParser Char st [Char]
end name = try $ do end name = try $ do
string $ "\\end{" ++ name ++ "}" string "\\end"
spaces
char '{'
string name
char '}'
return name return name
-- | Returns a list of block elements containing the contents of an -- | Returns a list of block elements containing the contents of an
@ -103,7 +111,9 @@ environment name = try $ begin name >> spaces >> manyTill block (end name) >>~ s
anyEnvironment :: GenParser Char ParserState Block anyEnvironment :: GenParser Char ParserState Block
anyEnvironment = try $ do anyEnvironment = try $ do
string "\\begin{" string "\\begin"
spaces
char '{'
name <- many letter name <- many letter
star <- option "" (string "*") -- some environments have starred variants star <- option "" (string "*") -- some environments have starred variants
char '}' char '}'
@ -215,10 +225,10 @@ codeBlock = codeBlockWith "verbatim" <|> codeBlockWith "Verbatim" <|> codeBlockW
codeBlockWith :: String -> GenParser Char st Block codeBlockWith :: String -> GenParser Char st Block
codeBlockWith env = try $ do codeBlockWith env = try $ do
string ("\\begin{" ++ env ++ "}") -- don't use begin function because it string "\\begin"
-- gobbles whitespace spaces -- don't use begin function because it
optional blanklines -- we want to gobble blank lines, but not string $ "{" ++ env ++ "}" -- gobbles whitespace; we want to gobble
-- leading space optional blanklines -- blank lines, but not leading space
contents <- manyTill anyChar (try (string $ "\\end{" ++ env ++ "}")) contents <- manyTill anyChar (try (string $ "\\end{" ++ env ++ "}"))
spaces spaces
let classes = if env == "code" then ["haskell"] else [] let classes = if env == "code" then ["haskell"] else []
@ -262,7 +272,10 @@ listItem = try $ do
orderedList :: GenParser Char ParserState Block orderedList :: GenParser Char ParserState Block
orderedList = try $ do orderedList = try $ do
string "\\begin{enumerate}" string "\\begin"
spaces
string "{enumerate}"
spaces
(_, style, delim) <- option (1, DefaultStyle, DefaultDelim) $ (_, style, delim) <- option (1, DefaultStyle, DefaultDelim) $
try $ do failIfStrict try $ do failIfStrict
char '[' char '['
@ -383,7 +396,9 @@ rawLaTeXEnvironment = do
-- the whole literal environment as raw TeX. -- the whole literal environment as raw TeX.
rawLaTeXEnvironment' :: GenParser Char st String rawLaTeXEnvironment' :: GenParser Char st String
rawLaTeXEnvironment' = try $ do rawLaTeXEnvironment' = try $ do
string "\\begin{" string "\\begin"
spaces
char '{'
name <- many1 letter name <- many1 letter
star <- option "" (string "*") -- for starred variants star <- option "" (string "*") -- for starred variants
let name' = name ++ star let name' = name ++ star
@ -448,7 +463,9 @@ commandsToIgnore = ["special","pdfannot","pdfstringdef", "index","bibliography"]
skipChar :: GenParser Char ParserState Block skipChar :: GenParser Char ParserState Block
skipChar = do skipChar = do
satisfy (/='\\') <|> satisfy (/='\\') <|>
(notFollowedBy' (lookAhead $ string "\\begin{document}") >> anyChar) (notFollowedBy' (try $
string "\\begin" >> spaces >> string "{document}") >>
anyChar)
spaces spaces
return Null return Null

View file

@ -71,7 +71,7 @@ E-mail style:
This is a block quote. It is pretty short. This is a block quote. It is pretty short.
\end{quote} \end{quote}
\begin{quote} \begin {quote}
Code in a block quote: Code in a block quote:
\begin{verbatim} \begin{verbatim}