{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}

{-# HLINT ignore "Use record patterns" #-}
module Text.BBCode.Internal.Pretty
  ( pretty
  )
where

import Text.BBCode.Internal.Helper

{- ORMOLU_DISABLE -}

{- | Serialize BBCode AST

Parsing @'pretty' x@ should give you @x@, but currently that rarely works mostly
because of whitespaces. Whitespaces are appended when prettifying, but not
stripped when parsing. This leads to redundant whitespaces in parsed AST

Can cause error at runtime if unrepresentable element is passed

>>> pretty $ ElSimple HR "abc"
Prelude.undefined
-}
pretty :: BBCode -> Text
pretty :: BBCode -> Text
pretty (ElText Text
x) = Text
x
pretty (ElDocument [BBCode]
xs) = forall a. Monoid a => [a] -> a
mconcat forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BBCode -> Text
pretty [BBCode]
xs

pretty (ElVoid El
HR) = El -> Text
opening El
HR forall a. Semigroup a => a -> a -> a
<> Text
"\n"
pretty (ElVoid El
BR) = El -> Text
opening El
BR forall a. Semigroup a => a -> a -> a
<> Text
"\n"
pretty (ElVoid El
Clear) = El -> Text
opening El
Clear forall a. Semigroup a => a -> a -> a
<> Text
"\n"
pretty (ElVoid El
ListElement) = Text
"\n" forall a. Semigroup a => a -> a -> a
<> El -> Text
opening El
ListElement
pretty (ElVoid El
_) = forall a. HasCallStack => a
undefined

pretty (ElSimple El
Bold BBCode
bb) = El -> Text -> Text
wrap El
Bold forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
Italic BBCode
bb) = El -> Text -> Text
wrap El
Italic forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
Underline BBCode
bb) = El -> Text -> Text
wrap El
Underline forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
Strikethrough BBCode
bb) = El -> Text -> Text
wrap El
Strikethrough forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
Indent BBCode
bb) = El -> Text -> Text
wrap El
Indent forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
NFO BBCode
bb) = El -> Text -> Text
wrap El
NFO forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
Oneline BBCode
bb) = El -> Text -> Text
wrap El
Oneline forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
Code BBCode
bb) = El -> Text -> Text
wrap El
Code forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
Preformatted BBCode
bb) = El -> Text -> Text
wrap El
Preformatted forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb

pretty (ElSimple El
Box BBCode
bb) = El -> Text -> Text
wrap El
Box forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
Image BBCode
bb) = El -> Text -> Text
wrap El
Image forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
Quote BBCode
bb) = El -> Text -> Text
wrap El
Quote forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Text
surround Text
"\n" forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
Spoiler BBCode
bb) = El -> Text -> Text
wrap El
Spoiler forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Text
surround Text
"\n" forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElSimple El
List (ElDocument [BBCode]
bb)) =
  El -> Text -> Text
wrap El
List
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Semigroup a => a -> a -> a
<> Text
"\n")
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl forall a. Semigroup a => a -> a -> a
(<>) Text
""
    forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BBCode -> Text
pretty [BBCode]
bb
pretty (ElSimple El
_ BBCode
_) = forall a. HasCallStack => a
undefined

pretty (ElArg El
Box Text
arg BBCode
bb) = El -> Text -> Text -> Text
wrapArg El
Box Text
arg forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElArg El
Image Text
arg BBCode
bb) = El -> Text -> Text -> Text
wrapArg El
Image Text
arg forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
-- BUG:/NOT? if arg contains double quote " then parser won't be able to parse it
pretty (ElArg El
Quote Text
arg BBCode
bb) = El -> Text -> Text -> Text
wrapArg El
Quote (forall a. Monoid a => [a] -> a
mconcat [Text
"\"", Text
arg, Text
"\""]) forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElArg El
Spoiler Text
arg BBCode
bb) = El -> Text -> Text -> Text
wrapArg El
Spoiler (forall a. Monoid a => [a] -> a
mconcat [Text
"\"", Text
arg, Text
"\""]) forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElArg El
List Text
arg (ElDocument [BBCode]
bb)) =
  El -> Text -> Text -> Text
wrapArg El
List Text
arg
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Semigroup a => a -> a -> a
<> Text
"\n")
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl forall a. Semigroup a => a -> a -> a
(<>) Text
""
    forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BBCode -> Text
pretty [BBCode]
bb

pretty (ElArg El
Color Text
arg BBCode
bb) = El -> Text -> Text -> Text
wrapArg El
Color Text
arg forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElArg El
URL Text
arg BBCode
bb) = El -> Text -> Text -> Text
wrapArg El
URL Text
arg forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElArg El
Size Text
arg BBCode
bb) = El -> Text -> Text -> Text
wrapArg El
Size Text
arg forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElArg El
Align Text
arg BBCode
bb) = El -> Text -> Text -> Text
wrapArg El
Align Text
arg forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElArg El
Font Text
arg BBCode
bb) = El -> Text -> Text -> Text
wrapArg El
Font Text
arg forall a b. (a -> b) -> a -> b
$ BBCode -> Text
pretty BBCode
bb
pretty (ElArg El
_ Text
_ BBCode
_) = forall a. HasCallStack => a
undefined
{-# INLINEANBLE pretty #-}
{- ORMOLU_ENABLE -}