packages feed

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

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

import Mdoc.Prelude

import Data.Text qualified as T
import Mdoc.MacroArg
import Mdoc.MacroName
import Mdoc.MdocLine (MdocLine (..))

data Name = Name
  { primary :: String
  , secondaries :: [String]
  , description :: String
  }
  deriving stock (Eq, Generic, Show)

nameMdocLines :: Name -> NonEmpty MdocLine
nameMdocLines n =
  maybe
    (nameLines n)
    (nameLines n <>)
    $ nonEmpty
    $ descriptionLines n.description

nameLines :: Name -> NonEmpty MdocLine
nameLines Name {primary, secondaries} =
  case nonEmpty secondaries of
    Nothing -> pure $ nm primary
    Just ne -> map nm_ (primary : init ne) |: nm (last ne)
 where
  nm :: String -> MdocLine
  nm x = MacroLine Nm [Bare $ esc $ pack x]

  nm_ :: String -> MdocLine
  nm_ x = MacroLine Nm [Bare $ esc $ pack x, Bare ","]

descriptionLines :: String -> [MdocLine]
descriptionLines d = [MacroLine Nd $ map (Bare . esc) $ T.words $ pack d]