@@ -1,3 +1,4 @@
1+{-# LANGUAGE OverloadedStrings #-}
12{-
23Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu>
34
@@ -32,10 +33,10 @@ import Text.Pandoc.Definition
3233import Text.Pandoc.Shared
3334import Text.Pandoc.Templates
3435import Text.Printf ( printf )
35−import Data.List ( (\\), isSuffixOf, isPrefixOf, intersperse, intercalate )
36+import Data.List ( (\\), isSuffixOf, isPrefixOf, intercalate )
3637import Data.Char ( toLower, isPunctuation )
3738import Control.Monad.State
38−import Text.PrettyPrint.HughesPJ hiding ( Str )
39+import Text.Pandoc.Pretty
3940import System.FilePath (dropExtension)
4041
4142data WriterState =
@@ -71,17 +72,21 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
7172 "{report}" `isSuffixOf` x)
7273 when (any usesBookClass (lines template)) $
7374 modify $ \s -> s{stBook = True}
74− titletext <- liftM render $ inlineListToLaTeX title
75− authorsText <- mapM (liftM render . inlineListToLaTeX) authors
76− dateText <- liftM render $ inlineListToLaTeX date
75+ opts <- liftM stOptions get
76+ let colwidth = if writerWrapText opts
77+ then Just $ writerColumns opts
78+ else Nothing
79+ titletext <- liftM (render colwidth) $ inlineListToLaTeX title
80+ authorsText <- mapM (liftM (render colwidth) . inlineListToLaTeX) authors
81+ dateText <- liftM (render colwidth) $ inlineListToLaTeX date
7782 let (blocks', lastHeader) = if writerCiteMethod options == Citeproc then
7883 (blocks, [])
7984 else case last blocks of
8085 Header 1 il -> (init blocks, il)
8186 _ -> (blocks, [])
8287 body <- blockListToLaTeX blocks'
83− biblioTitle <- liftM render $ inlineListToLaTeX lastHeader
84− let main = render body
88+ biblioTitle <- liftM (render colwidth) $ inlineListToLaTeX lastHeader
89+ let main = render colwidth body
8590 st <- get
8691 let biblioFiles = intercalate "," $ map dropExtension $ writerBiblioFiles options
8792 citecontext = case writerCiteMethod options of
@@ -152,20 +157,15 @@ deVerb (other:rest) = other:(deVerb rest)
152157blockToLaTeX :: Block -- ^ Block to convert
153158 -> State WriterState Doc
154159blockToLaTeX Null = return empty
155−blockToLaTeX (Plain lst) = do
156− st <- get
157− let opts = stOptions st
158− wrapTeXIfNeeded opts True inlineListToLaTeX lst
160+blockToLaTeX (Plain lst) = inlineListToLaTeX lst
159161blockToLaTeX (Para [Image txt (src,tit)]) = do
160162 capt <- inlineListToLaTeX txt
161163 img <- inlineToLaTeX (Image txt (src,tit))
162164 return $ text "\\begin{figure}[htb]" $$ text "\\centering" $$ img $$
163− (text "\\caption{" <> capt <> char '}') $$ text "\\end{figure}\n"
165+ (text "\\caption{" <> capt <> char '}') $$ text "\\end{figure}" $$ blankline
164166blockToLaTeX (Para lst) = do
165− st <- get
166− let opts = stOptions st
167− result <- wrapTeXIfNeeded opts True inlineListToLaTeX lst
168− return $ result <> char '\n'
167+ result <- inlineListToLaTeX lst
168+ return $ result <> blankline
169169blockToLaTeX (BlockQuote lst) = do
170170 contents <- blockListToLaTeX lst
171171 return $ text "\\begin{quote}" $$ contents $$ text "\\end{quote}"
@@ -181,8 +181,8 @@ blockToLaTeX (CodeBlock (_,classes,_) str) = do
181181 modify $ \s -> s{ stVerbInNote = True }
182182 return "Verbatim"
183183 else return "verbatim"
184− return $ text ("\\begin{" ++ env ++ "}\n") <> text str <>
185− text ("\n\\end{" ++ env ++ "}")
184+ return $ "\\begin{" <> text env <> "}" $$ flush (text str) $$
185+ "\\end{" <> text env <> "}" $$ cr -- final cr needed because of footnotes
186186blockToLaTeX (RawHtml _) = return empty
187187blockToLaTeX (BulletList lst) = do
188188 items <- mapM listItemToLaTeX lst
@@ -211,8 +211,8 @@ blockToLaTeX (DefinitionList lst) = do
211211 items <- mapM defListItemToLaTeX lst
212212 return $ text "\\begin{description}" $$ vcat items $$
213213 text "\\end{description}"
214−blockToLaTeX HorizontalRule = return $ text $
215− "\\begin{center}\\rule{3in}{0.4pt}\\end{center}\n"
214+blockToLaTeX HorizontalRule = return $
215+ "\\begin{center}\\rule{3in}{0.4pt}\\end{center}" $$ blankline
216216blockToLaTeX (Header level lst) = do
217217 let lst' = deVerb lst
218218 txt <- inlineListToLaTeX lst'
@@ -229,7 +229,7 @@ blockToLaTeX (Header level lst) = do
229229 let stuffing = optional <> char '{' <> txt <> char '}'
230230 book <- liftM stBook get
231231 let level' = if book then level - 1 else level
232− let headerWith x y = text x <> y <> char '\n'
232+ let headerWith x y = text x <> y $$ blankline
233233 return $ case level' of
234234 0 -> headerWith "\\chapter" stuffing
235235 1 -> headerWith "\\section" stuffing
@@ -237,7 +237,7 @@ blockToLaTeX (Header level lst) = do
237237 3 -> headerWith "\\subsubsection" stuffing
238238 4 -> headerWith "\\paragraph" stuffing
239239 5 -> headerWith "\\subparagraph" stuffing
240− _ -> txt <> char '\n'
240+ _ -> txt $$ blankline
241241blockToLaTeX (Table caption aligns widths heads rows) = do
242242 headers <- if all null heads
243243 then return empty
@@ -246,13 +246,13 @@ blockToLaTeX (Table caption aligns widths heads rows) = do
246246 rows' <- mapM tableRowToLaTeX rows
247247 let colDescriptors = concat $ zipWith toColDescriptor widths aligns
248248 let tableBody = text ("\\begin{tabular}{" ++ colDescriptors ++ "}") $$
249− headers $$ vcat rows' $$ text "\\end{tabular}"
249+ headers $$ vcat rows' $$ text "\\end{tabular}"
250250 let centered txt = text "\\begin{center}" $$ txt $$ text "\\end{center}"
251251 modify $ \s -> s{ stTable = True }
252252 return $ if isEmpty captionText
253− then centered tableBody <> char '\n'
254− else text "\\begin{table}[h]" $$ centered tableBody $$
255− inCmd "caption" captionText $$ text "\\end{table}\n"
253+ then centered tableBody $$ blankline
254+ else text "\\begin{table}[h]" $$ centered tableBody $$
255+ inCmd "caption" captionText $$ text "\\end{table}" $$ blankline
256256
257257toColDescriptor :: Double -> Alignment -> String
258258toColDescriptor 0 align =
@@ -285,7 +285,7 @@ listItemToLaTeX lst = blockListToLaTeX lst >>= return . (text "\\item" $$) .
285285defListItemToLaTeX :: ([Inline], [[Block]]) -> State WriterState Doc
286286defListItemToLaTeX (term, defs) = do
287287 term' <- inlineListToLaTeX $ deVerb term
288− def' <- liftM (vcat . intersperse (text "")) $ mapM blockListToLaTeX defs
288+ def' <- liftM vsep $ mapM blockListToLaTeX defs
289289 return $ text "\\item[" <> term' <> text "]" $$ def'
290290
291291-- | Convert list of inline elements to LaTeX.
@@ -360,7 +360,7 @@ inlineToLaTeX (Math DisplayMath str) = return $ text "\\[" <> text str <> text "
360360inlineToLaTeX (TeX str) = return $ text str
361361inlineToLaTeX (HtmlInline _) = return empty
362362inlineToLaTeX (LineBreak) = return $ text "\\\\"
363−inlineToLaTeX Space = return $ char ' '
363+inlineToLaTeX Space = return space
364364inlineToLaTeX (Link txt (src, _)) =
365365 case txt of
366366 [Code x] | x == src -> -- autolink
@@ -373,15 +373,11 @@ inlineToLaTeX (Image _ (source, _)) = do
373373 modify $ \s -> s{ stGraphics = True }
374374 return $ text $ "\\includegraphics{" ++ source ++ "}"
375375inlineToLaTeX (Note contents) = do
376− st <- get
377− put (st {stInNote = True})
376+ modify (\s -> s{stInNote = True})
378377 contents' <- blockListToLaTeX contents
379378 modify (\s -> s {stInNote = False})
380− let rawnote = stripTrailingNewlines $ render contents'
381379 -- note: a \n before } is needed when note ends with a Verbatim environment
382− let optNewline = "\\end{Verbatim}" `isSuffixOf` rawnote
383− return $ text "\\footnote{" <>
384− text rawnote <> (if optNewline then char '\n' else empty) <> char '}'
380+ return $ text "\\footnote{" <> nest 2 contents' <> char '}'
385381
386382
387383citationsToNatbib :: [Citation] -> State WriterState Doc