@@ -30,62 +30,56 @@ Conversion of 'Pandoc' documents to groff man page format.
3030-}
3131module Text.Pandoc.Writers.Man ( writeMan) where
3232import Text.Pandoc.Definition
33+import Text.Pandoc.Templates
3334import Text.Pandoc.Shared
3435import Text.Printf ( printf )
35−import Data.List ( isPrefixOf, drop, nub, intersperse, intercalate )
36+import Data.List ( isPrefixOf, drop, intersperse, intercalate )
3637import Text.PrettyPrint.HughesPJ hiding ( Str )
3738import Control.Monad.State
3839import Control.Monad ( liftM )
3940
4041type Notes = [[Block]]
41−type Preprocessors = [String] -- e.g. "t" for tbl
42−type WriterState = (Notes, Preprocessors)
42+data WriterState = WriterState { stNotes :: Notes
43+ , stHasTables :: Bool }
4344
4445-- | Convert Pandoc to Man.
4546writeMan :: WriterOptions -> Pandoc -> String
46−writeMan opts document = render $ evalState (pandocToMan opts document) ([],[])
47+writeMan opts document = evalState (pandocToMan opts document) (WriterState [] False)
4748
4849-- | Return groff man representation of document.
49−pandocToMan :: WriterOptions -> Pandoc -> State WriterState Doc
50−pandocToMan opts (Pandoc meta blocks) = do
51− return empty -- TODO
52−-- let before = writerIncludeBefore opts
53−-- let after = writerIncludeAfter opts
54−-- let before' = if null before then empty else text before
55−-- let after' = if null after then empty else text after
56−-- (head', foot) <- metaToMan opts meta
57−-- body <- blockListToMan opts blocks
58−-- (notes, preprocessors) <- get
59−-- let preamble = if null preprocessors || not (writerStandalone opts)
60−-- then empty
61−-- else text $ ".\\\" " ++ concat (nub preprocessors)
62−-- notes' <- notesToMan opts (reverse notes)
63−-- return $ preamble $$ head' $$ before' $$ body $$ notes' $$ foot $$ after'
64−
65−-- | Insert bibliographic information into Man header and footer.
66−metaToMan :: WriterOptions -- ^ Options, including Man header
67− -> Meta -- ^ Meta with bibliographic information
68− -> State WriterState (Doc, Doc)
69−metaToMan options (Meta title authors date) = do
70− titleText <- inlineListToMan options title
71− authorsText <- mapM (inlineListToMan options) authors
72− dateText <- inlineListToMan options date
50+pandocToMan :: WriterOptions -> Pandoc -> State WriterState String
51+pandocToMan opts (Pandoc (Meta title authors date) blocks) = do
52+ let before = writerIncludeBefore opts
53+ let after = writerIncludeAfter opts
54+ let before' = if null before then empty else text before
55+ let after' = if null after then empty else text after
56+ titleText <- inlineListToMan opts title
57+ authors' <- mapM (inlineListToMan opts) authors
58+ date' <- inlineListToMan opts date
7359 let (cmdName, rest) = break (== ' ') $ render titleText
7460 let (title', section) = case reverse cmdName of
7561 (')':d:'(':xs) | d `elem` ['0'..'9'] ->
7662 (text (reverse xs), char d)
7763 xs -> (text (reverse xs), doubleQuotes empty)
78− let extras = map (doubleQuotes . text . removeLeadingTrailingSpace) $
79− splitBy '|' rest
80− let head' = (text ".TH") <+> title' <+> section <+>
81− doubleQuotes dateText <+> hsep extras
82− let foot = case length authorsText of
83− 0 -> empty
84− 1 -> text ".SH AUTHOR" $$ (hcat $ intersperse (text ", ") authorsText)
85− _ -> text ".SH AUTHORS" $$ (hcat $ intersperse (text ", ") authorsText)
86− return $ if writerStandalone options
87− then (head', foot)
88− else (empty, empty)
64+ let description = hsep $
65+ map (doubleQuotes . text . removeLeadingTrailingSpace) $
66+ splitBy '|' rest
67+ body <- blockListToMan opts blocks
68+ notes <- liftM stNotes get
69+ notes' <- notesToMan opts (reverse notes)
70+ let main = render $ before' $$ body $$ notes' $$ after'
71+ hasTables <- liftM stHasTables get
72+ let context = writerVariables opts ++
73+ [ ("body", main)
74+ , ("title", render title')
75+ , ("section", render section)
76+ , ("date", render date')
77+ , ("description", render description) ] ++
78+ [ ("has-tables", "yes") | hasTables ] ++
79+ [ ("author", render a) | a <- authors' ]
80+ if writerStandalone opts
81+ then return $ renderTemplate context $ writerTemplate opts
82+ else return main
8983
9084-- | Return man representation of notes.
9185notesToMan :: WriterOptions -> [[Block]] -> State WriterState Doc
@@ -173,7 +167,7 @@ blockToMan opts (Table caption alignments widths headers rows) =
173167 aligncode AlignDefault = "l"
174168 in do
175169 caption' <- inlineListToMan opts caption
176− modify (\(notes, preprocessors) -> (notes, "t":preprocessors))
170+ modify $ \st -> st{ stHasTables = True }
177171 let iwidths = if all (== 0) widths
178172 then repeat ""
179173 else map (printf "w(%0.2fn)" . (70 *)) widths
@@ -332,8 +326,9 @@ inlineToMan opts (Image alternate (source, tit)) = do
332326 linkPart <- inlineToMan opts (Link txt (source, tit))
333327 return $ char '[' <> text "IMAGE: " <> linkPart <> char ']'
334328inlineToMan _ (Note contents) = do
335− modify (\(notes, prep) -> (contents:notes, prep)) -- add to notes in state
336− (notes, _) <- get
329+ -- add to notes in state
330+ modify $ \st -> st{ stNotes = contents : stNotes st }
331+ notes <- liftM stNotes get
337332 let ref = show $ (length notes)
338333 return $ char '[' <> text ref <> char ']'
339334