module Imm.Pretty where
import Imm.Prelude
import Data.NonNull
import Data.Time
import Data.Tree
import Text.Atom.Types as Atom
import Text.PrettyPrint.ANSI.Leijen hiding ((<$>), (<>))
import Text.RSS.Types as RSS
import URI.ByteString
prettyTree :: (Pretty a) => Tree a -> Doc
prettyTree (Node n s) = pretty n <++> indent 2 (vsep $ prettyTree <$> s)
prettyTime :: UTCTime -> Doc
prettyTime = text . formatTime defaultTimeLocale rfc822DateFormat
prettyPerson :: AtomPerson -> Doc
prettyPerson p = text (fromText $ toNullable $ personName p) <> email where
email = if null $ personEmail p
then mempty
else space <> angles (text $ fromText $ personEmail p)
prettyLink :: AtomLink -> Doc
prettyLink l = withAtomURI prettyURI $ linkHref l
prettyAtomText :: AtomText -> Doc
prettyAtomText (AtomPlainText _ t) = text $ fromText t
prettyAtomText (AtomXHTMLText t) = text $ fromText t
prettyEntry :: AtomEntry -> Doc
prettyEntry e = text "Entry:" <+> prettyAtomText (entryTitle e) <++> indent 4
( text "By" <+> equals <+> list (prettyPerson <$> entryAuthors e)
<++> text "Updated" <+> equals <+> prettyTime (entryUpdated e)
<++> text "Links" <+> equals <+> list (prettyLink <$> entryLinks e)
)
prettyItem :: RssItem -> Doc
prettyItem i = text "Item:" <+> text (fromText $ itemTitle i) <++> indent 4
( text "By" <+> equals <+> text (fromText $ itemAuthor i)
<++> text "Updated" <+> equals <+> fromMaybe (text "<empty>") (prettyTime <$> itemPubDate i)
<++> text "Link" <+> equals <+> fromMaybe (text "<empty>") (withRssURI prettyURI <$> itemLink i)
)
prettyURI :: URIRef a -> Doc
prettyURI uri = text $ fromText $ decodeUtf8 $ serializeURIRef' uri
prettyGuid :: RssGuid -> Doc
prettyGuid (GuidText t) = text $ fromText t
prettyGuid (GuidUri (RssURI u)) = prettyURI u
prettyAtomContent :: AtomContent -> Doc
prettyAtomContent (AtomContentInlineText _ t) = text $ fromText t
prettyAtomContent (AtomContentInlineXHTML t) = text $ fromText t
prettyAtomContent (AtomContentInlineOther _ t) = text $ fromText t
prettyAtomContent (AtomContentOutOfLine _ u) = withAtomURI prettyURI u