@@ -30,17 +30,19 @@ Conversion of 'Pandoc' format into Texinfo.
30module Text.Pandoc.Writers.Texinfo ( writeTexinfo ) where
30module Text.Pandoc.Writers.Texinfo ( writeTexinfo ) where
31import Text.Pandoc.Definition
31import Text.Pandoc.Definition
32import Text.Pandoc.Shared
32import Text.Pandoc.Shared
33+import Text.Pandoc.Templates (renderTemplate)
33import Text.Pandoc.Readers.TeXMath
34import Text.Pandoc.Readers.TeXMath
34import Text.Printf ( printf )
35import Text.Printf ( printf )
35import Data.List ( isSuffixOf, transpose, maximumBy )
36import Data.List ( isSuffixOf, transpose, maximumBy )
36import Data.Ord ( comparing )
37import Data.Ord ( comparing )
37import Data.Char ( chr, ord )
38import Data.Char ( chr, ord )
38−import qualified Data.Set as S
39import Control.Monad.State
39import Control.Monad.State
40import Text.PrettyPrint.HughesPJ hiding ( Str )
40import Text.PrettyPrint.HughesPJ hiding ( Str )
42data WriterState =
42data WriterState =
43− WriterState { stIncludes :: S.Set String -- strings to include in header
43+ WriterState { stStrikeout :: Bool -- document contains strikeout
44+ , stSuperscript :: Bool -- document contains superscript
45+ , stSubscript :: Bool -- document contains subscript
@@ -48,81 +50,45 @@ data WriterState =
48 - generated .texi files don't work when run through texi2dvi
50 - generated .texi files don't work when run through texi2dvi
51−-- | Add line to header.
52−addToHeader :: String -> State WriterState ()
55− let includes = stIncludes st
56− put st {stIncludes = S.insert str includes}
58-- | Convert Pandoc to Texinfo.
53-- | Convert Pandoc to Texinfo.
59writeTexinfo :: WriterOptions -> Pandoc -> String
54writeTexinfo :: WriterOptions -> Pandoc -> String
60writeTexinfo options document =
55writeTexinfo options document =
61− render $ evalState (pandocToTexinfo options $ wrapTop document) $
56+ evalState (pandocToTexinfo options $ wrapTop document) $
62− WriterState { stIncludes = S.empty }
57+ WriterState { stStrikeout = False, stSuperscript = False, stSubscript = False }
64-- | Add a "Top" node around the document, needed by Texinfo.
59-- | Add a "Top" node around the document, needed by Texinfo.
65wrapTop :: Pandoc -> Pandoc
60wrapTop :: Pandoc -> Pandoc
66wrapTop (Pandoc (Meta title authors date) blocks) =
61wrapTop (Pandoc (Meta title authors date) blocks) =
67 Pandoc (Meta title authors date) (Header 0 title : blocks)
62 Pandoc (Meta title authors date) (Header 0 title : blocks)
69−pandocToTexinfo :: WriterOptions -> Pandoc -> State WriterState Doc
64+pandocToTexinfo :: WriterOptions -> Pandoc -> State WriterState String
70−pandocToTexinfo options (Pandoc meta blocks) = do
65+pandocToTexinfo options (Pandoc (Meta title authors date) blocks) = do
71− return empty -- TODO
66+ titleText <- inlineListToTexinfo title
67+ authorsText <- mapM inlineListToTexinfo authors
68+ dateText <- inlineListToTexinfo date
69+ let titlePage = not $ all null $ title : date : authors
72−-- main <- blockListToTexinfo blocks
70+ main <- blockListToTexinfo blocks
73−-- head' <- if writerStandalone options
71+ st <- get
74−-- then texinfoHeader options meta
76−-- let before = if null (writerIncludeBefore options)
72+ let before = if null (writerIncludeBefore options)
77−-- then empty
73+ then empty
78−-- else text (writerIncludeBefore options)
74+ else text (writerIncludeBefore options)
79−-- let after = if null (writerIncludeAfter options)
75+ let after = if null (writerIncludeAfter options)
80−-- then empty
76+ then empty
81−-- else text (writerIncludeAfter options)
77+ else text (writerIncludeAfter options)
82−-- let body = before $$ main $$ after
78+ let body = render $ before $$ main $$ after
83−-- -- XXX toc untested
79+ let context = writerVariables options ++
81+ , ("title", render titleText)
82+ , ("date", render dateText) ] ++
84−-- let toc = if writerTableOfContents options
83+ [ ("toc", "yes") | writerTableOfContents options ] ++
85−-- then text "@contents"
84+ [ ("titlepage", "yes") | titlePage ] ++
86−-- else empty
85+ [ ("subscript", "yes") | stSubscript st ] ++
87−-- let foot = if writerStandalone options
86+ [ ("superscript", "yes") | stSuperscript st ] ++
88−-- then text "@bye"
87+ [ ("strikeout", "yes") | stStrikeout st ] ++
89−-- else empty
88+ [ ("author", render a) | a <- authorsText ]
90−-- return $ head' $$ toc $$ body $$ foot
89+ if writerStandalone options
91−
90+ then return $ renderTemplate context $ writerTemplate options
92−-- | Insert bibliographic information into Texinfo header.
91+ else return body
93−texinfoHeader :: WriterOptions -- ^ Options, including Texinfo header
94− -> Meta -- ^ Meta with bibliographic information
95− -> State WriterState Doc
96−texinfoHeader options (Meta title authors date) = do
98−-- titletext <- if null title
101−-- t <- inlineListToTexinfo title
102−-- return $ text "@title " <> t
103−-- headerIncludes <- get >>= return . S.toList . stIncludes
104−-- let extras = text $ unlines headerIncludes
105−-- let authorstext = map makeAuthor authors
106−-- let datetext = if date == ""
108−-- else text $ stringToTexinfo date
110−-- let baseHeader = case writerHeader options of
113−-- let header = text "@documentencoding utf-8" $$ baseHeader $$ extras
114−-- return $ text "\\input texinfo" $$
116−-- text "@ifnottex" $$
117−-- text "@paragraphindent 0" $$
118−-- text "@end ifnottex" $$
119−-- text "@titlepage" $$
120−-- titletext $$ vcat authorstext $$
122−-- text "@end titlepage"
124−makeAuthor :: String -> Doc
125−makeAuthor author = text $ "@author " ++ (stringToTexinfo author)
127-- | Escape things as needed for Texinfo.
93-- | Escape things as needed for Texinfo.
128stringToTexinfo :: String -> String
94stringToTexinfo :: String -> String
@@ -397,33 +363,17 @@ inlineToTexinfo (Strong lst) =
397 inlineListToTexinfo lst >>= return . inCmd "strong"
363 inlineListToTexinfo lst >>= return . inCmd "strong"
399inlineToTexinfo (Strikeout lst) = do
365inlineToTexinfo (Strikeout lst) = do
400− addToHeader $ "@macro textstrikeout{text}\n" ++
366+ modify $ \st -> st{ stStrikeout = True }
403 contents <- inlineListToTexinfo lst
367 contents <- inlineListToTexinfo lst
404 return $ text "@textstrikeout{" <> contents <> text "}"
368 return $ text "@textstrikeout{" <> contents <> text "}"
406inlineToTexinfo (Superscript lst) = do
370inlineToTexinfo (Superscript lst) = do
407− addToHeader $ "@macro textsuperscript{text}\n" ++
371+ modify $ \st -> st{ stSuperscript = True }
409− "@textsuperscript{\\text\\}\n" ++
412− "^@{\\text\\@}\n" ++
413− "@end ifnottex\n" ++
415 contents <- inlineListToTexinfo lst
372 contents <- inlineListToTexinfo lst
416 return $ text "@textsuperscript{" <> contents <> char '}'
373 return $ text "@textsuperscript{" <> contents <> char '}'
418inlineToTexinfo (Subscript lst) = do
375inlineToTexinfo (Subscript lst) = do
419− addToHeader $ "@macro textsubscript{text}\n" ++
376+ modify $ \st -> st{ stSubscript = True }
421− "@textsubscript{\\text\\}\n" ++
424− "_@{\\text\\@}\n" ++
425− "@end ifnottex\n" ++
427 contents <- inlineListToTexinfo lst
377 contents <- inlineListToTexinfo lst
428 return $ text "@textsubscript{" <> contents <> char '}'
378 return $ text "@textsubscript{" <> contents <> char '}'