@@ -1,3 +1,4 @@
1+{-# LANGUAGE OverloadedStrings #-}
2Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu>
3Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu>
@@ -32,10 +33,10 @@ import Text.Pandoc.Definition
32import Text.Pandoc.Shared
33import Text.Pandoc.Shared
33import Text.Pandoc.Templates
34import Text.Pandoc.Templates
34import Text.Printf ( printf )
35import Text.Printf ( printf )
35−import Data.List ( (\\), isSuffixOf, isPrefixOf, intersperse, intercalate )
36+import Data.List ( (\\), isSuffixOf, isPrefixOf, intercalate )
36import Data.Char ( toLower, isPunctuation )
37import Data.Char ( toLower, isPunctuation )
37import Control.Monad.State
38import Control.Monad.State
38−import Text.PrettyPrint.HughesPJ hiding ( Str )
39+import Text.Pandoc.Pretty
39import System.FilePath (dropExtension)
40import System.FilePath (dropExtension)
41data WriterState =
42data WriterState =
@@ -71,17 +72,21 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
71 "{report}" `isSuffixOf` x)
72 "{report}" `isSuffixOf` x)
72 when (any usesBookClass (lines template)) $
73 when (any usesBookClass (lines template)) $
73 modify $ \s -> s{stBook = True}
74 modify $ \s -> s{stBook = True}
75+ opts <- liftM stOptions get
76+ let colwidth = if writerWrapText opts
77+ then Just $ writerColumns opts
74− titletext <- liftM render $ inlineListToLaTeX title
79+ titletext <- liftM (render colwidth) $ inlineListToLaTeX title
75− authorsText <- mapM (liftM render . inlineListToLaTeX) authors
80+ authorsText <- mapM (liftM (render colwidth) . inlineListToLaTeX) authors
76− dateText <- liftM render $ inlineListToLaTeX date
81+ dateText <- liftM (render colwidth) $ inlineListToLaTeX date
77 let (blocks', lastHeader) = if writerCiteMethod options == Citeproc then
82 let (blocks', lastHeader) = if writerCiteMethod options == Citeproc then
78 (blocks, [])
83 (blocks, [])
79 else case last blocks of
84 else case last blocks of
80 Header 1 il -> (init blocks, il)
85 Header 1 il -> (init blocks, il)
81 _ -> (blocks, [])
86 _ -> (blocks, [])
82 body <- blockListToLaTeX blocks'
87 body <- blockListToLaTeX blocks'
83− biblioTitle <- liftM render $ inlineListToLaTeX lastHeader
88+ biblioTitle <- liftM (render colwidth) $ inlineListToLaTeX lastHeader
84− let main = render body
89+ let main = render colwidth body
86 let biblioFiles = intercalate "," $ map dropExtension $ writerBiblioFiles options
91 let biblioFiles = intercalate "," $ map dropExtension $ writerBiblioFiles options
87 citecontext = case writerCiteMethod options of
92 citecontext = case writerCiteMethod options of
@@ -152,20 +157,15 @@ deVerb (other:rest) = other:(deVerb rest)
152blockToLaTeX :: Block -- ^ Block to convert
157blockToLaTeX :: Block -- ^ Block to convert
153 -> State WriterState Doc
158 -> State WriterState Doc
154blockToLaTeX Null = return empty
159blockToLaTeX Null = return empty
155−blockToLaTeX (Plain lst) = do
160+blockToLaTeX (Plain lst) = inlineListToLaTeX lst
157− let opts = stOptions st
158− wrapTeXIfNeeded opts True inlineListToLaTeX lst
159blockToLaTeX (Para [Image txt (src,tit)]) = do
161blockToLaTeX (Para [Image txt (src,tit)]) = do
160 capt <- inlineListToLaTeX txt
162 capt <- inlineListToLaTeX txt
161 img <- inlineToLaTeX (Image txt (src,tit))
163 img <- inlineToLaTeX (Image txt (src,tit))
162 return $ text "\\begin{figure}[htb]" $$ text "\\centering" $$ img $$
164 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
164blockToLaTeX (Para lst) = do
166blockToLaTeX (Para lst) = do
166− let opts = stOptions st
167− result <- wrapTeXIfNeeded opts True inlineListToLaTeX lst
167+ result <- inlineListToLaTeX lst
168− return $ result <> char '\n'
168+ return $ result <> blankline
169blockToLaTeX (BlockQuote lst) = do
169blockToLaTeX (BlockQuote lst) = do
170 contents <- blockListToLaTeX lst
170 contents <- blockListToLaTeX lst
171 return $ text "\\begin{quote}" $$ contents $$ text "\\end{quote}"
171 return $ text "\\begin{quote}" $$ contents $$ text "\\end{quote}"
@@ -181,8 +181,8 @@ blockToLaTeX (CodeBlock (_,classes,_) str) = do
181 modify $ \s -> s{ stVerbInNote = True }
181 modify $ \s -> s{ stVerbInNote = True }
182 return "Verbatim"
182 return "Verbatim"
183 else return "verbatim"
183 else return "verbatim"
184− return $ text ("\\begin{" ++ env ++ "}\n") <> text str <>
184+ return $ "\\begin{" <> text env <> "}" $$ flush (text str) $$
185− text ("\n\\end{" ++ env ++ "}")
185+ "\\end{" <> text env <> "}" $$ cr -- final cr needed because of footnotes
186blockToLaTeX (RawHtml _) = return empty
186blockToLaTeX (RawHtml _) = return empty
187blockToLaTeX (BulletList lst) = do
187blockToLaTeX (BulletList lst) = do
188 items <- mapM listItemToLaTeX lst
188 items <- mapM listItemToLaTeX lst
@@ -211,8 +211,8 @@ blockToLaTeX (DefinitionList lst) = do
211 items <- mapM defListItemToLaTeX lst
211 items <- mapM defListItemToLaTeX lst
212 return $ text "\\begin{description}" $$ vcat items $$
212 return $ text "\\begin{description}" $$ vcat items $$
213 text "\\end{description}"
213 text "\\end{description}"
214−blockToLaTeX HorizontalRule = return $ text $
214+blockToLaTeX HorizontalRule = return $
215− "\\begin{center}\\rule{3in}{0.4pt}\\end{center}\n"
215+ "\\begin{center}\\rule{3in}{0.4pt}\\end{center}" $$ blankline
216blockToLaTeX (Header level lst) = do
216blockToLaTeX (Header level lst) = do
217 let lst' = deVerb lst
217 let lst' = deVerb lst
218 txt <- inlineListToLaTeX lst'
218 txt <- inlineListToLaTeX lst'
@@ -229,7 +229,7 @@ blockToLaTeX (Header level lst) = do
229 let stuffing = optional <> char '{' <> txt <> char '}'
229 let stuffing = optional <> char '{' <> txt <> char '}'
230 book <- liftM stBook get
230 book <- liftM stBook get
231 let level' = if book then level - 1 else level
231 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
233 return $ case level' of
233 return $ case level' of
234 0 -> headerWith "\\chapter" stuffing
234 0 -> headerWith "\\chapter" stuffing
235 1 -> headerWith "\\section" stuffing
235 1 -> headerWith "\\section" stuffing
@@ -237,7 +237,7 @@ blockToLaTeX (Header level lst) = do
237 3 -> headerWith "\\subsubsection" stuffing
237 3 -> headerWith "\\subsubsection" stuffing
238 4 -> headerWith "\\paragraph" stuffing
238 4 -> headerWith "\\paragraph" stuffing
239 5 -> headerWith "\\subparagraph" stuffing
239 5 -> headerWith "\\subparagraph" stuffing
240− _ -> txt <> char '\n'
240+ _ -> txt $$ blankline
241blockToLaTeX (Table caption aligns widths heads rows) = do
241blockToLaTeX (Table caption aligns widths heads rows) = do
242 headers <- if all null heads
242 headers <- if all null heads
243 then return empty
243 then return empty
@@ -246,13 +246,13 @@ blockToLaTeX (Table caption aligns widths heads rows) = do
246 rows' <- mapM tableRowToLaTeX rows
246 rows' <- mapM tableRowToLaTeX rows
247 let colDescriptors = concat $ zipWith toColDescriptor widths aligns
247 let colDescriptors = concat $ zipWith toColDescriptor widths aligns
248 let tableBody = text ("\\begin{tabular}{" ++ colDescriptors ++ "}") $$
248 let tableBody = text ("\\begin{tabular}{" ++ colDescriptors ++ "}") $$
249− headers $$ vcat rows' $$ text "\\end{tabular}"
249+ headers $$ vcat rows' $$ text "\\end{tabular}"
250 let centered txt = text "\\begin{center}" $$ txt $$ text "\\end{center}"
250 let centered txt = text "\\begin{center}" $$ txt $$ text "\\end{center}"
251 modify $ \s -> s{ stTable = True }
251 modify $ \s -> s{ stTable = True }
252 return $ if isEmpty captionText
252 return $ if isEmpty captionText
253− then centered tableBody <> char '\n'
253+ then centered tableBody $$ blankline
254− else text "\\begin{table}[h]" $$ centered tableBody $$
254+ else text "\\begin{table}[h]" $$ centered tableBody $$
255− inCmd "caption" captionText $$ text "\\end{table}\n"
255+ inCmd "caption" captionText $$ text "\\end{table}" $$ blankline
257toColDescriptor :: Double -> Alignment -> String
257toColDescriptor :: Double -> Alignment -> String
258toColDescriptor 0 align =
258toColDescriptor 0 align =
@@ -285,7 +285,7 @@ listItemToLaTeX lst = blockListToLaTeX lst >>= return . (text "\\item" $$) .
285defListItemToLaTeX :: ([Inline], [[Block]]) -> State WriterState Doc
285defListItemToLaTeX :: ([Inline], [[Block]]) -> State WriterState Doc
286defListItemToLaTeX (term, defs) = do
286defListItemToLaTeX (term, defs) = do
287 term' <- inlineListToLaTeX $ deVerb term
287 term' <- inlineListToLaTeX $ deVerb term
288− def' <- liftM (vcat . intersperse (text "")) $ mapM blockListToLaTeX defs
288+ def' <- liftM vsep $ mapM blockListToLaTeX defs
289 return $ text "\\item[" <> term' <> text "]" $$ def'
289 return $ text "\\item[" <> term' <> text "]" $$ def'
291-- | Convert list of inline elements to LaTeX.
291-- | Convert list of inline elements to LaTeX.
@@ -360,7 +360,7 @@ inlineToLaTeX (Math DisplayMath str) = return $ text "\\[" <> text str <> text "
360inlineToLaTeX (TeX str) = return $ text str
360inlineToLaTeX (TeX str) = return $ text str
361inlineToLaTeX (HtmlInline _) = return empty
361inlineToLaTeX (HtmlInline _) = return empty
362inlineToLaTeX (LineBreak) = return $ text "\\\\"
362inlineToLaTeX (LineBreak) = return $ text "\\\\"
363−inlineToLaTeX Space = return $ char ' '
363+inlineToLaTeX Space = return space
364inlineToLaTeX (Link txt (src, _)) =
364inlineToLaTeX (Link txt (src, _)) =
365 case txt of
365 case txt of
366 [Code x] | x == src -> -- autolink
366 [Code x] | x == src -> -- autolink
@@ -373,15 +373,11 @@ inlineToLaTeX (Image _ (source, _)) = do
373 modify $ \s -> s{ stGraphics = True }
373 modify $ \s -> s{ stGraphics = True }
374 return $ text $ "\\includegraphics{" ++ source ++ "}"
374 return $ text $ "\\includegraphics{" ++ source ++ "}"
375inlineToLaTeX (Note contents) = do
375inlineToLaTeX (Note contents) = do
377− put (st {stInNote = True})
376+ modify (\s -> s{stInNote = True})
378 contents' <- blockListToLaTeX contents
377 contents' <- blockListToLaTeX contents
379 modify (\s -> s {stInNote = False})
378 modify (\s -> s {stInNote = False})
380− let rawnote = stripTrailingNewlines $ render contents'
381 -- note: a \n before } is needed when note ends with a Verbatim environment
379 -- note: a \n before } is needed when note ends with a Verbatim environment
382− let optNewline = "\\end{Verbatim}" `isSuffixOf` rawnote
383− return $ text "\\footnote{" <>
380+ return $ text "\\footnote{" <> nest 2 contents' <> char '}'
384− text rawnote <> (if optNewline then char '\n' else empty) <> char '}'
387citationsToNatbib :: [Citation] -> State WriterState Doc
383citationsToNatbib :: [Citation] -> State WriterState Doc