summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/LaTeX.hs
diff options
context:
space:
mode:
authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>2006-12-20 06:50:14 +0000
committerfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>2006-12-20 06:50:14 +0000
commitdc9c6450f3b16592d0ee865feafc17b670e4ad14 (patch)
treedc29955e1ea518d6652af3d12876863b19819f6d /src/Text/Pandoc/Readers/LaTeX.hs
parent42d29838960f9aed3a08a4d76fc7e9c3941680a8 (diff)
+ Added module data for haddock.
+ Reformatted code consistently. git-svn-id: https://pandoc.googlecode.com/svn/trunk@252 788f1e2b-df1e-0410-8736-df70ead52e1b
Diffstat (limited to 'src/Text/Pandoc/Readers/LaTeX.hs')
-rw-r--r--src/Text/Pandoc/Readers/LaTeX.hs118
1 files changed, 66 insertions, 52 deletions
diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index a62ff7b94..81004b1f1 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -1,4 +1,14 @@
--- | Converts LaTeX to 'Pandoc' document.
+{- |
+ Module : Text.Pandoc.Readers.LaTeX
+ Copyright : Copyright (C) 2006 John MacFarlane
+ License : GNU GPL, version 2 or above
+
+ Maintainer : John MacFarlane <jgm at berkeley dot edu>
+ Stability : unstable
+ Portability : portable
+
+Conversion of LaTeX to 'Pandoc' document.
+-}
module Text.Pandoc.Readers.LaTeX (
readLaTeX,
rawLaTeXInline,
@@ -81,7 +91,8 @@ end name = try (do
spaces
return name)
--- | Returns a list of block elements containing the contents of an environment.
+-- | Returns a list of block elements containing the contents of an
+-- environment.
environment name = try (do
begin name
spaces
@@ -104,15 +115,16 @@ anyEnvironment = try (do
-- | Process LaTeX preamble, extracting metadata.
processLaTeXPreamble = do
- manyTill (choice [bibliographic, comment, unknownCommand, nullBlock]) (try (string "\\begin{document}"))
+ manyTill (choice [bibliographic, comment, unknownCommand, nullBlock])
+ (try (string "\\begin{document}"))
spaces
-- | Parse LaTeX and return 'Pandoc'.
parseLaTeX = do
- option () processLaTeXPreamble -- preamble might not be present, if a fragment
+ option () processLaTeXPreamble -- preamble might not be present (fragment)
blocks <- parseBlocks
spaces
- option "" (string "\\end{document}") -- if parsing a fragment, this might not be present
+ option "" (string "\\end{document}") -- might not be present (in fragment)
spaces
eof
state <- getState
@@ -122,7 +134,8 @@ parseLaTeX = do
let title' = stateTitle state
let authors' = stateAuthors state
let date' = stateDate state
- return (Pandoc (Meta title' authors' date') (blocks' ++ (reverse noteBlocks) ++ (reverse keyBlocks)))
+ return (Pandoc (Meta title' authors' date')
+ (blocks' ++ (reverse noteBlocks) ++ (reverse keyBlocks)))
--
-- parsing blocks
@@ -133,9 +146,10 @@ parseBlocks = do
result <- many block
return result
-block = choice [ hrule, codeBlock, header, list, blockQuote, mathBlock, comment,
- bibliographic, para, specialEnvironment, itemBlock, unknownEnvironment,
- unknownCommand ] <?> "block"
+block = choice [ hrule, codeBlock, header, list, blockQuote, mathBlock,
+ comment, bibliographic, para, specialEnvironment,
+ itemBlock, unknownEnvironment, unknownCommand ] <?>
+ "block"
--
-- header blocks
@@ -157,7 +171,8 @@ headerLevel n = try (do
--
hrule = try (do
- oneOfStrings [ "\\begin{center}\\rule{3in}{0.4pt}\\end{center}\n\n", "\\newpage" ]
+ oneOfStrings [ "\\begin{center}\\rule{3in}{0.4pt}\\end{center}\n\n",
+ "\\newpage" ]
spaces
return HorizontalRule)
@@ -166,8 +181,10 @@ hrule = try (do
--
codeBlock = try (do
- string "\\begin{verbatim}" -- don't use begin function because it gobbles whitespace
- option "" blanklines -- we want to gobble blank lines, but not leading space
+ string "\\begin{verbatim}" -- don't use begin function because it
+ -- gobbles whitespace
+ option "" blanklines -- we want to gobble blank lines, but not
+ -- leading space
contents <- manyTill anyChar (try (string "\\end{verbatim}"))
spaces
return (CodeBlock (stripTrailingNewlines contents)))
@@ -266,7 +283,8 @@ authors = try (do
string "\\author{"
authors <- manyTill anyChar (char '}')
spaces
- let authors' = map removeLeadingTrailingSpace $ lines $ gsub "\\\\\\\\" "\n" authors
+ let authors' = map removeLeadingTrailingSpace $ lines $
+ gsub "\\\\\\\\" "\n" authors
updateState (\state -> state { stateAuthors = authors' })
return Null)
@@ -286,21 +304,19 @@ date = try (do
itemBlock = try (do
("item", _, args) <- command
state <- getState
- if (stateParserContext state == ListItemState) then
- fail "item should be handled by list block"
- else
- if null args then
- return Null
- else
- return (Plain [Str (stripFirstAndLast (head args))]))
+ if (stateParserContext state == ListItemState)
+ then fail "item should be handled by list block"
+ else if null args
+ then return Null
+ else return (Plain [Str (stripFirstAndLast (head args))]))
--
-- raw LaTeX
--
specialEnvironment = do -- these are always parsed as raw
- followedBy' (choice (map (\name -> begin name) ["tabular", "figure", "tabbing", "eqnarry",
- "picture", "table", "verse", "theorem"]))
+ followedBy' (choice (map (\name -> begin name) ["tabular", "figure",
+ "tabbing", "eqnarry", "picture", "table", "verse", "theorem"]))
rawLaTeXEnvironment
-- | Parse any LaTeX environment and return a Para block containing
@@ -316,18 +332,20 @@ rawLaTeXEnvironment = try (do
args <- option [] commandArgs
let argStr = concat args
contents <- manyTill (choice [(many1 (noneOf "\\")),
- (do{ (Para [TeX str]) <- rawLaTeXEnvironment; return str }),
- string "\\"]) (end name')
+ (do
+ (Para [TeX str]) <- rawLaTeXEnvironment
+ return str),
+ string "\\" ])
+ (end name')
spaces
return (Para [TeX ("\\begin{" ++ name' ++ "}" ++ argStr ++
- (concat contents) ++ "\\end{" ++ name' ++ "}")]))
+ (concat contents) ++ "\\end{" ++ name' ++ "}")]))
unknownEnvironment = try (do
state <- getState
- result <- if stateParseRaw state then -- check to see whether we should include raw TeX
- rawLaTeXEnvironment -- if so, get the whole raw environment
- else
- anyEnvironment -- otherwise just the contents
+ result <- if stateParseRaw state -- check whether we should include raw TeX
+ then rawLaTeXEnvironment -- if so, get whole raw environment
+ else anyEnvironment -- otherwise just the contents
return result)
unknownCommand = try (do
@@ -338,14 +356,12 @@ unknownCommand = try (do
spaces
let argStr = concat args
state <- getState
- if (name == "item") && ((stateParserContext state) == ListItemState) then
- fail "should not be parsed as raw"
- else
- string ""
- if stateParseRaw state then
- return (Plain [TeX ("\\" ++ name ++ star ++ argStr)])
- else
- return (Plain [Str (joinWithSep " " args)]))
+ if (name == "item") && ((stateParserContext state) == ListItemState)
+ then fail "should not be parsed as raw"
+ else string ""
+ if stateParseRaw state
+ then return (Plain [TeX ("\\" ++ name ++ star ++ argStr)])
+ else return (Plain [Str (joinWithSep " " args)]))
-- latex comment
comment = try (do
@@ -358,9 +374,9 @@ comment = try (do
-- inline
--
-inline = choice [ strong, emph, ref, lab, code, linebreak, math, ldots, accentedChar,
- specialChar, specialInline, escapedChar, unescapedChar, str,
- endline, whitespace ] <?> "inline"
+inline = choice [ strong, emph, ref, lab, code, linebreak, math, ldots,
+ accentedChar, specialChar, specialInline, escapedChar,
+ unescapedChar, str, endline, whitespace ] <?> "inline"
specialInline = choice [ link, image, footnote, rawLaTeXInline ] <?>
"link, raw TeX, note, or image"
@@ -397,8 +413,8 @@ accentTable =
('o', [('`', 242), ('\'', 243), ('^', 244), ('~', 245), ('"', 246)]),
('u', [('`', 249), ('\'', 250), ('^', 251), ('"', 252)]) ]
-specialAccentedChar = choice [ ccedil, aring, iuml, szlig, aelig, oslash, pound,
- euro, copyright, sect ]
+specialAccentedChar = choice [ ccedil, aring, iuml, szlig, aelig,
+ oslash, pound, euro, copyright, sect ]
ccedil = try (do
char '\\'
@@ -563,15 +579,14 @@ image = try (do
footnote = try (do
(name, _, (contents:[])) <- command
- if ((name == "footnote") || (name == "thanks")) then
- string ""
- else
- fail "not a footnote or thanks command"
+ if ((name == "footnote") || (name == "thanks"))
+ then string ""
+ else fail "not a footnote or thanks command"
let contents' = stripFirstAndLast contents
state <- getState
let blocks = case runParser parseBlocks state "footnote" contents of
- Left err -> error $ "Input:\n" ++ show contents' ++
- "\nError:\n" ++ show err
+ Left err -> error $ "Input:\n" ++ show contents' ++
+ "\nError:\n" ++ show err
Right result -> result
let notes = stateNoteBlocks state
let nextRef = case notes of
@@ -586,8 +601,7 @@ rawLaTeXInline = try (do
(name, star, args) <- command
let argStr = concat args
state <- getState
- if ((name == "begin") || (name == "end") || (name == "item")) then
- fail "not an inline command"
- else
- string ""
+ if ((name == "begin") || (name == "end") || (name == "item"))
+ then fail "not an inline command"
+ else string ""
return (TeX ("\\" ++ name ++ star ++ argStr)))