packages feed

mdoc-0.2.0.0: src/Mdoc/Syntax/MdocLine.hs

-- |
--
-- Module      : Mdoc.Syntax.MdocLine
-- Copyright   : (c) 2026 Patrick Brisbin
-- License     : AGPL-3
-- Maintainer  : pbrisbin@gmail.com
-- Stability   : experimental
-- Portability : POSIX
module Mdoc.Syntax.MdocLine
  ( MdocLine (..)

    -- * Lenses
  , _Dd

    -- * Pretty
  , prettyMdocLine
  ) where

import Mdoc.Prelude

import Mdoc.Optics
import Mdoc.Pretty
import Mdoc.Syntax.MacroArg
import Mdoc.Syntax.MacroName
import Mdoc.Syntax.TableLine
import Mdoc.Syntax.TroffMacro

data MdocLine
  = Comment Text
  | MacroLine MacroName [MacroArg]
  | TextLine Text
  | TableLines [TableLine]
  | TroffMacro TroffMacro
  deriving stock (Eq, Show)

_Dd :: Prism' MdocLine [MacroArg]
_Dd = prism (MacroLine Dd) $ \case
  MacroLine Dd args -> Right args
  other -> Left other

prettyMdocLine :: MdocLine -> Doc Ann
prettyMdocLine = \case
  Comment x -> annotate AnnComment $ ".\\\" " <> pretty x
  MacroLine name args ->
    hsep
      $ annotate AnnComment "."
        <> annotate AnnCommand (prettyMacroName name)
        : map prettyMacroArg args
  TextLine x -> reflow x
  TableLines ls -> prettyTableLines ls
  TroffMacro m -> prettyTroffMacro m

instance Pretty MdocLine where
  pretty = unAnnotate . prettyMdocLine