{-| Contains all utilities related to markdown processing
-}
module Dhall.Docs.Markdown
    ( MarkdownParseError(..)
    , MMark
    , parseMarkdown
    , markdownToHtml
    , MMark.render
    ) where

import Data.Text       (Text)
import Lucid
import Path            (File, Path, Rel)
import Text.MMark      (MMarkErr, MMark)
import Text.Megaparsec (ParseErrorBundle (..))

import qualified Path
import qualified Text.MMark as MMark

-- | Wrapper around `MMarkErr` errors
newtype MarkdownParseError = MarkdownParseError
    { MarkdownParseError -> ParseErrorBundle Text MMarkErr
unwrap :: ParseErrorBundle Text MMarkErr
    }

{-| Takes a text that could contain markdown and returns the generated HTML.
    If an error occurs while parsing, it also returns the error information.
-}
markdownToHtml
    :: Path Rel File -- ^ Used by `Mmark.parse` for error messages
    -> Text          -- ^ Text to parse
    -> Either MarkdownParseError (Html ())
markdownToHtml :: Path Rel File -> Text -> Either MarkdownParseError (Html ())
markdownToHtml Path Rel File
relFile Text
contents =
    MMark -> Html ()
MMark.render (MMark -> Html ())
-> Either MarkdownParseError MMark
-> Either MarkdownParseError (Html ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Path Rel File -> Text -> Either MarkdownParseError MMark
parseMarkdown Path Rel File
relFile Text
contents

{-| Takes a text that could contain markdown and returns either the parsed
    markdown or, if parsing fails, the error information.
-}
parseMarkdown
    :: Path Rel File -- ^ Used by `Mmark.parse` for error messages
    -> Text          -- ^ Text to parse
    -> Either MarkdownParseError MMark
parseMarkdown :: Path Rel File -> Text -> Either MarkdownParseError MMark
parseMarkdown Path Rel File
relFile Text
contents =
    case FilePath -> Text -> Either (ParseErrorBundle Text MMarkErr) MMark
MMark.parse (Path Rel File -> FilePath
Path.fromRelFile Path Rel File
relFile) Text
contents of
        Left ParseErrorBundle Text MMarkErr
err -> MarkdownParseError -> Either MarkdownParseError MMark
forall a b. a -> Either a b
Left MarkdownParseError { unwrap :: ParseErrorBundle Text MMarkErr
unwrap = ParseErrorBundle Text MMarkErr
err }
        Right MMark
mmark -> MMark -> Either MarkdownParseError MMark
forall a b. b -> Either a b
Right MMark
mmark