diff options
author | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2008-08-11 07:04:36 +0000 |
---|---|---|
committer | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2008-08-11 07:04:36 +0000 |
commit | c8a56a2864191831e234224eb5638ee53672b314 (patch) | |
tree | bcaa974794c9e815e92bd3d91450cfa05977997c | |
parent | b7a9d3206ee8c18228f5b41963d1454e932edbf5 (diff) | |
download | pandoc-c8a56a2864191831e234224eb5638ee53672b314.tar.gz |
Parse raw ConTeXt environments as TeX in markdown reader.
Resolves Issue #73.
Also made some structural changes to parsing of raw LaTeX environments.
Previously there was a special block parser for LaTeX environments.
It returned a Para element containing the raw TeX inline. This has
been removed, and the raw LaTeX environment parser is now used in the
rawLaTeXInline parser. The effect is exactly the same, except that we
can now handle consecutive LaTeX and ConTeXt environments not separated
by spaces. This new flexibility is required by the example in
Issue #73:
\placeformula \startformula
L_{1} = L_{2}
\stopformula
API change: The LaTeX reader now exports rawLaTeXEnvironment' (which
returns a string) rather than rawLaTeXEnvironment (which returns a block
element). This is more likely to be useful in other applications.
Added test cases for raw ConTeXt environments to markdown-reader-more.txt.
git-svn-id: https://pandoc.googlecode.com/svn/trunk@1405 788f1e2b-df1e-0410-8736-df70ead52e1b
-rw-r--r-- | Text/Pandoc/Readers/LaTeX.hs | 25 | ||||
-rw-r--r-- | Text/Pandoc/Readers/Markdown.hs | 25 | ||||
-rw-r--r-- | tests/markdown-reader-more.native | 5 | ||||
-rw-r--r-- | tests/markdown-reader-more.txt | 11 |
4 files changed, 52 insertions, 14 deletions
diff --git a/Text/Pandoc/Readers/LaTeX.hs b/Text/Pandoc/Readers/LaTeX.hs index c45b18d5b..2ce0204ee 100644 --- a/Text/Pandoc/Readers/LaTeX.hs +++ b/Text/Pandoc/Readers/LaTeX.hs @@ -30,7 +30,7 @@ Conversion of LaTeX to 'Pandoc' document. module Text.Pandoc.Readers.LaTeX ( readLaTeX, rawLaTeXInline, - rawLaTeXEnvironment + rawLaTeXEnvironment' ) where import Text.ParserCombinators.Parsec @@ -92,13 +92,12 @@ begin name = try $ do end :: [Char] -> GenParser Char st [Char] end name = try $ do string $ "\\end{" ++ name ++ "}" - spaces return name -- | Returns a list of block elements containing the contents of an -- environment. environment :: [Char] -> GenParser Char ParserState [Block] -environment name = try $ begin name >> spaces >> manyTill block (end name) +environment name = try $ begin name >> spaces >> manyTill block (end name) >>~ spaces anyEnvironment :: GenParser Char ParserState Block anyEnvironment = try $ do @@ -109,6 +108,7 @@ anyEnvironment = try $ do optional commandArgs spaces contents <- manyTill block (end (name ++ star)) + spaces return $ BlockQuote contents -- @@ -377,7 +377,15 @@ specialEnvironment = do -- these are always parsed as raw -- | Parse any LaTeX environment and return a Para block containing -- the whole literal environment as raw TeX. rawLaTeXEnvironment :: GenParser Char st Block -rawLaTeXEnvironment = try $ do +rawLaTeXEnvironment = do + contents <- rawLaTeXEnvironment' + spaces + return $ Para [TeX contents] + +-- | Parse any LaTeX environment and return a string containing +-- the whole literal environment as raw TeX. +rawLaTeXEnvironment' :: GenParser Char st String +rawLaTeXEnvironment' = try $ do string "\\begin{" name <- many1 letter star <- option "" (string "*") -- for starred variants @@ -386,14 +394,11 @@ rawLaTeXEnvironment = try $ do args <- option [] commandArgs let argStr = concat args contents <- manyTill (choice [ (many1 (noneOf "\\")), - (do - (Para [TeX s]) <- rawLaTeXEnvironment - return s), + rawLaTeXEnvironment', string "\\" ]) (end name') - spaces - return $ Para [TeX $ "\\begin{" ++ name' ++ "}" ++ argStr ++ - concat contents ++ "\\end{" ++ name' ++ "}"] + return $ "\\begin{" ++ name' ++ "}" ++ argStr ++ + concat contents ++ "\\end{" ++ name' ++ "}" unknownEnvironment :: GenParser Char ParserState Block unknownEnvironment = try $ do diff --git a/Text/Pandoc/Readers/Markdown.hs b/Text/Pandoc/Readers/Markdown.hs index cf1053628..ac7096d37 100644 --- a/Text/Pandoc/Readers/Markdown.hs +++ b/Text/Pandoc/Readers/Markdown.hs @@ -38,7 +38,7 @@ import Data.Char ( isAlphaNum, isAlpha, isLower, isDigit ) import Data.Maybe import Text.Pandoc.Definition import Text.Pandoc.Shared -import Text.Pandoc.Readers.LaTeX ( rawLaTeXInline, rawLaTeXEnvironment ) +import Text.Pandoc.Readers.LaTeX ( rawLaTeXInline, rawLaTeXEnvironment' ) import Text.Pandoc.Readers.HTML ( rawHtmlBlock, anyHtmlBlockTag, anyHtmlInlineTag, anyHtmlTag, anyHtmlEndTag, htmlEndTag, extractTagType, @@ -272,7 +272,6 @@ block = do , bulletList , orderedList , definitionList - , rawLaTeXEnvironment , para , rawHtmlBlocks , plain @@ -1144,7 +1143,27 @@ inlineNote = try $ do return $ Note [Para contents] rawLaTeXInline' :: GenParser Char ParserState Inline -rawLaTeXInline' = failIfStrict >> rawLaTeXInline +rawLaTeXInline' = do + failIfStrict + (rawConTeXtEnvironment' >>= return . TeX) + <|> (rawLaTeXEnvironment' >>= return . TeX) + <|> rawLaTeXInline + +rawConTeXtEnvironment' :: GenParser Char st String +rawConTeXtEnvironment' = try $ do + string "\\start" + completion <- inBrackets (letter <|> digit <|> spaceChar) + <|> (many1 letter) + contents <- manyTill (rawConTeXtEnvironment' <|> (count 1 anyChar)) + (try $ string "\\stop" >> string completion) + return $ "\\start" ++ completion ++ concat contents ++ "\\stop" ++ completion + +inBrackets :: (GenParser Char st Char) -> GenParser Char st String +inBrackets parser = do + char '[' + contents <- many parser + char ']' + return $ "[" ++ contents ++ "]" rawHtmlInline' :: GenParser Char ParserState Inline rawHtmlInline' = do diff --git a/tests/markdown-reader-more.native b/tests/markdown-reader-more.native index 7785f85fb..5fea0f02d 100644 --- a/tests/markdown-reader-more.native +++ b/tests/markdown-reader-more.native @@ -1,5 +1,8 @@ Pandoc (Meta [] [] "") [ Header 1 [Str "Additional",Space,Str "markdown",Space,Str "reader",Space,Str "tests"] , Header 2 [Str "Blank",Space,Str "line",Space,Str "before",Space,Str "URL",Space,Str "in",Space,Str "link",Space,Str "reference"] -, Para [Link [Str "foo"] ("/url",""),Space,Str "and",Space,Link [Str "bar"] ("/url","title")] ] +, Para [Link [Str "foo"] ("/url",""),Space,Str "and",Space,Link [Str "bar"] ("/url","title")] +, Header 2 [Str "Raw",Space,Str "ConTeXt",Space,Str "environments"] +, Para [TeX "\\placeformula",Space,TeX "\\startformula\n L_{1} = L_{2}\n \\stopformula"] +, Para [TeX "\\start[a2]\n\\start[a2]\n\\stop[a2]\n\\stop[a2]"] ] diff --git a/tests/markdown-reader-more.txt b/tests/markdown-reader-more.txt index 32f10d61c..6c6191fad 100644 --- a/tests/markdown-reader-more.txt +++ b/tests/markdown-reader-more.txt @@ -11,3 +11,14 @@ /url "title" +## Raw ConTeXt environments + +\placeformula \startformula + L_{1} = L_{2} + \stopformula + +\start[a2] +\start[a2] +\stop[a2] +\stop[a2] + |