git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: if book, report, or memoir documentclass, use \chapter{}
e91cb55fiddlosopher
src/Text/Pandoc/Writers/LaTeX.hs+21−7
@@ -32,7 +32,7 @@ import Text.Pandoc.Definition
32import Text.Pandoc.Shared
32import Text.Pandoc.Shared
33import Text.Pandoc.Templates
33import Text.Pandoc.Templates
34import Text.Printf ( printf )
34import Text.Printf ( printf )
35−import Data.List ( (\\), isSuffixOf, intersperse )
35+import Data.List ( (\\), isSuffixOf, isPrefixOf, intersperse )
36import Data.Char ( toLower )
36import Data.Char ( toLower )
37import Control.Monad.State
37import Control.Monad.State
38import Text.PrettyPrint.HughesPJ hiding ( Str )
38import Text.PrettyPrint.HughesPJ hiding ( Str )
@@ -50,6 +50,7 @@ data WriterState =
50 , stUrl :: Bool -- true if document has visible URL link
50 , stUrl :: Bool -- true if document has visible URL link
51 , stGraphics :: Bool -- true if document contains images
51 , stGraphics :: Bool -- true if document contains images
52 , stLHS :: Bool -- true if document has literate haskell code
52 , stLHS :: Bool -- true if document has literate haskell code
53+ , stBook :: Bool -- true if document uses book or memoir class
53 }
54 }
54
55
55-- | Convert Pandoc to LaTeX.
56-- | Convert Pandoc to LaTeX.
@@ -60,10 +61,16 @@ writeLaTeX options document =
60 stVerbInNote = False, stEnumerate = False,
61 stVerbInNote = False, stEnumerate = False,
61 stTable = False, stStrikeout = False, stSubscript = False,
62 stTable = False, stStrikeout = False, stSubscript = False,
62 stLink = False, stUrl = False, stGraphics = False,
63 stLink = False, stUrl = False, stGraphics = False,
63− stLHS = False }
64+ stLHS = False, stBook = False }
64
65
65pandocToLaTeX :: WriterOptions -> Pandoc -> State WriterState String
66pandocToLaTeX :: WriterOptions -> Pandoc -> State WriterState String
66pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
67pandocToLaTeX 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}
67 titletext <- liftM render $ inlineListToLaTeX title
74 titletext <- liftM render $ inlineListToLaTeX title
68 authorsText <- mapM (liftM render . inlineListToLaTeX) authors
75 authorsText <- mapM (liftM render . inlineListToLaTeX) authors
69 dateText <- liftM render $ inlineListToLaTeX date
76 dateText <- liftM render $ inlineListToLaTeX date
@@ -93,7 +100,7 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
93 [ ("lhs", "yes") | stLHS st ] ++
100 [ ("lhs", "yes") | stLHS st ] ++
94 [ ("graphics", "yes") | stGraphics st ]
101 [ ("graphics", "yes") | stGraphics st ]
95 return $ if writerStandalone options
102 return $ if writerStandalone options
96− then renderTemplate context $ writerTemplate options
103+ then renderTemplate context template
97 else main
104 else main
98
105
99-- escape things as needed for LaTeX
106-- escape things as needed for LaTeX
@@ -195,10 +202,17 @@ blockToLaTeX (Header level lst) = do
195 else do
202 else do
196 res <- inlineListToLaTeX lstNoNotes
203 res <- inlineListToLaTeX lstNoNotes
197 return $ char '[' <> res <> char ']'
204 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"
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'
201− else txt <> char '\n'
215+ _ -> txt <> char '\n'
202blockToLaTeX (Table caption aligns widths heads rows) = do
216blockToLaTeX (Table caption aligns widths heads rows) = do
203 headers <- tableRowToLaTeX heads
217 headers <- tableRowToLaTeX heads
204 captionText <- inlineListToLaTeX caption
218 captionText <- inlineListToLaTeX caption