packages feed

mdoc-0.1.0.0: src/Mdoc/Gen/Described.hs

-- |
--
-- Module      : Mdoc.Gen.Described
-- Copyright   : (c) 2026 Patrick Brisbin
-- License     : AGPL-3
-- Maintainer  : pbrisbin@gmail.com
-- Stability   : experimental
-- Portability : POSIX
module Mdoc.Gen.Described
  ( Described (..)
  , renderDescribedItems
  , renderDescribedItem
  ) where

import Mdoc.Prelude

import Data.List (intersperse)
import Mdoc.Gen.Optionality
import Mdoc.MacroArg
import Mdoc.MacroName
import Mdoc.MdocLine

data Described a = Described
  { item :: a
  , optionality :: Optionality
  , multiple :: Bool
  , helpLines :: Maybe (NonEmpty String)
  }
  deriving stock (Eq, Functor, Generic, Show)

renderDescribedItems
  :: Foldable t
  => (a -> [MacroArg])
  -> t (Described a)
  -> [MdocLine]
renderDescribedItems f = concatMap (renderDescribedItem f) . toList

-- TODO: add "Default:" line
-- TODO: add "This option may be specified multiple times line
renderDescribedItem :: (a -> [MacroArg]) -> Described a -> [MdocLine]
renderDescribedItem f d = MacroLine It (f d.item) : descriptionLines
 where
  descriptionLines =
    intersperse (MacroLine Pp [])
      $ map (TextLine . esc . pack)
      $ maybe [] toList d.helpLines