{-# LANGUAGE CPP #-}
{-# LANGUAGE Safe #-}
module Text.Show.Pretty
(
Value(..), Name
, valToStr
, valToDoc
, valToHtmlPage
, parseValue, reify, ppDoc, ppShow, pPrint
,
ppDocList, ppShowList, pPrintList
, dumpDoc, dumpStr, PrettyVal(..)
, valToHtml, HtmlOpts(..), defaultHtmlOpts, htmlPage, Html(..)
, getDataDir
, ppValue
) where
import Text.PrettyPrint
import qualified Text.Show.Parser as P
import Text.Show.Value
import Text.Show.PrettyVal
import Text.Show.Html
import Data.Foldable(Foldable,toList)
import Language.Haskell.Lexer(rmSpace,lexerPass0)
import Paths_pretty_show (getDataDir)
#if MIN_VERSION_base(4,11,0)
import Prelude hiding ( (<>) )
#else
import Prelude
#endif
{-# DEPRECATED ppValue "Please use `valToDoc` instead." #-}
ppValue :: Value -> Doc
ppValue = valToDoc
reify :: Show a => a -> Maybe Value
reify = parseValue . show
parseValue :: String -> Maybe Value
parseValue = P.parseValue . rmSpace . lexerPass0
ppShow :: Show a => a -> String
ppShow = show . ppDoc
ppShowList :: (Foldable f, Show a) => f a -> String
ppShowList = show . ppDocList
ppDoc :: Show a => a -> Doc
ppDoc a = case parseValue txt of
Just v -> valToDoc v
Nothing -> text txt
where txt = show a
ppDocList :: (Foldable f, Show a) => f a -> Doc
ppDocList = blockWith vcat '[' ']' . map ppDoc . toList
pPrint :: Show a => a -> IO ()
pPrint = putStrLn . ppShow
pPrintList :: (Foldable f, Show a) => f a -> IO ()
pPrintList = putStrLn . ppShowList
dumpDoc :: PrettyVal a => a -> Doc
dumpDoc = valToDoc . prettyVal
dumpStr :: PrettyVal a => a -> String
dumpStr = show . dumpDoc
valToStr :: Value -> String
valToStr = show . valToDoc
valToDoc :: Value -> Doc
valToDoc val = case val of
Con c vs -> ppCon c vs
InfixCons v1 cvs -> hang_sep (go v1 cvs)
where
go v [] = [ppInfixAtom v]
go v ((n,v2):cvs') = (ppInfixAtom v <+> text n):go v2 cvs'
hang_sep [] = empty
hang_sep (x:xs) = hang x 2 (sep xs)
Rec c fs -> hang (text c) 2 $ block '{' '}' (map ppField fs)
where ppField (x,v) = hang (text x <+> char '=') 2 (valToDoc v)
List vs -> block '[' ']' (map valToDoc vs)
Tuple vs -> block '(' ')' (map valToDoc vs)
Neg v -> char '-' <> ppAtom v
Ratio x y -> hang (ppAtom x <+> text "%") 2 (ppAtom y)
Integer x -> text x
Float x -> text x
Char x -> text x
String x -> text x
ppAtom :: Value -> Doc
ppAtom v
| isAtom v = valToDoc v
| otherwise = parens (valToDoc v)
ppInfixAtom :: Value -> Doc
ppInfixAtom v
| isInfixAtom v = valToDoc v
| otherwise = parens (valToDoc v)
ppCon :: Name -> [Value] -> Doc
ppCon "" vs = sep (map ppAtom vs)
ppCon c vs = hang (text c) 2 (sep (map ppAtom vs))
isAtom :: Value -> Bool
isAtom (Con _ (_:_)) = False
isAtom (InfixCons {}) = False
isAtom (Ratio {}) = False
isAtom (Neg {}) = False
isAtom _ = True
isInfixAtom :: Value -> Bool
isInfixAtom (InfixCons {}) = False
isInfixAtom (Ratio {}) = False
isInfixAtom (Neg {}) = False
isInfixAtom _ = True
block :: Char -> Char -> [Doc] -> Doc
block = blockWith sep
blockWith :: ([Doc] -> Doc) -> Char -> Char -> [Doc] -> Doc
blockWith _ a b [] = char a <> char b
blockWith f a b (d:ds) = f $
(char a <+> d) : [ char ',' <+> x | x <- ds ] ++ [ char b ]