git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: improvements in raw LaTeX parsing.

+ "loose punctuation" (like {}) parsed as Space + Para elements must contain more than Str "" and Space elements + Added parser for "\ignore" command used in literate haskell. + Reworked unknownCommand and rawLaTeXInline: when not in "parse raw" mode, these parsers simply strip off the command part and allow the arguments to be parsed normally. So, for example, \blorg{\emph{hi}} will be parsed as Emph "hi" rather than Str "{\\emph{hi}}". git-svn-id: https://pandoc.googlecode.com/svn/trunk@1420 788f1e2b-df1e-0410-8736-df70ead52e1b

commitfc24c79db6ca54bfd5e2a99e12421d7749514faa authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSat 6 Sep 2008, 18:05 (18 years ago) parentee2dee2
1 file changed+41−20
Text/Pandoc/Readers/LaTeX.hs+41−20
Text/Pandoc/Readers/LaTeX.hs+41−20
@@ -159,6 +159,7 @@ block = choice [ hrule
159159 , specialEnvironment
160160 , itemBlock
161161 , unknownEnvironment
162+ , ignore
162163 , unknownCommand ] <?> "block"
163164
164165--
@@ -283,7 +284,12 @@ definitionList = try $ do
283284--
284285
285286para :: GenParser Char ParserState Block
286−para = many1 inline >>~ spaces >>= return . Para . normalizeSpaces
287+para = do
288+ res <- many1 inline
289+ spaces
290+ return $ if null (filter (`notElem` [Str "", Space]) res)
291+ then Null
292+ else Para $ normalizeSpaces res
287293
288294--
289295-- title authors date
@@ -331,7 +337,7 @@ itemBlock :: GenParser Char ParserState Block
331337itemBlock = try $ do
332338 ("item", _, args) <- command
333339 state <- getState
334− if (stateParserContext state == ListItemState)
340+ if stateParserContext state == ListItemState
335341 then fail "item should be handled by list block"
336342 else if null args
337343 then return Null
@@ -381,20 +387,33 @@ unknownEnvironment = try $ do
381387 else anyEnvironment -- otherwise just the contents
382388 return result
383389
390+-- \ignore{} is used conventionally in literate haskell for definitions
391+-- that are to be processed by the compiler but not printed.
392+ignore :: GenParser Char ParserState Block
393+ignore = try $ do
394+ ("ignore", _, _) <- command
395+ spaces
396+ return Null
397+
384398unknownCommand :: GenParser Char ParserState Block
385399unknownCommand = try $ do
386− notFollowedBy' $ choice $ map end ["itemize", "enumerate", "description",
400+ notFollowedBy' $ choice $ map end ["itemize", "enumerate", "description",
387401 "document"]
388− (name, star, args) <- command
389− spaces
390− let argStr = concat args
391402 state <- getState
392− if name == "item" && (stateParserContext state) == ListItemState
393− then fail "should not be parsed as raw"
394− else return ""
395403 if stateParseRaw state
396− then return $ Plain [TeX ("\\" ++ name ++ star ++ argStr)]
397− else return $ Plain [Str (joinWithSep " " args)]
404+ then do
405+ (name, star, args) <- command
406+ spaces
407+ if name == "item" && stateParserContext state == ListItemState
408+ then fail "should not be parsed as raw"
409+ else return ""
410+ return $ Plain [TeX ("\\" ++ name ++ star ++ concat args)]
411+ else do -- skip unknown command, leaving arguments to be parsed
412+ char '\\'
413+ letter
414+ many (letter <|> digit)
415+ spaces
416+ return Null
398417
399418-- latex comment
400419comment :: GenParser Char st Block
@@ -523,9 +542,9 @@ escapedChar = do
523542 result <- escaped (oneOf " $%&_#{}\n")
524543 return $ if result == Str "\n" then Str " " else result
525544
526−-- ignore standalone, nonescaped special characters
545+-- treat nonescaped special characters as spaces
527546unescapedChar :: GenParser Char st Inline
528−unescapedChar = oneOf "`$^&_#{}|<>" >> return (Str "")
547+unescapedChar = oneOf "`$^&_#{}|<>" >> return Space
529548
530549specialChar :: GenParser Char st Inline
531550specialChar = choice [ backslash, tilde, caret, bar, lt, gt, doubleQuote ]
@@ -727,12 +746,14 @@ footnote = try $ do
727746-- | Parse any LaTeX command and return it in a raw TeX inline element.
728747rawLaTeXInline :: GenParser Char ParserState Inline
729748rawLaTeXInline = try $ do
730− (name, star, args) <- command
749+ notFollowedBy' $ oneOfStrings ["\\begin", "\\end", "\\item", "\\ignore"]
731750 state <- getState
732− if ((name == "begin") || (name == "end") || (name == "item"))
733− then fail "not an inline command"
734− else string ""
735751 if stateParseRaw state
736− then return $ TeX ("\\" ++ name ++ star ++ concat args)
737− else return $ Str (joinWithSep " " args)
738−
752+ then do
753+ (name, star, args) <- command
754+ return $ TeX ("\\" ++ name ++ star ++ concat args)
755+ else do -- skip unknown command, leaving arguments to be parsed
756+ char '\\'
757+ letter
758+ many (letter <|> digit)
759+ return $ Str ""