@@ -17,9 +17,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20− Module : Text.Pandoc.Writers.Man
20+ Module : Text.Pandoc.Writers.Man
21 Copyright : Copyright (C) 2007-2010 John MacFarlane
21 Copyright : Copyright (C) 2007-2010 John MacFarlane
22− License : GNU GPL, version 2 or above
22+ License : GNU GPL, version 2 or above
24 Maintainer : John MacFarlane <jgm@berkeley.edu>
24 Maintainer : John MacFarlane <jgm@berkeley.edu>
25 Stability : alpha
25 Stability : alpha
@@ -44,21 +44,21 @@ data WriterState = WriterState { stNotes :: Notes
45-- | Convert Pandoc to Man.
45-- | Convert Pandoc to Man.
46writeMan :: WriterOptions -> Pandoc -> String
46writeMan :: WriterOptions -> Pandoc -> String
47−writeMan opts document = evalState (pandocToMan opts document) (WriterState [] False)
47+writeMan opts document = evalState (pandocToMan opts document) (WriterState [] False)
49-- | Return groff man representation of document.
49-- | Return groff man representation of document.
50pandocToMan :: WriterOptions -> Pandoc -> State WriterState String
50pandocToMan :: WriterOptions -> Pandoc -> State WriterState String
51pandocToMan opts (Pandoc (Meta title authors date) blocks) = do
51pandocToMan opts (Pandoc (Meta title authors date) blocks) = do
52 titleText <- inlineListToMan opts title
52 titleText <- inlineListToMan opts title
53 authors' <- mapM (inlineListToMan opts) authors
53 authors' <- mapM (inlineListToMan opts) authors
54− date' <- inlineListToMan opts date
54+ date' <- inlineListToMan opts date
55 let colwidth = if writerWrapText opts
55 let colwidth = if writerWrapText opts
56 then Just $ writerColumns opts
56 then Just $ writerColumns opts
57 else Nothing
57 else Nothing
58 let render' = render colwidth
58 let render' = render colwidth
59 let (cmdName, rest) = break (== ' ') $ render' titleText
59 let (cmdName, rest) = break (== ' ') $ render' titleText
60 let (title', section) = case reverse cmdName of
60 let (title', section) = case reverse cmdName of
61− (')':d:'(':xs) | d `elem` ['0'..'9'] ->
61+ (')':d:'(':xs) | d `elem` ['0'..'9'] ->
62 (text (reverse xs), char d)
62 (text (reverse xs), char d)
63 xs -> (text (reverse xs), doubleQuotes empty)
63 xs -> (text (reverse xs), doubleQuotes empty)
64 let description = hsep $
64 let description = hsep $
@@ -86,7 +86,7 @@ notesToMan :: WriterOptions -> [[Block]] -> State WriterState Doc
86notesToMan opts notes =
86notesToMan opts notes =
87 if null notes
87 if null notes
88 then return empty
88 then return empty
89− else mapM (\(num, note) -> noteToMan opts num note) (zip [1..] notes) >>=
89+ else mapM (\(num, note) -> noteToMan opts num note) (zip [1..] notes) >>=
90 return . (text ".SH NOTES" $$) . vcat
90 return . (text ".SH NOTES" $$) . vcat
92-- | Return man representation of a note.
92-- | Return man representation of a note.
@@ -94,7 +94,7 @@ noteToMan :: WriterOptions -> Int -> [Block] -> State WriterState Doc
94noteToMan opts num note = do
94noteToMan opts num note = do
95 contents <- blockListToMan opts note
95 contents <- blockListToMan opts note
96 let marker = cr <> text ".SS " <> brackets (text (show num))
96 let marker = cr <> text ".SS " <> brackets (text (show num))
97− return $ marker $$ contents
97+ return $ marker $$ contents
99-- | Association list of characters to escape.
99-- | Association list of characters to escape.
100manEscapes :: [(Char, String)]
100manEscapes :: [(Char, String)]
@@ -113,7 +113,7 @@ escapeString = escapeStringUsing manEscapes
113-- | Escape a literal (code) section for Man.
113-- | Escape a literal (code) section for Man.
114escapeCode :: String -> String
114escapeCode :: String -> String
115escapeCode = concat . intersperse "\n" . map escapeLine . lines where
115escapeCode = concat . intersperse "\n" . map escapeLine . lines where
116− escapeLine codeline =
116+ escapeLine codeline =
117 case escapeStringUsing (manEscapes ++ backslashEscapes "\t ") codeline of
117 case escapeStringUsing (manEscapes ++ backslashEscapes "\t ") codeline of
118 a@('.':_) -> "\\&" ++ a
118 a@('.':_) -> "\\&" ++ a
@@ -150,14 +150,14 @@ splitSentences xs =
150-- | Convert Pandoc block element to man.
150-- | Convert Pandoc block element to man.
151blockToMan :: WriterOptions -- ^ Options
151blockToMan :: WriterOptions -- ^ Options
152 -> Block -- ^ Block element
152 -> Block -- ^ Block element
153− -> State WriterState Doc
153+ -> State WriterState Doc
154blockToMan _ Null = return empty
154blockToMan _ Null = return empty
155−blockToMan opts (Plain inlines) =
155+blockToMan opts (Plain inlines) =
156 liftM vcat $ mapM (inlineListToMan opts) $ splitSentences inlines
156 liftM vcat $ mapM (inlineListToMan opts) $ splitSentences inlines
157blockToMan opts (Para inlines) = do
157blockToMan opts (Para inlines) = do
158 contents <- liftM vcat $ mapM (inlineListToMan opts) $
158 contents <- liftM vcat $ mapM (inlineListToMan opts) $
159 splitSentences inlines
159 splitSentences inlines
160− return $ text ".PP" $$ contents
160+ return $ text ".PP" $$ contents
161blockToMan _ (RawBlock "man" str) = return $ text str
161blockToMan _ (RawBlock "man" str) = return $ text str
162blockToMan _ (RawBlock _ _) = return empty
162blockToMan _ (RawBlock _ _) = return empty
163blockToMan _ HorizontalRule = return $ text ".PP" $$ text " * * * * *"
163blockToMan _ HorizontalRule = return $ text ".PP" $$ text " * * * * *"
@@ -166,7 +166,7 @@ blockToMan opts (Header level inlines) = do
166 let heading = case level of
166 let heading = case level of
167 1 -> ".SH "
167 1 -> ".SH "
168 _ -> ".SS "
168 _ -> ".SS "
169− return $ text heading <> contents
169+ return $ text heading <> contents
170blockToMan _ (CodeBlock _ str) = return $
170blockToMan _ (CodeBlock _ str) = return $
171 text ".IP" $$
171 text ".IP" $$
172 text ".nf" $$
172 text ".nf" $$
@@ -174,10 +174,10 @@ blockToMan _ (CodeBlock _ str) = return $
174 text (escapeCode str) $$
174 text (escapeCode str) $$
175 text "\\f[]" $$
175 text "\\f[]" $$
176 text ".fi"
176 text ".fi"
177−blockToMan opts (BlockQuote blocks) = do
177+blockToMan opts (BlockQuote blocks) = do
178 contents <- blockListToMan opts blocks
178 contents <- blockListToMan opts blocks
179 return $ text ".RS" $$ contents $$ text ".RE"
179 return $ text ".RS" $$ contents $$ text ".RE"
180−blockToMan opts (Table caption alignments widths headers rows) =
180+blockToMan opts (Table caption alignments widths headers rows) =
181 let aligncode AlignLeft = "l"
181 let aligncode AlignLeft = "l"
182 aligncode AlignRight = "r"
182 aligncode AlignRight = "r"
183 aligncode AlignCenter = "c"
183 aligncode AlignCenter = "c"
@@ -190,53 +190,53 @@ blockToMan opts (Table caption alignments widths headers rows) =
190 else map (printf "w(%0.2fn)" . (70 *)) widths
190 else map (printf "w(%0.2fn)" . (70 *)) widths
191 -- 78n default width - 8n indent = 70n
191 -- 78n default width - 8n indent = 70n
192 let coldescriptions = text $ intercalate " "
192 let coldescriptions = text $ intercalate " "
193− (zipWith (\align width -> aligncode align ++ width)
193+ (zipWith (\align width -> aligncode align ++ width)
194 alignments iwidths) ++ "."
194 alignments iwidths) ++ "."
195 colheadings <- mapM (blockListToMan opts) headers
195 colheadings <- mapM (blockListToMan opts) headers
196− let makeRow cols = text "T{" $$
196+ let makeRow cols = text "T{" $$
197− (vcat $ intersperse (text "T}@T{") cols) $$
197+ (vcat $ intersperse (text "T}@T{") cols) $$
198 text "T}"
198 text "T}"
199 let colheadings' = if all null headers
199 let colheadings' = if all null headers
200 then empty
200 then empty
201 else makeRow colheadings $$ char '_'
201 else makeRow colheadings $$ char '_'
202− body <- mapM (\row -> do
202+ body <- mapM (\row -> do
203 cols <- mapM (blockListToMan opts) row
203 cols <- mapM (blockListToMan opts) row
204 return $ makeRow cols) rows
204 return $ makeRow cols) rows
205− return $ text ".PP" $$ caption' $$
205+ return $ text ".PP" $$ caption' $$
206− text ".TS" $$ text "tab(@);" $$ coldescriptions $$
206+ text ".TS" $$ text "tab(@);" $$ coldescriptions $$
207 colheadings' $$ vcat body $$ text ".TE"
207 colheadings' $$ vcat body $$ text ".TE"
209blockToMan opts (BulletList items) = do
209blockToMan opts (BulletList items) = do
210 contents <- mapM (bulletListItemToMan opts) items
210 contents <- mapM (bulletListItemToMan opts) items
211− return (vcat contents)
211+ return (vcat contents)
212blockToMan opts (OrderedList attribs items) = do
212blockToMan opts (OrderedList attribs items) = do
213− let markers = take (length items) $ orderedListMarkers attribs
213+ let markers = take (length items) $ orderedListMarkers attribs
214 let indent = 1 + (maximum $ map length markers)
214 let indent = 1 + (maximum $ map length markers)
215 contents <- mapM (\(num, item) -> orderedListItemToMan opts num indent item) $
215 contents <- mapM (\(num, item) -> orderedListItemToMan opts num indent item) $
216− zip markers items
216+ zip markers items
217 return (vcat contents)
217 return (vcat contents)
218−blockToMan opts (DefinitionList items) = do
218+blockToMan opts (DefinitionList items) = do
219 contents <- mapM (definitionListItemToMan opts) items
219 contents <- mapM (definitionListItemToMan opts) items
220 return (vcat contents)
220 return (vcat contents)
222-- | Convert bullet list item (list of blocks) to man.
222-- | Convert bullet list item (list of blocks) to man.
223bulletListItemToMan :: WriterOptions -> [Block] -> State WriterState Doc
223bulletListItemToMan :: WriterOptions -> [Block] -> State WriterState Doc
224bulletListItemToMan _ [] = return empty
224bulletListItemToMan _ [] = return empty
225−bulletListItemToMan opts ((Para first):rest) =
225+bulletListItemToMan opts ((Para first):rest) =
226 bulletListItemToMan opts ((Plain first):rest)
226 bulletListItemToMan opts ((Plain first):rest)
227bulletListItemToMan opts ((Plain first):rest) = do
227bulletListItemToMan opts ((Plain first):rest) = do
228− first' <- blockToMan opts (Plain first)
228+ first' <- blockToMan opts (Plain first)
229 rest' <- blockListToMan opts rest
229 rest' <- blockListToMan opts rest
230 let first'' = text ".IP \\[bu] 2" $$ first'
230 let first'' = text ".IP \\[bu] 2" $$ first'
231 let rest'' = if null rest
231 let rest'' = if null rest
232 then empty
232 then empty
233 else text ".RS 2" $$ rest' $$ text ".RE"
233 else text ".RS 2" $$ rest' $$ text ".RE"
234− return (first'' $$ rest'')
234+ return (first'' $$ rest'')
235bulletListItemToMan opts (first:rest) = do
235bulletListItemToMan opts (first:rest) = do
236 first' <- blockToMan opts first
236 first' <- blockToMan opts first
237 rest' <- blockListToMan opts rest
237 rest' <- blockListToMan opts rest
238 return $ text "\\[bu] .RS 2" $$ first' $$ rest' $$ text ".RE"
238 return $ text "\\[bu] .RS 2" $$ first' $$ rest' $$ text ".RE"
240-- | Convert ordered list item (a list of blocks) to man.
240-- | Convert ordered list item (a list of blocks) to man.
241orderedListItemToMan :: WriterOptions -- ^ options
241orderedListItemToMan :: WriterOptions -- ^ options
242 -> String -- ^ order marker for list item
242 -> String -- ^ order marker for list item
@@ -244,7 +244,7 @@ orderedListItemToMan :: WriterOptions -- ^ options
244 -> [Block] -- ^ list item (list of blocks)
244 -> [Block] -- ^ list item (list of blocks)
245 -> State WriterState Doc
245 -> State WriterState Doc
246orderedListItemToMan _ _ _ [] = return empty
246orderedListItemToMan _ _ _ [] = return empty
247−orderedListItemToMan opts num indent ((Para first):rest) =
247+orderedListItemToMan opts num indent ((Para first):rest) =
248 orderedListItemToMan opts num indent ((Plain first):rest)
248 orderedListItemToMan opts num indent ((Plain first):rest)
249orderedListItemToMan opts num indent (first:rest) = do
249orderedListItemToMan opts num indent (first:rest) = do
250 first' <- blockToMan opts first
250 first' <- blockToMan opts first
@@ -254,17 +254,17 @@ orderedListItemToMan opts num indent (first:rest) = do
254 let rest'' = if null rest
254 let rest'' = if null rest
255 then empty
255 then empty
256 else text ".RS 4" $$ rest' $$ text ".RE"
256 else text ".RS 4" $$ rest' $$ text ".RE"
257− return $ first'' $$ rest''
257+ return $ first'' $$ rest''
259-- | Convert definition list item (label, list of blocks) to man.
259-- | Convert definition list item (label, list of blocks) to man.
260definitionListItemToMan :: WriterOptions
260definitionListItemToMan :: WriterOptions
261− -> ([Inline],[[Block]])
261+ -> ([Inline],[[Block]])
262 -> State WriterState Doc
262 -> State WriterState Doc
263definitionListItemToMan opts (label, defs) = do
263definitionListItemToMan opts (label, defs) = do
264 labelText <- inlineListToMan opts label
264 labelText <- inlineListToMan opts label
265− contents <- if null defs
265+ contents <- if null defs
266 then return empty
266 then return empty
267− else liftM vcat $ forM defs $ \blocks -> do
267+ else liftM vcat $ forM defs $ \blocks -> do
268 let (first, rest) = case blocks of
268 let (first, rest) = case blocks of
269 ((Para x):y) -> (Plain x,y)
269 ((Para x):y) -> (Plain x,y)
270 (x:y) -> (x,y)
270 (x:y) -> (x,y)
@@ -278,7 +278,7 @@ definitionListItemToMan opts (label, defs) = do
278-- | Convert list of Pandoc block elements to man.
278-- | Convert list of Pandoc block elements to man.
279blockListToMan :: WriterOptions -- ^ Options
279blockListToMan :: WriterOptions -- ^ Options
280 -> [Block] -- ^ List of block elements
280 -> [Block] -- ^ List of block elements
281− -> State WriterState Doc
281+ -> State WriterState Doc
282blockListToMan opts blocks =
282blockListToMan opts blocks =
283 mapM (blockToMan opts) blocks >>= (return . vcat)
283 mapM (blockToMan opts) blocks >>= (return . vcat)
@@ -292,7 +292,7 @@ inlineListToMan opts lst = mapM (inlineToMan opts) lst >>= (return . hcat)
293-- | Convert Pandoc inline element to man.
293-- | Convert Pandoc inline element to man.
294inlineToMan :: WriterOptions -> Inline -> State WriterState Doc
294inlineToMan :: WriterOptions -> Inline -> State WriterState Doc
295−inlineToMan opts (Emph lst) = do
295+inlineToMan opts (Emph lst) = do
296 contents <- inlineListToMan opts lst
296 contents <- inlineListToMan opts lst
297 return $ text "\\f[I]" <> contents <> text "\\f[]"
297 return $ text "\\f[I]" <> contents <> text "\\f[]"
298inlineToMan opts (Strong lst) = do
298inlineToMan opts (Strong lst) = do
@@ -333,16 +333,16 @@ inlineToMan opts (Link txt (src, _)) = do
333 let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
333 let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
334 return $ case txt of
334 return $ case txt of
335 [Code _ s]
335 [Code _ s]
336− | s == srcSuffix -> char '<' <> text srcSuffix <> char '>'
336+ | s == srcSuffix -> char '<' <> text srcSuffix <> char '>'
337 _ -> linktext <> text " (" <> text src <> char ')'
337 _ -> linktext <> text " (" <> text src <> char ')'
338inlineToMan opts (Image alternate (source, tit)) = do
338inlineToMan opts (Image alternate (source, tit)) = do
339− let txt = if (null alternate) || (alternate == [Str ""]) ||
339+ let txt = if (null alternate) || (alternate == [Str ""]) ||
340 (alternate == [Str source]) -- to prevent autolinks
340 (alternate == [Str source]) -- to prevent autolinks
341 then [Str "image"]
341 then [Str "image"]
342 else alternate
342 else alternate
343− linkPart <- inlineToMan opts (Link txt (source, tit))
343+ linkPart <- inlineToMan opts (Link txt (source, tit))
344 return $ char '[' <> text "IMAGE: " <> linkPart <> char ']'
344 return $ char '[' <> text "IMAGE: " <> linkPart <> char ']'
345−inlineToMan _ (Note contents) = do
345+inlineToMan _ (Note contents) = do
346 -- add to notes in state
346 -- add to notes in state
347 modify $ \st -> st{ stNotes = contents : stNotes st }
347 modify $ \st -> st{ stNotes = contents : stNotes st }
348 notes <- liftM stNotes get
348 notes <- liftM stNotes get