git.iko.soy/typeable/pandoc
on master help sign in
Added table support to man writer (using the tbl preprocessor).

The writer state now includes a list of "preprocessor" codes. If the document contains a table, "t" (for "tbl") is added to the list. If this list is nonempty, the man page starts with .\" <list> which instructs man to run the file through the appropriate preprocessor before processing with groff. git-svn-id: https://pandoc.googlecode.com/svn/trunk@618 788f1e2b-df1e-0410-8736-df70ead52e1b

commitd5c47c33cabfc3d8b90c6ef0b7a5a90ec1745d08 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateWed 4 Jul 2007, 03:14 (19 years ago) parentcd08eab
1 file changed+36−10
src/Text/Pandoc/Writers/Man.hs+36−10
src/Text/Pandoc/Writers/Man.hs+36−10
@@ -33,18 +33,20 @@ module Text.Pandoc.Writers.Man (
3333 ) where
3434import Text.Pandoc.Definition
3535import Text.Pandoc.Shared
36+import Text.Printf ( printf )
3637import Data.Char ( toUpper )
37−import Data.List ( group, isPrefixOf, drop, find )
38+import Data.List ( group, isPrefixOf, drop, find, nub, intersperse )
3839import Text.PrettyPrint.HughesPJ hiding ( Str )
3940import Control.Monad.State
4041
4142type Notes = [[Block]]
42−type WriterState = Notes
43+type Preprocessors = [String] -- e.g. "t" for tbl
44+type WriterState = (Notes, Preprocessors)
4345
4446-- | Convert Pandoc to Man.
4547writeMan :: WriterOptions -> Pandoc -> String
4648writeMan opts document =
47− render $ evalState (pandocToMan opts document) []
49+ render $ evalState (pandocToMan opts document) ([],[])
4850
4951-- | Return groff man representation of document.
5052pandocToMan :: WriterOptions -> Pandoc -> State WriterState Doc
@@ -55,9 +57,12 @@ pandocToMan opts (Pandoc meta blocks) = do
5557 after' = if null after then empty else text after
5658 (head, foot) <- metaToMan opts meta
5759 body <- blockListToMan opts blocks
58− notes <- get
60+ (notes, preprocessors) <- get
61+ let preamble = if null preprocessors
62+ then empty
63+ else text $ ".\\\" " ++ concat (nub preprocessors)
5964 notes' <- notesToMan opts (reverse notes)
60− return $ head $$ before' $$ body $$ notes' $$ foot $$ after'
65+ return $ preamble $$ head $$ before' $$ body $$ notes' $$ foot $$ after'
6166
6267-- | Insert bibliographic information into Man header and footer.
6368metaToMan :: WriterOptions -- ^ Options, including Man header
@@ -112,7 +117,7 @@ escapeSingleQuote str =
112117
113118-- | Escape special characters for Man.
114119escapeString :: String -> String
115−escapeString = escapeSingleQuote . escapeNbsp . backslashEscape "\".\\"
120+escapeString = escapeSingleQuote . escapeNbsp . backslashEscape "\".@\\"
116121
117122-- | Escape a literal (code) section for Man.
118123escapeCode :: String -> String
@@ -142,8 +147,29 @@ blockToMan opts (CodeBlock str) = return $
142147blockToMan opts (BlockQuote blocks) = do
143148 contents <- blockListToMan opts blocks
144149 return $ text ".RS" $$ contents $$ text ".RE"
145−blockToMan opts (Table caption _ _ headers rows) = blockToMan opts
146− (Para [Str "pandoc: TABLE unsupported in Man writer"])
150+blockToMan opts (Table caption alignments widths headers rows) =
151+ let aligncode AlignLeft = "l"
152+ aligncode AlignRight = "r"
153+ aligncode AlignCenter = "c"
154+ aligncode AlignDefault = "l"
155+ in do
156+ caption' <- inlineListToMan opts caption
157+ modify (\(notes, preprocessors) -> (notes, "t":preprocessors))
158+ let iwidths = map (printf "w(%.2fi)" . (6.5 *)) widths -- 6.5i default width
159+ let coldescriptions = text $ joinWithSep " "
160+ (zipWith (\align width -> aligncode align ++ width)
161+ alignments iwidths) ++ "."
162+ colheadings <- mapM (blockListToMan opts) headers
163+ let makeRow cols = text "T{" $$
164+ (vcat $ intersperse (text "T}@T{") cols) $$
165+ text "T}"
166+ let colheadings' = makeRow colheadings
167+ body <- mapM (\row -> do
168+ cols <- mapM (blockListToMan opts) row
169+ return $ makeRow cols) rows
170+ return $ text ".PP" $$ caption' $$
171+ text ".TS" $$ text "tab(@);" $$ coldescriptions $$
172+ colheadings' $$ text "_" $$ vcat body $$ text ".TE"
147173
148174blockToMan opts (BulletList items) = do
149175 contents <- mapM (bulletListItemToMan opts) items
@@ -264,8 +290,8 @@ inlineToMan opts (Image alternate (source, tit)) = do
264290 linkPart <- inlineToMan opts (Link txt (source, tit))
265291 return $ char '[' <> text "IMAGE: " <> linkPart <> char ']'
266292inlineToMan opts (Note contents) = do
267− modify (\notes -> contents:notes) -- add to notes in state
268− notes <- get
293+ modify (\(notes, prep) -> (contents:notes, prep)) -- add to notes in state
294+ (notes, _) <- get
269295 let ref = show $ (length notes)
270296 return $ text "[" <> text ref <> char ']'
271297