git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: if book, report, or memoir documentclass, use \chapter{}

for first-level headers. Otherwise use \section{}. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1793 788f1e2b-df1e-0410-8736-df70ead52e1b

commite91cb556d298e9191ce3d23c8cc878e5b733621b authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSun 3 Jan 2010, 08:47 (17 years ago) parentaf46691
1 file changed+21−7
src/Text/Pandoc/Writers/LaTeX.hs+21−7
src/Text/Pandoc/Writers/LaTeX.hs+21−7
@@ -32,7 +32,7 @@ import Text.Pandoc.Definition
3232import Text.Pandoc.Shared
3333import Text.Pandoc.Templates
3434import Text.Printf ( printf )
35−import Data.List ( (\\), isSuffixOf, intersperse )
35+import Data.List ( (\\), isSuffixOf, isPrefixOf, intersperse )
3636import Data.Char ( toLower )
3737import Control.Monad.State
3838import Text.PrettyPrint.HughesPJ hiding ( Str )
@@ -50,6 +50,7 @@ data WriterState =
5050 , stUrl :: Bool -- true if document has visible URL link
5151 , stGraphics :: Bool -- true if document contains images
5252 , stLHS :: Bool -- true if document has literate haskell code
53+ , stBook :: Bool -- true if document uses book or memoir class
5354 }
5455
5556-- | Convert Pandoc to LaTeX.
@@ -60,10 +61,16 @@ writeLaTeX options document =
6061 stVerbInNote = False, stEnumerate = False,
6162 stTable = False, stStrikeout = False, stSubscript = False,
6263 stLink = False, stUrl = False, stGraphics = False,
63− stLHS = False }
64+ stLHS = False, stBook = False }
6465
6566pandocToLaTeX :: WriterOptions -> Pandoc -> State WriterState String
6667pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
68+ let template = writerTemplate options
69+ let usesBookClass x = "\\documentclass" `isPrefixOf` x &&
70+ ("{memoir}" `isSuffixOf` x || "{book}" `isSuffixOf` x ||
71+ "{report}" `isSuffixOf` x)
72+ when (any usesBookClass (lines template)) $
73+ modify $ \s -> s{stBook = True}
6774 titletext <- liftM render $ inlineListToLaTeX title
6875 authorsText <- mapM (liftM render . inlineListToLaTeX) authors
6976 dateText <- liftM render $ inlineListToLaTeX date
@@ -93,7 +100,7 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
93100 [ ("lhs", "yes") | stLHS st ] ++
94101 [ ("graphics", "yes") | stGraphics st ]
95102 return $ if writerStandalone options
96− then renderTemplate context $ writerTemplate options
103+ then renderTemplate context template
97104 else main
98105
99106-- escape things as needed for LaTeX
@@ -195,10 +202,17 @@ blockToLaTeX (Header level lst) = do
195202 else do
196203 res <- inlineListToLaTeX lstNoNotes
197204 return $ char '[' <> res <> char ']'
198− return $ if (level > 0) && (level <= 3)
199− then text ("\\" ++ (concat (replicate (level - 1) "sub")) ++
200− "section") <> optional <> char '{' <> txt <> text "}\n"
201− else txt <> char '\n'
205+ let stuffing = optional <> char '{' <> txt <> char '}'
206+ book <- liftM stBook get
207+ return $ case (book, level) of
208+ (True, 1) -> text "\\chapter" <> stuffing <> char '\n'
209+ (True, 2) -> text "\\section" <> stuffing <> char '\n'
210+ (True, 3) -> text "\\subsection" <> stuffing <> char '\n'
211+ (True, 4) -> text "\\subsubsection" <> stuffing <> char '\n'
212+ (False, 1) -> text "\\section" <> stuffing <> char '\n'
213+ (False, 2) -> text "\\subsection" <> stuffing <> char '\n'
214+ (False, 3) -> text "\\subsubsection" <> stuffing <> char '\n'
215+ _ -> txt <> char '\n'
202216blockToLaTeX (Table caption aligns widths heads rows) = do
203217 headers <- tableRowToLaTeX heads
204218 captionText <- inlineListToLaTeX caption