2007-11-03 23:27:58 +00:00
|
|
|
|
{-
|
2015-04-26 10:18:29 -07:00
|
|
|
|
Copyright (C) 2007-2015 John MacFarlane <jgm@berkeley.edu>
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
{- |
|
2012-07-26 22:32:53 -07:00
|
|
|
|
Module : Text.Pandoc.Writers.Man
|
2015-04-26 10:18:29 -07:00
|
|
|
|
Copyright : Copyright (C) 2007-2015 John MacFarlane
|
2012-07-26 22:32:53 -07:00
|
|
|
|
License : GNU GPL, version 2 or above
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
Maintainer : John MacFarlane <jgm@berkeley.edu>
|
|
|
|
|
Stability : alpha
|
|
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
|
|
Conversion of 'Pandoc' documents to groff man page format.
|
|
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
module Text.Pandoc.Writers.Man ( writeMan) where
|
|
|
|
|
import Text.Pandoc.Definition
|
2009-12-31 01:17:27 +00:00
|
|
|
|
import Text.Pandoc.Templates
|
2007-12-02 00:36:32 +00:00
|
|
|
|
import Text.Pandoc.Shared
|
2013-07-01 20:47:26 -07:00
|
|
|
|
import Text.Pandoc.Writers.Shared
|
2012-07-26 22:59:56 -07:00
|
|
|
|
import Text.Pandoc.Options
|
2010-04-26 23:04:29 -07:00
|
|
|
|
import Text.Pandoc.Readers.TeXMath
|
2007-11-03 23:27:58 +00:00
|
|
|
|
import Text.Printf ( printf )
|
2014-08-03 14:44:39 +04:00
|
|
|
|
import Data.List ( stripPrefix, intersperse, intercalate )
|
|
|
|
|
import Data.Maybe (fromMaybe)
|
2010-12-22 00:21:56 -08:00
|
|
|
|
import Text.Pandoc.Pretty
|
2013-05-10 22:53:35 -07:00
|
|
|
|
import Text.Pandoc.Builder (deleteMeta)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
import Control.Monad.State
|
|
|
|
|
|
|
|
|
|
type Notes = [[Block]]
|
2009-12-31 01:17:27 +00:00
|
|
|
|
data WriterState = WriterState { stNotes :: Notes
|
|
|
|
|
, stHasTables :: Bool }
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert Pandoc to Man.
|
|
|
|
|
writeMan :: WriterOptions -> Pandoc -> String
|
2012-07-26 22:32:53 -07:00
|
|
|
|
writeMan opts document = evalState (pandocToMan opts document) (WriterState [] False)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Return groff man representation of document.
|
2009-12-31 01:17:27 +00:00
|
|
|
|
pandocToMan :: WriterOptions -> Pandoc -> State WriterState String
|
2013-05-10 22:53:35 -07:00
|
|
|
|
pandocToMan opts (Pandoc meta blocks) = do
|
2015-12-11 15:58:11 -08:00
|
|
|
|
let colwidth = if writerWrapText opts == WrapAuto
|
2010-12-22 00:21:56 -08:00
|
|
|
|
then Just $ writerColumns opts
|
|
|
|
|
else Nothing
|
|
|
|
|
let render' = render colwidth
|
2013-05-10 22:53:35 -07:00
|
|
|
|
titleText <- inlineListToMan opts $ docTitle meta
|
2013-06-27 19:32:28 -07:00
|
|
|
|
let title' = render' titleText
|
|
|
|
|
let setFieldsFromTitle =
|
|
|
|
|
case break (== ' ') title' of
|
2016-08-27 22:00:42 +02:00
|
|
|
|
(cmdName, rest) -> case break (=='(') cmdName of
|
|
|
|
|
(xs, '(':ys) | not (null ys) &&
|
|
|
|
|
last ys == ')' ->
|
|
|
|
|
defField "title" xs .
|
|
|
|
|
defField "section" (init ys) .
|
2013-06-27 19:32:28 -07:00
|
|
|
|
case splitBy (=='|') rest of
|
|
|
|
|
(ft:hds) ->
|
2013-06-27 22:42:55 -07:00
|
|
|
|
defField "footer" (trim ft) .
|
|
|
|
|
defField "header"
|
2013-06-27 19:32:28 -07:00
|
|
|
|
(trim $ concat hds)
|
|
|
|
|
[] -> id
|
2013-06-27 22:42:55 -07:00
|
|
|
|
_ -> defField "title" title'
|
2013-07-01 20:47:26 -07:00
|
|
|
|
metadata <- metaToJSON opts
|
2013-05-10 22:53:35 -07:00
|
|
|
|
(fmap (render colwidth) . blockListToMan opts)
|
|
|
|
|
(fmap (render colwidth) . inlineListToMan opts)
|
|
|
|
|
$ deleteMeta "title" meta
|
2009-12-31 01:17:27 +00:00
|
|
|
|
body <- blockListToMan opts blocks
|
|
|
|
|
notes <- liftM stNotes get
|
|
|
|
|
notes' <- notesToMan opts (reverse notes)
|
2010-12-22 00:21:56 -08:00
|
|
|
|
let main = render' $ body $$ notes' $$ text ""
|
2009-12-31 01:17:27 +00:00
|
|
|
|
hasTables <- liftM stHasTables get
|
2013-06-27 22:42:55 -07:00
|
|
|
|
let context = defField "body" main
|
2013-06-27 19:32:28 -07:00
|
|
|
|
$ setFieldsFromTitle
|
2013-06-27 22:42:55 -07:00
|
|
|
|
$ defField "has-tables" hasTables
|
2015-09-19 19:12:35 +08:00
|
|
|
|
$ defField "hyphenate" True
|
2015-09-25 04:05:58 +08:00
|
|
|
|
$ defField "pandoc-version" pandocVersion
|
2013-06-29 22:14:01 -07:00
|
|
|
|
$ metadata
|
2009-12-31 01:17:27 +00:00
|
|
|
|
if writerStandalone opts
|
2013-05-10 22:53:35 -07:00
|
|
|
|
then return $ renderTemplate' (writerTemplate opts) context
|
2009-12-31 01:17:27 +00:00
|
|
|
|
else return main
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Return man representation of notes.
|
|
|
|
|
notesToMan :: WriterOptions -> [[Block]] -> State WriterState Doc
|
|
|
|
|
notesToMan opts notes =
|
|
|
|
|
if null notes
|
|
|
|
|
then return empty
|
2012-07-26 22:32:53 -07:00
|
|
|
|
else mapM (\(num, note) -> noteToMan opts num note) (zip [1..] notes) >>=
|
2007-11-03 23:27:58 +00:00
|
|
|
|
return . (text ".SH NOTES" $$) . vcat
|
|
|
|
|
|
|
|
|
|
-- | Return man representation of a note.
|
|
|
|
|
noteToMan :: WriterOptions -> Int -> [Block] -> State WriterState Doc
|
|
|
|
|
noteToMan opts num note = do
|
|
|
|
|
contents <- blockListToMan opts note
|
2010-12-22 00:21:56 -08:00
|
|
|
|
let marker = cr <> text ".SS " <> brackets (text (show num))
|
2012-07-26 22:32:53 -07:00
|
|
|
|
return $ marker $$ contents
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Association list of characters to escape.
|
|
|
|
|
manEscapes :: [(Char, String)]
|
2011-12-27 15:45:34 -08:00
|
|
|
|
manEscapes = [ ('\160', "\\ ")
|
|
|
|
|
, ('\'', "\\[aq]")
|
|
|
|
|
, ('’', "'")
|
|
|
|
|
, ('\x2014', "\\[em]")
|
|
|
|
|
, ('\x2013', "\\[en]")
|
|
|
|
|
, ('\x2026', "\\&...")
|
2012-08-18 10:27:07 -07:00
|
|
|
|
] ++ backslashEscapes "-@\\"
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Escape special characters for Man.
|
|
|
|
|
escapeString :: String -> String
|
|
|
|
|
escapeString = escapeStringUsing manEscapes
|
|
|
|
|
|
|
|
|
|
-- | Escape a literal (code) section for Man.
|
|
|
|
|
escapeCode :: String -> String
|
2012-03-07 11:35:18 -05:00
|
|
|
|
escapeCode = concat . intersperse "\n" . map escapeLine . lines where
|
2012-07-26 22:32:53 -07:00
|
|
|
|
escapeLine codeline =
|
2012-03-07 11:35:18 -05:00
|
|
|
|
case escapeStringUsing (manEscapes ++ backslashEscapes "\t ") codeline of
|
|
|
|
|
a@('.':_) -> "\\&" ++ a
|
|
|
|
|
b -> b
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
2009-06-06 21:27:46 +00:00
|
|
|
|
-- We split inline lists into sentences, and print one sentence per
|
|
|
|
|
-- line. groff/troff treats the line-ending period differently.
|
|
|
|
|
-- See http://code.google.com/p/pandoc/issues/detail?id=148.
|
|
|
|
|
|
|
|
|
|
-- | Returns the first sentence in a list of inlines, and the rest.
|
|
|
|
|
breakSentence :: [Inline] -> ([Inline], [Inline])
|
|
|
|
|
breakSentence [] = ([],[])
|
|
|
|
|
breakSentence xs =
|
2012-03-09 13:39:19 -08:00
|
|
|
|
let isSentenceEndInline (Str ys@(_:_)) | last ys == '.' = True
|
|
|
|
|
isSentenceEndInline (Str ys@(_:_)) | last ys == '?' = True
|
2012-03-07 12:27:46 -05:00
|
|
|
|
isSentenceEndInline (LineBreak) = True
|
2009-06-06 21:27:46 +00:00
|
|
|
|
isSentenceEndInline _ = False
|
|
|
|
|
(as, bs) = break isSentenceEndInline xs
|
|
|
|
|
in case bs of
|
|
|
|
|
[] -> (as, [])
|
|
|
|
|
[c] -> (as ++ [c], [])
|
|
|
|
|
(c:Space:cs) -> (as ++ [c], cs)
|
2015-12-11 15:58:11 -08:00
|
|
|
|
(c:SoftBreak:cs) -> (as ++ [c], cs)
|
2012-03-09 13:39:19 -08:00
|
|
|
|
(Str ".":Str (')':ys):cs) -> (as ++ [Str ".", Str (')':ys)], cs)
|
|
|
|
|
(x@(Str ('.':')':_)):cs) -> (as ++ [x], cs)
|
|
|
|
|
(LineBreak:x@(Str ('.':_)):cs) -> (as ++[LineBreak], x:cs)
|
2009-06-06 21:27:46 +00:00
|
|
|
|
(c:cs) -> (as ++ [c] ++ ds, es)
|
|
|
|
|
where (ds, es) = breakSentence cs
|
|
|
|
|
|
|
|
|
|
-- | Split a list of inlines into sentences.
|
|
|
|
|
splitSentences :: [Inline] -> [[Inline]]
|
|
|
|
|
splitSentences xs =
|
|
|
|
|
let (sent, rest) = breakSentence xs
|
|
|
|
|
in if null rest then [sent] else sent : splitSentences rest
|
|
|
|
|
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- | Convert Pandoc block element to man.
|
|
|
|
|
blockToMan :: WriterOptions -- ^ Options
|
|
|
|
|
-> Block -- ^ Block element
|
2012-07-26 22:32:53 -07:00
|
|
|
|
-> State WriterState Doc
|
2008-07-13 23:39:52 +00:00
|
|
|
|
blockToMan _ Null = return empty
|
2013-08-08 23:14:12 -07:00
|
|
|
|
blockToMan opts (Div _ bs) = blockListToMan opts bs
|
2012-07-26 22:32:53 -07:00
|
|
|
|
blockToMan opts (Plain inlines) =
|
2010-12-22 00:21:56 -08:00
|
|
|
|
liftM vcat $ mapM (inlineListToMan opts) $ splitSentences inlines
|
2007-11-03 23:27:58 +00:00
|
|
|
|
blockToMan opts (Para inlines) = do
|
2010-12-22 00:21:56 -08:00
|
|
|
|
contents <- liftM vcat $ mapM (inlineListToMan opts) $
|
2009-06-06 21:27:46 +00:00
|
|
|
|
splitSentences inlines
|
2012-07-26 22:32:53 -07:00
|
|
|
|
return $ text ".PP" $$ contents
|
2016-10-13 08:46:44 +02:00
|
|
|
|
blockToMan opts (LineBlock lns) =
|
|
|
|
|
blockToMan opts $ linesToPara lns
|
2013-08-10 17:23:51 -07:00
|
|
|
|
blockToMan _ (RawBlock f str)
|
|
|
|
|
| f == Format "man" = return $ text str
|
|
|
|
|
| otherwise = return empty
|
2010-12-22 00:21:56 -08:00
|
|
|
|
blockToMan _ HorizontalRule = return $ text ".PP" $$ text " * * * * *"
|
2012-10-29 22:45:52 -07:00
|
|
|
|
blockToMan opts (Header level _ inlines) = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
contents <- inlineListToMan opts inlines
|
|
|
|
|
let heading = case level of
|
|
|
|
|
1 -> ".SH "
|
|
|
|
|
_ -> ".SS "
|
2012-07-26 22:32:53 -07:00
|
|
|
|
return $ text heading <> contents
|
2008-07-13 23:39:52 +00:00
|
|
|
|
blockToMan _ (CodeBlock _ str) = return $
|
2010-07-14 10:52:06 -07:00
|
|
|
|
text ".IP" $$
|
|
|
|
|
text ".nf" $$
|
|
|
|
|
text "\\f[C]" $$
|
|
|
|
|
text (escapeCode str) $$
|
|
|
|
|
text "\\f[]" $$
|
|
|
|
|
text ".fi"
|
2012-07-26 22:32:53 -07:00
|
|
|
|
blockToMan opts (BlockQuote blocks) = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
contents <- blockListToMan opts blocks
|
|
|
|
|
return $ text ".RS" $$ contents $$ text ".RE"
|
2012-07-26 22:32:53 -07:00
|
|
|
|
blockToMan opts (Table caption alignments widths headers rows) =
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let aligncode AlignLeft = "l"
|
|
|
|
|
aligncode AlignRight = "r"
|
|
|
|
|
aligncode AlignCenter = "c"
|
|
|
|
|
aligncode AlignDefault = "l"
|
|
|
|
|
in do
|
|
|
|
|
caption' <- inlineListToMan opts caption
|
2009-12-31 01:17:27 +00:00
|
|
|
|
modify $ \st -> st{ stHasTables = True }
|
2009-11-28 03:22:33 +00:00
|
|
|
|
let iwidths = if all (== 0) widths
|
|
|
|
|
then repeat ""
|
2013-04-09 19:56:43 -07:00
|
|
|
|
else map (printf "w(%0.1fn)" . (70 *)) widths
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- 78n default width - 8n indent = 70n
|
2008-09-08 06:36:28 +00:00
|
|
|
|
let coldescriptions = text $ intercalate " "
|
2012-07-26 22:32:53 -07:00
|
|
|
|
(zipWith (\align width -> aligncode align ++ width)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
alignments iwidths) ++ "."
|
|
|
|
|
colheadings <- mapM (blockListToMan opts) headers
|
2012-07-26 22:32:53 -07:00
|
|
|
|
let makeRow cols = text "T{" $$
|
|
|
|
|
(vcat $ intersperse (text "T}@T{") cols) $$
|
2007-11-03 23:27:58 +00:00
|
|
|
|
text "T}"
|
2010-03-13 03:19:13 +00:00
|
|
|
|
let colheadings' = if all null headers
|
|
|
|
|
then empty
|
|
|
|
|
else makeRow colheadings $$ char '_'
|
2012-07-26 22:32:53 -07:00
|
|
|
|
body <- mapM (\row -> do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
cols <- mapM (blockListToMan opts) row
|
|
|
|
|
return $ makeRow cols) rows
|
2012-07-26 22:32:53 -07:00
|
|
|
|
return $ text ".PP" $$ caption' $$
|
|
|
|
|
text ".TS" $$ text "tab(@);" $$ coldescriptions $$
|
2010-03-13 03:19:13 +00:00
|
|
|
|
colheadings' $$ vcat body $$ text ".TE"
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
blockToMan opts (BulletList items) = do
|
|
|
|
|
contents <- mapM (bulletListItemToMan opts) items
|
2012-07-26 22:32:53 -07:00
|
|
|
|
return (vcat contents)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
blockToMan opts (OrderedList attribs items) = do
|
2012-07-26 22:32:53 -07:00
|
|
|
|
let markers = take (length items) $ orderedListMarkers attribs
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let indent = 1 + (maximum $ map length markers)
|
|
|
|
|
contents <- mapM (\(num, item) -> orderedListItemToMan opts num indent item) $
|
2012-07-26 22:32:53 -07:00
|
|
|
|
zip markers items
|
2007-11-03 23:27:58 +00:00
|
|
|
|
return (vcat contents)
|
2012-07-26 22:32:53 -07:00
|
|
|
|
blockToMan opts (DefinitionList items) = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
contents <- mapM (definitionListItemToMan opts) items
|
|
|
|
|
return (vcat contents)
|
|
|
|
|
|
|
|
|
|
-- | Convert bullet list item (list of blocks) to man.
|
|
|
|
|
bulletListItemToMan :: WriterOptions -> [Block] -> State WriterState Doc
|
2008-07-13 23:39:52 +00:00
|
|
|
|
bulletListItemToMan _ [] = return empty
|
2012-07-26 22:32:53 -07:00
|
|
|
|
bulletListItemToMan opts ((Para first):rest) =
|
2007-11-03 23:27:58 +00:00
|
|
|
|
bulletListItemToMan opts ((Plain first):rest)
|
|
|
|
|
bulletListItemToMan opts ((Plain first):rest) = do
|
2012-07-26 22:32:53 -07:00
|
|
|
|
first' <- blockToMan opts (Plain first)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
rest' <- blockListToMan opts rest
|
|
|
|
|
let first'' = text ".IP \\[bu] 2" $$ first'
|
|
|
|
|
let rest'' = if null rest
|
|
|
|
|
then empty
|
|
|
|
|
else text ".RS 2" $$ rest' $$ text ".RE"
|
2012-07-26 22:32:53 -07:00
|
|
|
|
return (first'' $$ rest'')
|
2007-11-03 23:27:58 +00:00
|
|
|
|
bulletListItemToMan opts (first:rest) = do
|
|
|
|
|
first' <- blockToMan opts first
|
|
|
|
|
rest' <- blockListToMan opts rest
|
|
|
|
|
return $ text "\\[bu] .RS 2" $$ first' $$ rest' $$ text ".RE"
|
2012-07-26 22:32:53 -07:00
|
|
|
|
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- | Convert ordered list item (a list of blocks) to man.
|
|
|
|
|
orderedListItemToMan :: WriterOptions -- ^ options
|
|
|
|
|
-> String -- ^ order marker for list item
|
|
|
|
|
-> Int -- ^ number of spaces to indent
|
|
|
|
|
-> [Block] -- ^ list item (list of blocks)
|
|
|
|
|
-> State WriterState Doc
|
|
|
|
|
orderedListItemToMan _ _ _ [] = return empty
|
2012-07-26 22:32:53 -07:00
|
|
|
|
orderedListItemToMan opts num indent ((Para first):rest) =
|
2007-11-03 23:27:58 +00:00
|
|
|
|
orderedListItemToMan opts num indent ((Plain first):rest)
|
|
|
|
|
orderedListItemToMan opts num indent (first:rest) = do
|
|
|
|
|
first' <- blockToMan opts first
|
|
|
|
|
rest' <- blockListToMan opts rest
|
|
|
|
|
let num' = printf ("%" ++ show (indent - 1) ++ "s") num
|
|
|
|
|
let first'' = text (".IP \"" ++ num' ++ "\" " ++ show indent) $$ first'
|
|
|
|
|
let rest'' = if null rest
|
|
|
|
|
then empty
|
|
|
|
|
else text ".RS 4" $$ rest' $$ text ".RE"
|
2012-07-26 22:32:53 -07:00
|
|
|
|
return $ first'' $$ rest''
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert definition list item (label, list of blocks) to man.
|
|
|
|
|
definitionListItemToMan :: WriterOptions
|
2012-07-26 22:32:53 -07:00
|
|
|
|
-> ([Inline],[[Block]])
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-> State WriterState Doc
|
2009-12-07 08:26:53 +00:00
|
|
|
|
definitionListItemToMan opts (label, defs) = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
labelText <- inlineListToMan opts label
|
2012-07-26 22:32:53 -07:00
|
|
|
|
contents <- if null defs
|
2007-11-03 23:27:58 +00:00
|
|
|
|
then return empty
|
2012-07-26 22:32:53 -07:00
|
|
|
|
else liftM vcat $ forM defs $ \blocks -> do
|
2009-12-07 08:26:53 +00:00
|
|
|
|
let (first, rest) = case blocks of
|
2008-07-13 23:39:52 +00:00
|
|
|
|
((Para x):y) -> (Plain x,y)
|
|
|
|
|
(x:y) -> (x,y)
|
2009-12-07 08:26:53 +00:00
|
|
|
|
[] -> error "blocks is null"
|
|
|
|
|
rest' <- liftM vcat $
|
|
|
|
|
mapM (\item -> blockToMan opts item) rest
|
2007-11-03 23:27:58 +00:00
|
|
|
|
first' <- blockToMan opts first
|
|
|
|
|
return $ first' $$ text ".RS" $$ rest' $$ text ".RE"
|
2014-03-12 10:23:45 -07:00
|
|
|
|
return $ text ".TP" $$ nowrap (text ".B " <> labelText) $$ contents
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert list of Pandoc block elements to man.
|
|
|
|
|
blockListToMan :: WriterOptions -- ^ Options
|
|
|
|
|
-> [Block] -- ^ List of block elements
|
2012-07-26 22:32:53 -07:00
|
|
|
|
-> State WriterState Doc
|
2007-11-03 23:27:58 +00:00
|
|
|
|
blockListToMan opts blocks =
|
|
|
|
|
mapM (blockToMan opts) blocks >>= (return . vcat)
|
|
|
|
|
|
|
|
|
|
-- | Convert list of Pandoc inline elements to man.
|
|
|
|
|
inlineListToMan :: WriterOptions -> [Inline] -> State WriterState Doc
|
2009-06-06 21:27:46 +00:00
|
|
|
|
-- if list starts with ., insert a zero-width character \& so it
|
|
|
|
|
-- won't be interpreted as markup if it falls at the beginning of a line.
|
2012-03-09 13:39:19 -08:00
|
|
|
|
inlineListToMan opts lst@(Str ('.':_) : _) = mapM (inlineToMan opts) lst >>=
|
2009-06-06 21:27:46 +00:00
|
|
|
|
(return . (text "\\&" <>) . hcat)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
inlineListToMan opts lst = mapM (inlineToMan opts) lst >>= (return . hcat)
|
|
|
|
|
|
|
|
|
|
-- | Convert Pandoc inline element to man.
|
|
|
|
|
inlineToMan :: WriterOptions -> Inline -> State WriterState Doc
|
2013-08-08 23:14:12 -07:00
|
|
|
|
inlineToMan opts (Span _ ils) = inlineListToMan opts ils
|
2012-07-26 22:32:53 -07:00
|
|
|
|
inlineToMan opts (Emph lst) = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
contents <- inlineListToMan opts lst
|
|
|
|
|
return $ text "\\f[I]" <> contents <> text "\\f[]"
|
|
|
|
|
inlineToMan opts (Strong lst) = do
|
|
|
|
|
contents <- inlineListToMan opts lst
|
|
|
|
|
return $ text "\\f[B]" <> contents <> text "\\f[]"
|
|
|
|
|
inlineToMan opts (Strikeout lst) = do
|
|
|
|
|
contents <- inlineListToMan opts lst
|
|
|
|
|
return $ text "[STRIKEOUT:" <> contents <> char ']'
|
|
|
|
|
inlineToMan opts (Superscript lst) = do
|
|
|
|
|
contents <- inlineListToMan opts lst
|
|
|
|
|
return $ char '^' <> contents <> char '^'
|
|
|
|
|
inlineToMan opts (Subscript lst) = do
|
|
|
|
|
contents <- inlineListToMan opts lst
|
|
|
|
|
return $ char '~' <> contents <> char '~'
|
2008-07-15 23:26:06 +00:00
|
|
|
|
inlineToMan opts (SmallCaps lst) = inlineListToMan opts lst -- not supported
|
2007-11-03 23:27:58 +00:00
|
|
|
|
inlineToMan opts (Quoted SingleQuote lst) = do
|
|
|
|
|
contents <- inlineListToMan opts lst
|
|
|
|
|
return $ char '`' <> contents <> char '\''
|
|
|
|
|
inlineToMan opts (Quoted DoubleQuote lst) = do
|
|
|
|
|
contents <- inlineListToMan opts lst
|
|
|
|
|
return $ text "\\[lq]" <> contents <> text "\\[rq]"
|
2008-08-04 03:15:12 +00:00
|
|
|
|
inlineToMan opts (Cite _ lst) =
|
|
|
|
|
inlineListToMan opts lst
|
2011-01-26 20:44:25 -08:00
|
|
|
|
inlineToMan _ (Code _ str) =
|
2010-07-14 10:52:06 -07:00
|
|
|
|
return $ text $ "\\f[C]" ++ escapeCode str ++ "\\f[]"
|
2008-07-13 23:39:52 +00:00
|
|
|
|
inlineToMan _ (Str str) = return $ text $ escapeString str
|
2013-11-01 14:27:22 -07:00
|
|
|
|
inlineToMan opts (Math InlineMath str) =
|
2014-07-19 18:10:51 +01:00
|
|
|
|
inlineListToMan opts $ texMathToInlines InlineMath str
|
2008-08-13 03:02:42 +00:00
|
|
|
|
inlineToMan opts (Math DisplayMath str) = do
|
2014-07-19 18:10:51 +01:00
|
|
|
|
contents <- inlineListToMan opts $ texMathToInlines DisplayMath str
|
2010-12-22 00:21:56 -08:00
|
|
|
|
return $ cr <> text ".RS" $$ contents $$ text ".RE"
|
2013-08-10 17:23:51 -07:00
|
|
|
|
inlineToMan _ (RawInline f str)
|
|
|
|
|
| f == Format "man" = return $ text str
|
|
|
|
|
| otherwise = return empty
|
2010-12-22 00:21:56 -08:00
|
|
|
|
inlineToMan _ (LineBreak) = return $
|
|
|
|
|
cr <> text ".PD 0" $$ text ".P" $$ text ".PD" <> cr
|
2015-12-11 15:58:11 -08:00
|
|
|
|
inlineToMan _ SoftBreak = return space
|
2010-12-22 00:21:56 -08:00
|
|
|
|
inlineToMan _ Space = return space
|
2015-07-26 18:30:47 +02:00
|
|
|
|
inlineToMan opts (Link _ txt (src, _)) = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
linktext <- inlineListToMan opts txt
|
2014-08-03 14:44:39 +04:00
|
|
|
|
let srcSuffix = fromMaybe src (stripPrefix "mailto:" src)
|
2011-01-26 20:44:25 -08:00
|
|
|
|
return $ case txt of
|
2013-01-06 20:51:51 -08:00
|
|
|
|
[Str s]
|
|
|
|
|
| escapeURI s == srcSuffix ->
|
|
|
|
|
char '<' <> text srcSuffix <> char '>'
|
2011-01-26 20:44:25 -08:00
|
|
|
|
_ -> linktext <> text " (" <> text src <> char ')'
|
2015-07-26 18:30:47 +02:00
|
|
|
|
inlineToMan opts (Image attr alternate (source, tit)) = do
|
2012-07-26 22:32:53 -07:00
|
|
|
|
let txt = if (null alternate) || (alternate == [Str ""]) ||
|
2007-11-03 23:27:58 +00:00
|
|
|
|
(alternate == [Str source]) -- to prevent autolinks
|
|
|
|
|
then [Str "image"]
|
|
|
|
|
else alternate
|
2015-07-26 18:30:47 +02:00
|
|
|
|
linkPart <- inlineToMan opts (Link attr txt (source, tit))
|
2007-11-03 23:27:58 +00:00
|
|
|
|
return $ char '[' <> text "IMAGE: " <> linkPart <> char ']'
|
2012-07-26 22:32:53 -07:00
|
|
|
|
inlineToMan _ (Note contents) = do
|
2009-12-31 01:17:27 +00:00
|
|
|
|
-- add to notes in state
|
|
|
|
|
modify $ \st -> st{ stNotes = contents : stNotes st }
|
|
|
|
|
notes <- liftM stNotes get
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let ref = show $ (length notes)
|
|
|
|
|
return $ char '[' <> text ref <> char ']'
|