packages feed

mdoc-0.5.0.0: src/Mdoc/Syntax/Mdoc.hs

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

    -- * Pretty
  , prettyMdoc

    -- * Util
  , textToMdoc
  , textToMdocPp
  ) where

import Mdoc.Prelude

import Data.Text qualified as T
import Mdoc.Optics
import Mdoc.Pretty
import Mdoc.Syntax.MacroName
import Mdoc.Syntax.MdocLine

newtype Mdoc = Mdoc
  { lines :: [MdocLine]
  }
  deriving stock (Eq, Generic, Show)
  deriving newtype (Monoid, Semigroup)
  deriving (ToJSON) via (Rendered Mdoc)

prettyMdoc :: Mdoc -> Doc Ann
prettyMdoc mdoc = vsep $ map prettyMdocLine mdoc.lines

instance Pretty Mdoc where
  pretty = unAnnotate . prettyMdoc

-- | Map 'Text' to an 'Mdoc' of 'TextLine's
--
-- @
-- Some text with
-- newlines
-- @
--
-- @
-- Some text with
-- newlines
-- @
textToMdoc :: Text -> Maybe Mdoc
textToMdoc =
  fmap (Mdoc . map (TextLine . esc) . toList)
    . nonEmpty
    . T.lines

-- | Map 'Text' to an 'Mdoc' of 'TextLine's, injecting a 'Pp' between each
--
-- @
-- Some text with
-- newlines
-- @
--
-- @
-- Some text with
-- .Pp
-- newlines
-- @
textToMdocPp :: Text -> Maybe Mdoc
textToMdocPp = fmap go . textToMdoc
 where
  go = field @"lines" %~ intersperse (MacroLine Pp [])