packages feed

mdoc-0.1.0.0: src/Mdoc/Gen.hs

-- |
--
-- Module      : Mdoc.Gen
-- Copyright   : (c) 2026 Patrick Brisbin
-- License     : AGPL-3
-- Maintainer  : pbrisbin@gmail.com
-- Stability   : experimental
-- Portability : POSIX
module Mdoc.Gen
  ( genMan1
  , genMan5
  , genMan1Template
  , genMan5Template
  , putMdoc
  ) where

import Mdoc.Prelude

import Data.Text qualified as T
import Mdoc
import Mdoc.Gen.CrossRef
import Mdoc.Gen.Description
import Mdoc.Gen.Environment
import Mdoc.Gen.ExitStatus
import Mdoc.Gen.File
import Mdoc.Gen.Man1
import Mdoc.Gen.Man5
import Mdoc.Gen.Name
import Mdoc.Gen.Synopsis
import Mdoc.Gen.Template
import Mdoc.Pretty
import System.IO (stdout)

genMan1 :: MonadIO m => Man1 -> m Mdoc
genMan1 = genMan1Template TemplateBuiltin1

genMan5 :: MonadIO m => Man5 -> m Mdoc
genMan5 = genMan5Template TemplateBuiltin5

genMan1Template :: MonadIO m => Template -> Man1 -> m Mdoc
genMan1Template t man1 = interpolateTemplate vs t
 where
  vs =
    InterpolationValues
      { docTitle = T.toUpper $ pack man1.name.primary
      , docSection = 1
      , name = nameMdocLines man1.name
      , synopsis = man1Synopsis man1
      , description = man1Description man1
      , environment = nonEmpty $ man1Environment man1
      , files = renderFiles <$> nonEmpty man1.files
      , exitStatus = renderExitStatuses <$> man1.exitStatus
      , seeAlso = renderCrossRefs <$> nonEmpty man1.seeAlso
      }

genMan5Template :: MonadIO m => Template -> Man5 -> m Mdoc
genMan5Template t man5 = interpolateTemplate vs t
 where
  vs =
    InterpolationValues
      { docTitle = T.toUpper $ pack man5.name.primary
      , docSection = 5
      , name = nameMdocLines man5.name
      , synopsis = man5Synopsis man5
      , description = man5Description man5
      , environment = Nothing -- unused
      , files = Nothing -- unused
      , exitStatus = Nothing -- unused
      , seeAlso = renderCrossRefs <$> nonEmpty man5.seeAlso
      }

-- | Render an 'Mdoc' with 'ColorAuto' to @stdout@
--
-- For more flexibility, use "Mdoc.Pretty" directly.
putMdoc :: MonadIO m => Mdoc -> m ()
putMdoc = putDoc ColorAuto stdout . prettyMdoc