git.iko.soy/typeable/pandoc
on master help sign in
Updated texinfo writer to use new templates.

git-svn-id: https://pandoc.googlecode.com/svn/trunk@1735 788f1e2b-df1e-0410-8736-df70ead52e1b

commit3f53d6f27045576665a6241693fdd4071842806f authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateThu 31 Dec 2009, 01:17 (17 years ago) parentcc6294c
3 files changed+86−125
src/Text/Pandoc/Writers/Texinfo.hs+37−87 templates/texinfo.template+46−37 tests/writer.texinfo+3−1
src/Text/Pandoc/Writers/Texinfo.hs+37−87
@@ -30,17 +30,19 @@ Conversion of 'Pandoc' format into Texinfo.
3030module Text.Pandoc.Writers.Texinfo ( writeTexinfo ) where
3131import Text.Pandoc.Definition
3232import Text.Pandoc.Shared
33+import Text.Pandoc.Templates (renderTemplate)
3334import Text.Pandoc.Readers.TeXMath
3435import Text.Printf ( printf )
3536import Data.List ( isSuffixOf, transpose, maximumBy )
3637import Data.Ord ( comparing )
3738import Data.Char ( chr, ord )
38−import qualified Data.Set as S
3939import Control.Monad.State
4040import Text.PrettyPrint.HughesPJ hiding ( Str )
4141
4242data 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
4446 }
4547
4648{- TODO:
@@ -48,81 +50,45 @@ data WriterState =
4850 - generated .texi files don't work when run through texi2dvi
4951 -}
5052
51−-- | Add line to header.
52−addToHeader :: String -> State WriterState ()
53−addToHeader str = do
54− st <- get
55− let includes = stIncludes st
56− put st {stIncludes = S.insert str includes}
57−
5853-- | Convert Pandoc to Texinfo.
5954writeTexinfo :: WriterOptions -> Pandoc -> String
6055writeTexinfo options document =
61− render $ evalState (pandocToTexinfo options $ wrapTop document) $
62− WriterState { stIncludes = S.empty }
56+ evalState (pandocToTexinfo options $ wrapTop document) $
57+ WriterState { stStrikeout = False, stSuperscript = False, stSubscript = False }
6358
6459-- | Add a "Top" node around the document, needed by Texinfo.
6560wrapTop :: Pandoc -> Pandoc
6661wrapTop (Pandoc (Meta title authors date) blocks) =
6762 Pandoc (Meta title authors date) (Header 0 title : blocks)
6863
69−pandocToTexinfo :: WriterOptions -> Pandoc -> State WriterState Doc
70−pandocToTexinfo options (Pandoc meta blocks) = do
71− return empty -- TODO
72−-- main <- blockListToTexinfo blocks
73−-- head' <- if writerStandalone options
74−-- then texinfoHeader options meta
75−-- else return empty
76−-- let before = if null (writerIncludeBefore options)
77−-- then empty
78−-- else text (writerIncludeBefore options)
79−-- let after = if null (writerIncludeAfter options)
80−-- then empty
81−-- else text (writerIncludeAfter options)
82−-- let body = before $$ main $$ after
83−-- -- XXX toc untested
84−-- let toc = if writerTableOfContents options
85−-- then text "@contents"
86−-- else empty
87−-- let foot = if writerStandalone options
88−-- then text "@bye"
89−-- else empty
90−-- return $ head' $$ toc $$ body $$ foot
91−
92−-- | Insert bibliographic information into Texinfo header.
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
97− return empty -- TODO
98−-- titletext <- if null title
99−-- then return empty
100−-- else do
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 == ""
107−-- then empty
108−-- else text $ stringToTexinfo date
109−--
110−-- let baseHeader = case writerHeader options of
111−-- "" -> empty
112−-- x -> text x
113−-- let header = text "@documentencoding utf-8" $$ baseHeader $$ extras
114−-- return $ text "\\input texinfo" $$
115−-- header $$
116−-- text "@ifnottex" $$
117−-- text "@paragraphindent 0" $$
118−-- text "@end ifnottex" $$
119−-- text "@titlepage" $$
120−-- titletext $$ vcat authorstext $$
121−-- datetext $$
122−-- text "@end titlepage"
123−
124−makeAuthor :: String -> Doc
125−makeAuthor author = text $ "@author " ++ (stringToTexinfo author)
64+pandocToTexinfo :: WriterOptions -> Pandoc -> State WriterState String
65+pandocToTexinfo options (Pandoc (Meta title authors date) blocks) = do
66+ titleText <- inlineListToTexinfo title
67+ authorsText <- mapM inlineListToTexinfo authors
68+ dateText <- inlineListToTexinfo date
69+ let titlePage = not $ all null $ title : date : authors
70+ main <- blockListToTexinfo blocks
71+ st <- get
72+ let before = if null (writerIncludeBefore options)
73+ then empty
74+ else text (writerIncludeBefore options)
75+ let after = if null (writerIncludeAfter options)
76+ then empty
77+ else text (writerIncludeAfter options)
78+ let body = render $ before $$ main $$ after
79+ let context = writerVariables options ++
80+ [ ("body", body)
81+ , ("title", render titleText)
82+ , ("date", render dateText) ] ++
83+ [ ("toc", "yes") | writerTableOfContents options ] ++
84+ [ ("titlepage", "yes") | titlePage ] ++
85+ [ ("subscript", "yes") | stSubscript st ] ++
86+ [ ("superscript", "yes") | stSuperscript st ] ++
87+ [ ("strikeout", "yes") | stStrikeout st ] ++
88+ [ ("author", render a) | a <- authorsText ]
89+ if writerStandalone options
90+ then return $ renderTemplate context $ writerTemplate options
91+ else return body
12692
12793-- | Escape things as needed for Texinfo.
12894stringToTexinfo :: String -> String
@@ -397,33 +363,17 @@ inlineToTexinfo (Strong lst) =
397363 inlineListToTexinfo lst >>= return . inCmd "strong"
398364
399365inlineToTexinfo (Strikeout lst) = do
400− addToHeader $ "@macro textstrikeout{text}\n" ++
401− "~~\\text\\~~\n" ++
402− "@end macro\n"
366+ modify $ \st -> st{ stStrikeout = True }
403367 contents <- inlineListToTexinfo lst
404368 return $ text "@textstrikeout{" <> contents <> text "}"
405369
406370inlineToTexinfo (Superscript lst) = do
407− addToHeader $ "@macro textsuperscript{text}\n" ++
408− "@iftex\n" ++
409− "@textsuperscript{\\text\\}\n" ++
410− "@end iftex\n" ++
411− "@ifnottex\n" ++
412− "^@{\\text\\@}\n" ++
413− "@end ifnottex\n" ++
414− "@end macro\n"
371+ modify $ \st -> st{ stSuperscript = True }
415372 contents <- inlineListToTexinfo lst
416373 return $ text "@textsuperscript{" <> contents <> char '}'
417374
418375inlineToTexinfo (Subscript lst) = do
419− addToHeader $ "@macro textsubscript{text}\n" ++
420− "@iftex\n" ++
421− "@textsubscript{\\text\\}\n" ++
422− "@end iftex\n" ++
423− "@ifnottex\n" ++
424− "_@{\\text\\@}\n" ++
425− "@end ifnottex\n" ++
426− "@end macro\n"
376+ modify $ \st -> st{ stSubscript = True }
427377 contents <- inlineListToTexinfo lst
428378 return $ text "@textsubscript{" <> contents <> char '}'
429379
templates/texinfo.template+46−37
@@ -1,51 +1,60 @@
1+$if(legacy-header)$
2+$legacy-header$
3+$else$
14\input texinfo
25@documentencoding utf-8
6+$endif$
7+$for(header-includes)$
38$header-includes$
9+$endfor$
10+
11+$if(strikeout)$
12+@macro textstrikeout{text}
13+~~\text\~~
14+@end macro
15+
16+$endif$
17+$if(subscript)$
18+@macro textsubscript{text}
19+@iftex
20+@textsubscript{\text\}
21+@end iftex
22+@ifnottex
23+_@{\text\@}
24+@end ifnottex
25+@end macro
26+
27+$endif$
28+$if(superscript)$
29+@macro textsuperscript{text}
30+@iftex
31+@textsuperscript{\text\}
32+@end iftex
33+@ifnottex
34+^@{\text\@}
35+@end ifnottex
36+@end macro
437
5−
38+$endif$
639@ifnottex
740@paragraphindent 0
841@end ifnottex
42+$if(titlepage)$
943@titlepage
10−@title @math{title}
11−@author $authors$
44+@title $title$
45+$for(author)$
46+@author $author$
47+$endfor$
48+$if(date)$
1249$date$
50+$endif$
1351@end titlepage
14−@contents
15−
16−@node Top
17−@top @math{title}
1852
19−@menu
20−* section oen::
21−@end menu
22−
23−@node section oen
24−@chapter section oen
25−@enumerate
26−@item
27−one
28−@enumerate a
29−@item
30−two
31−@enumerate 3
32−@item
33−three
34−@end enumerate
35−
36−@end enumerate
37−
38−@end enumerate
39−
40−@verbatim
41−hi
42−@end verbatim
43−
44−footnote@footnote{with code
45−@verbatim
46−code
47−@end verbatim
48−}
53+$endif$
54+$if(toc)$
55+@contents
4956
57+$endif$
58+$body$
5059
5160@bye
tests/writer.texinfo+3−1
@@ -1,5 +1,6 @@
11\input texinfo
22@documentencoding utf-8
3+
34@macro textstrikeout{text}
45~~\text\~~
56@end macro
@@ -22,7 +23,6 @@ _@{\text\@}
2223@end ifnottex
2324@end macro
2425
25−
2626@ifnottex
2727@paragraphindent 0
2828@end ifnottex
@@ -32,6 +32,7 @@ _@{\text\@}
3232@author Anonymous
3333July 17@comma{} 2006
3434@end titlepage
35+
3536@node Top
3637@top Pandoc Test Suite
3738
@@ -1010,3 +1011,4 @@ And in list items.@footnote{In list.}
10101011This paragraph should not be part of the note@comma{} as it is not indented.
10111012
10121013@bye
1014+