-- | Return groff man representation of document.
pandocToMan :: WriterOptions -> Pandoc -> State WriterState String
pandocToMan opts (Pandoc (Meta title authors date) blocks) = do
  titleText <- inlineListToMan opts title
  authors' <- mapM (inlineListToMan opts) authors
  date' <- inlineListToMan opts date 
  let (cmdName, rest) = break (== ' ') $ render titleText
  let (title', section) = case reverse cmdName of
                            (')':d:'(':xs) | d `elem` ['0'..'9'] -> 
                                  (text (reverse xs), char d)
                            xs -> (text (reverse xs), doubleQuotes empty)                    
  let description = hsep $
                    map (doubleQuotes . text . removeLeadingTrailingSpace) $
                    splitBy '|' rest
  body <- blockListToMan opts blocks
  notes <- liftM stNotes get
  notes' <- notesToMan opts (reverse notes)
  let main = render $ body $$ notes'
  hasTables <- liftM stHasTables get
  let context  = writerVariables opts ++
                 [ ("body", main)
                 , ("title", render title')
                 , ("section", render section)
                 , ("date", render date')
                 , ("description", render description) ] ++
                 [ ("has-tables", "yes") | hasTables ] ++
                 [ ("author", render a) | a <- authors' ]
  if writerStandalone opts
     then return $ renderTemplate context $ writerTemplate opts
     else return main

-- | Return man representation of notes.
pandocToMan :: WriterOptions -> Pandoc -> State WriterState String
pandocToMan opts (Pandoc (Meta title authors date) blocks) = do
  titleText <- inlineListToMan opts title
  authors' <- mapM (inlineListToMan opts) authors
  date' <- inlineListToMan opts date 
  let (cmdName, rest) = break (== ' ') $ render titleText
  let (title', section) = case reverse cmdName of
                            (')':d:'(':xs) | d `elem` ['0'..'9'] -> 
                                  (text (reverse xs), char d)
                            xs -> (text (reverse xs), doubleQuotes empty)                    
  let description = hsep $
                    map (doubleQuotes . text . removeLeadingTrailingSpace) $
                    splitBy '|' rest
  body <- blockListToMan opts blocks
  notes <- liftM stNotes get
  notes' <- notesToMan opts (reverse notes)
  let main = render $ body $$ notes'
  hasTables <- liftM stHasTables get
  let context  = writerVariables opts ++
                 [ ("body", main)
                 , ("title", render title')
                 , ("section", render section)
                 , ("date", render date')
                 , ("description", render description) ] ++
                 [ ("has-tables", "yes") | hasTables ] ++
                 [ ("author", render a) | a <- authors' ]
  if writerStandalone opts
     then return $ renderTemplate context $ writerTemplate opts
     else return main

-- | Return man representation of notes.
pandocToMan opts (Pandoc (Meta title authors date) blocks) = do
  titleText <- inlineListToMan opts title
  authors' <- mapM (inlineListToMan opts) authors
  date' <- inlineListToMan opts date 
  let (cmdName, rest) = break (== ' ') $ render titleText
  let (title', section) = case reverse cmdName of
                            (')':d:'(':xs) | d `elem` ['0'..'9'] -> 
                                  (text (reverse xs), char d)
                            xs -> (text (reverse xs), doubleQuotes empty)                    
  let description = hsep $
                    map (doubleQuotes . text . removeLeadingTrailingSpace) $
                    splitBy '|' rest
  body <- blockListToMan opts blocks
  notes <- liftM stNotes get
  notes' <- notesToMan opts (reverse notes)
  let main = render $ body $$ notes'
  hasTables <- liftM stHasTables get
  let context  = writerVariables opts ++
                 [ ("body", main)
                 , ("title", render title')
                 , ("section", render section)
                 , ("date", render date')
                 , ("description", render description) ] ++
                 [ ("has-tables", "yes") | hasTables ] ++
                 [ ("author", render a) | a <- authors' ]
  if writerStandalone opts
     then return $ renderTemplate context $ writerTemplate opts
     else return main

-- | Return man representation of notes.