git.iko.soy/typeable/pandoc
on master help sign in
Updated texinfo writer to use new templates.
3f53d6ffiddlosopher
src/Text/Pandoc/Writers/Texinfo.hs+37−87
@@ -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 )
41
41
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
44 }
46 }
45
47
46{- TODO:
48{- TODO:
@@ -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
49 -}
51 -}
50
52
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−
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 }
63
58
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)
68
63
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
75−-- else return empty
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 ++
80+ [ ("body", body)
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
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)
126
92
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"
398
364
399inlineToTexinfo (Strikeout lst) = do
365inlineToTexinfo (Strikeout lst) = do
400− addToHeader $ "@macro textstrikeout{text}\n" ++
366+ modify $ \st -> st{ stStrikeout = True }
401− "~~\\text\\~~\n" ++
402− "@end macro\n"
403 contents <- inlineListToTexinfo lst
367 contents <- inlineListToTexinfo lst
404 return $ text "@textstrikeout{" <> contents <> text "}"
368 return $ text "@textstrikeout{" <> contents <> text "}"
405
369
406inlineToTexinfo (Superscript lst) = do
370inlineToTexinfo (Superscript lst) = do
407− addToHeader $ "@macro textsuperscript{text}\n" ++
371+ modify $ \st -> st{ stSuperscript = True }
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"
415 contents <- inlineListToTexinfo lst
372 contents <- inlineListToTexinfo lst
416 return $ text "@textsuperscript{" <> contents <> char '}'
373 return $ text "@textsuperscript{" <> contents <> char '}'
417
374
418inlineToTexinfo (Subscript lst) = do
375inlineToTexinfo (Subscript lst) = do
419− addToHeader $ "@macro textsubscript{text}\n" ++
376+ modify $ \st -> st{ stSubscript = True }
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"
427 contents <- inlineListToTexinfo lst
377 contents <- inlineListToTexinfo lst
428 return $ text "@textsubscript{" <> contents <> char '}'
378 return $ text "@textsubscript{" <> contents <> char '}'
429
379
templates/texinfo.template+46−37
@@ -1,51 +1,60 @@
1+$if(legacy-header)$
2+$legacy-header$
3+$else$
1\input texinfo
4\input texinfo
2@documentencoding utf-8
5@documentencoding utf-8
6+$endif$
7+$for(header-includes)$
3$header-includes$
8$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
4
37
5−
38+$endif$
6@ifnottex
39@ifnottex
7@paragraphindent 0
40@paragraphindent 0
8@end ifnottex
41@end ifnottex
42+$if(titlepage)$
9@titlepage
43@titlepage
10−@title @math{title}
44+@title $title$
45+$for(author)$
11−@author $authors$
46+@author $author$
47+$endfor$
48+$if(date)$
12$date$
49$date$
50+$endif$
13@end titlepage
51@end titlepage
14−@contents
15−
16−@node Top
17−@top @math{title}
18
52
19−@menu
53+$endif$
20−* section oen::
54+$if(toc)$
21−@end menu
55+@contents
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−}
49
56
57+$endif$
58+$body$
50
59
51@bye
60@bye
tests/writer.texinfo+3−1
@@ -1,5 +1,6 @@
1\input texinfo
1\input texinfo
2@documentencoding utf-8
2@documentencoding utf-8
3+
3@macro textstrikeout{text}
4@macro textstrikeout{text}
4~~\text\~~
5~~\text\~~
5@end macro
6@end macro
@@ -22,7 +23,6 @@ _@{\text\@}
22@end ifnottex
23@end ifnottex
23@end macro
24@end macro
24
25
25−
26@ifnottex
26@ifnottex
27@paragraphindent 0
27@paragraphindent 0
28@end ifnottex
28@end ifnottex
@@ -32,6 +32,7 @@ _@{\text\@}
32@author Anonymous
32@author Anonymous
33July 17@comma{} 2006
33July 17@comma{} 2006
34@end titlepage
34@end titlepage
35+
35@node Top
36@node Top
36@top Pandoc Test Suite
37@top Pandoc Test Suite
37
38
@@ -1010,3 +1011,4 @@ And in list items.@footnote{In list.}
1010This paragraph should not be part of the note@comma{} as it is not indented.
1011This paragraph should not be part of the note@comma{} as it is not indented.
1011
1012
1012@bye
1013@bye
1014+