Skip to content

Commit

Permalink
Reformat ihaskell display packages
Browse files Browse the repository at this point in the history
  • Loading branch information
gibiansky committed Mar 21, 2015
1 parent c53f70d commit 80aa284
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 240 deletions.
15 changes: 8 additions & 7 deletions ihaskell-display/ihaskell-aeson/IHaskell/Display/Aeson.hs
@@ -1,16 +1,17 @@
{-# LANGUAGE NoImplicitPrelude, TypeSynonymInstances, QuasiQuotes #-}

module IHaskell.Display.Aeson () where

import ClassyPrelude
import Data.Textual.Encoding
import Data.Aeson
import Data.Aeson.Encode.Pretty
import Data.String.Here
import ClassyPrelude
import Data.Textual.Encoding
import Data.Aeson
import Data.Aeson.Encode.Pretty
import Data.String.Here

import IHaskell.Display
import IHaskell.Display

instance IHaskellDisplay Value where
display renderable = return $ Display [plain json, html dom]
where
where
json = unpack $ decodeUtf8 $ encodePretty renderable
dom = [i|<div class="highlight-code" id="javascript">${json}</div>|]
16 changes: 10 additions & 6 deletions ihaskell-display/ihaskell-basic/IHaskell/Display/Basic.hs
@@ -1,15 +1,19 @@
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}

module IHaskell.Display.Basic () where

import IHaskell.Display
import IHaskell.Display

import Text.Printf
import Text.Printf

instance Show a => IHaskellDisplay (Maybe a) where
display just = return $ Display [stringDisplay, htmlDisplay]
where
where
stringDisplay = plain (show just)
htmlDisplay = html str
str = case just of
Nothing -> "<span style='color: red; font-weight: bold;'>Nothing</span>"
Just x -> printf "<span style='color: green; font-weight: bold;'>Just</span><span style='font-family: monospace;'>%s</span>" (show x)
str =
case just of
Nothing -> "<span style='color: red; font-weight: bold;'>Nothing</span>"
Just x -> printf
"<span style='color: green; font-weight: bold;'>Just</span><span style='font-family: monospace;'>%s</span>"
(show x)
15 changes: 8 additions & 7 deletions ihaskell-display/ihaskell-blaze/IHaskell/Display/Blaze.hs
@@ -1,17 +1,18 @@
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}

module IHaskell.Display.Blaze () where

import IHaskell.Display
import IHaskell.Display

import Text.Printf
import Text.Blaze.Html
import Text.Blaze.Renderer.Pretty
import Text.Blaze.Internal
import Control.Monad
import Text.Printf
import Text.Blaze.Html
import Text.Blaze.Renderer.Pretty
import Text.Blaze.Internal
import Control.Monad

instance IHaskellDisplay (MarkupM a) where
display val = return $ Display [stringDisplay, htmlDisplay]
where
where
str = renderMarkup (void val)
stringDisplay = plain str
htmlDisplay = html str
39 changes: 20 additions & 19 deletions ihaskell-display/ihaskell-charts/IHaskell/Display/Charts.hs
@@ -1,16 +1,17 @@
{-# LANGUAGE NoImplicitPrelude, CPP #-}

module IHaskell.Display.Charts () where

import ClassyPrelude
import ClassyPrelude

import System.Directory
import Data.Default.Class
import Graphics.Rendering.Chart.Renderable
import Graphics.Rendering.Chart.Backend.Cairo
import System.Directory
import Data.Default.Class
import Graphics.Rendering.Chart.Renderable
import Graphics.Rendering.Chart.Backend.Cairo
import qualified Data.ByteString.Char8 as Char
import System.IO.Unsafe
import System.IO.Unsafe

import IHaskell.Display
import IHaskell.Display

width :: Width
width = 450
Expand All @@ -22,8 +23,8 @@ instance IHaskellDisplay (Renderable a) where
display renderable = do
pngDisp <- chartData renderable PNG

-- We can add `svg svgDisplay` to the output of `display`,
-- but SVGs are not resizable in the IPython notebook.
-- We can add `svg svgDisplay` to the output of `display`, but SVGs are not resizable in the IPython
-- notebook.
svgDisp <- chartData renderable SVG

return $ Display [pngDisp, svgDisp]
Expand All @@ -34,17 +35,17 @@ chartData renderable format = do

-- Write the PNG image.
let filename = ".ihaskell-chart.png"
opts = def{_fo_format = format, _fo_size = (width, height)}
toFile = renderableToFile opts
opts = def { _fo_format = format, _fo_size = (width, height) }
mkFile opts filename renderable

-- Convert to base64.
imgData <- readFile $ fpFromString filename
return $
case format of
PNG -> png width height $ base64 imgData
SVG -> svg $ Char.unpack imgData
#if MIN_VERSION_Chart_cairo(1,3,0)
toFile filename renderable
mkFile opts filename renderable = renderableToFile opts filename renderable
#else
toFile renderable filename
mkFile opts filename renderable = renderableToFile opts renderable filename
#endif

-- Convert to base64.
imgData <- readFile $ fpFromString filename
return $ case format of
PNG -> png width height $ base64 imgData
SVG -> svg $ Char.unpack imgData
25 changes: 14 additions & 11 deletions ihaskell-display/ihaskell-diagrams/IHaskell/Display/Diagrams.hs
@@ -1,17 +1,18 @@
{-# LANGUAGE NoImplicitPrelude, TypeSynonymInstances, FlexibleInstances #-}
{-# LANGUAGE NoImplicitPrelude, TypeSynonymInstances, FlexibleInstances #-}

module IHaskell.Display.Diagrams (diagram, animation) where

import ClassyPrelude
import ClassyPrelude

import System.Directory
import System.Directory
import qualified Data.ByteString.Char8 as Char
import System.IO.Unsafe
import System.IO.Unsafe

import Diagrams.Prelude
import Diagrams.Backend.Cairo
import Diagrams.Prelude
import Diagrams.Backend.Cairo

import IHaskell.Display
import IHaskell.Display.Diagrams.Animation
import IHaskell.Display
import IHaskell.Display.Diagrams.Animation

instance IHaskellDisplay (QDiagram Cairo R2 Any) where
display renderable = do
Expand All @@ -36,11 +37,13 @@ diagramData renderable format = do

-- Convert to base64.
imgData <- readFile $ fpFromString filename
let value = case format of
PNG -> png (floor imgWidth) (floor imgHeight) $ base64 imgData
SVG -> svg $ Char.unpack imgData
let value =
case format of
PNG -> png (floor imgWidth) (floor imgHeight) $ base64 imgData
SVG -> svg $ Char.unpack imgData

return value

where
extension SVG = "svg"
extension PNG = "png"
Expand Down
@@ -1,20 +1,21 @@
{-# LANGUAGE NoImplicitPrelude, TypeSynonymInstances, FlexibleInstances #-}
{-# LANGUAGE NoImplicitPrelude, TypeSynonymInstances, FlexibleInstances #-}

module IHaskell.Display.Diagrams.Animation (animation) where

import ClassyPrelude hiding (filename)
import ClassyPrelude hiding (filename)

import Diagrams.Prelude
import Diagrams.Backend.Cairo
import Diagrams.Backend.Cairo.CmdLine (GifOpts (..))
import Diagrams.Backend.CmdLine (DiagramOpts (..), mainRender)
import Diagrams.Prelude
import Diagrams.Backend.Cairo
import Diagrams.Backend.Cairo.CmdLine (GifOpts(..))
import Diagrams.Backend.CmdLine (DiagramOpts(..), mainRender)

import IHaskell.Display
import IHaskell.Display

instance IHaskellDisplay (QAnimation Cairo R2 Any) where
display renderable = do
gif <- animationData renderable
return $ Display [html $ "<img src=\"data:image/gif;base64,"
++ gif ++ "\" />"]
++ gif ++ "\" />"]

animationData :: Animation Cairo R2 -> IO String
animationData renderable = do
Expand All @@ -37,16 +38,12 @@ animationData renderable = do

-- Write the image.
let filename = ".ihaskell-diagram.gif"
diagOpts = DiagramOpts {
_width = Just . ceiling $ imgWidth
, _height = Just . ceiling $ imgHeight
, _output = filename
}
gifOpts = GifOpts {
_dither = True
, _noLooping = False
, _loopRepeat = Nothing
}
diagOpts = DiagramOpts
{ _width = Just . ceiling $ imgWidth
, _height = Just . ceiling $ imgHeight
, _output = filename
}
gifOpts = GifOpts { _dither = True, _noLooping = False, _loopRepeat = Nothing }
mainRender (diagOpts, gifOpts) frameSet

-- Convert to ascii represented base64 encoding
Expand Down
7 changes: 4 additions & 3 deletions ihaskell-display/ihaskell-hatex/IHaskell/Display/Hatex.hs
@@ -1,14 +1,15 @@
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}

-- | Provides 'IHaskellDisplay' instances for 'LaTeX' and 'LaTeXT'.
module IHaskell.Display.Hatex () where

import IHaskell.Display
import Text.LaTeX
import IHaskell.Display
import Text.LaTeX
import qualified Data.Text as T

instance IHaskellDisplay LaTeX where
display = display . IHaskell.Display.latex . T.unpack . render

instance (a ~ (), IO ~ io) => IHaskellDisplay (LaTeXT io a) where
display ma = display =<< execLaTeXT ma
display ma = display =<< execLaTeXT ma
103 changes: 64 additions & 39 deletions ihaskell-display/ihaskell-juicypixels/IHaskell/Display/Juicypixels.hs
@@ -1,33 +1,58 @@
{-# LANGUAGE NoImplicitPrelude, TypeSynonymInstances, FlexibleInstances #-}
module IHaskell.Display.Juicypixels
( module IHaskell.Display
, module Codec.Picture
) where

import Codec.Picture
import ClassyPrelude
import IHaskell.Display
import System.Directory
import System.IO.Unsafe
{-# LANGUAGE NoImplicitPrelude, TypeSynonymInstances, FlexibleInstances #-}

module IHaskell.Display.Juicypixels (module IHaskell.Display, module Codec.Picture) where

import Codec.Picture
import ClassyPrelude
import IHaskell.Display
import System.Directory
import System.IO.Unsafe

-- instances
instance IHaskellDisplay DynamicImage where display = displayImageAsJpg
instance IHaskellDisplay (Image Pixel8) where display = displayImageAsJpg . ImageY8
instance IHaskellDisplay (Image Pixel16) where display = displayImageAsJpg . ImageY16
instance IHaskellDisplay (Image PixelF) where display = displayImageAsJpg . ImageYF
instance IHaskellDisplay (Image PixelYA8) where display = displayImageAsJpg . ImageYA8
instance IHaskellDisplay (Image PixelYA16) where display = displayImageAsJpg . ImageYA16
instance IHaskellDisplay (Image PixelRGB8) where display = displayImageAsJpg . ImageRGB8
instance IHaskellDisplay (Image PixelRGB16) where display = displayImageAsJpg . ImageRGB16
instance IHaskellDisplay (Image PixelRGBF) where display = displayImageAsJpg . ImageRGBF
instance IHaskellDisplay (Image PixelRGBA8) where display = displayImageAsJpg . ImageRGBA8
instance IHaskellDisplay (Image PixelRGBA16) where display = displayImageAsJpg . ImageRGBA16
instance IHaskellDisplay (Image PixelYCbCr8) where display = displayImageAsJpg . ImageYCbCr8
instance IHaskellDisplay (Image PixelCMYK8) where display = displayImageAsJpg . ImageCMYK8
instance IHaskellDisplay (Image PixelCMYK16) where display = displayImageAsJpg . ImageCMYK16
instance IHaskellDisplay DynamicImage where
display = displayImageAsJpg

instance IHaskellDisplay (Image Pixel8) where
display = displayImageAsJpg . ImageY8

instance IHaskellDisplay (Image Pixel16) where
display = displayImageAsJpg . ImageY16

instance IHaskellDisplay (Image PixelF) where
display = displayImageAsJpg . ImageYF

instance IHaskellDisplay (Image PixelYA8) where
display = displayImageAsJpg . ImageYA8

instance IHaskellDisplay (Image PixelYA16) where
display = displayImageAsJpg . ImageYA16

instance IHaskellDisplay (Image PixelRGB8) where
display = displayImageAsJpg . ImageRGB8

instance IHaskellDisplay (Image PixelRGB16) where
display = displayImageAsJpg . ImageRGB16

instance IHaskellDisplay (Image PixelRGBF) where
display = displayImageAsJpg . ImageRGBF

instance IHaskellDisplay (Image PixelRGBA8) where
display = displayImageAsJpg . ImageRGBA8

instance IHaskellDisplay (Image PixelRGBA16) where
display = displayImageAsJpg . ImageRGBA16

instance IHaskellDisplay (Image PixelYCbCr8) where
display = displayImageAsJpg . ImageYCbCr8

instance IHaskellDisplay (Image PixelCMYK8) where
display = displayImageAsJpg . ImageCMYK8

instance IHaskellDisplay (Image PixelCMYK16) where
display = displayImageAsJpg . ImageCMYK16

-- main rendering function
displayImageAsJpg :: DynamicImage -> IO Display
displayImageAsJpg :: DynamicImage -> IO Display
displayImageAsJpg renderable = do
switchToTmpDir

Expand All @@ -40,30 +65,30 @@ displayImageAsJpg renderable = do

-- The type DynamicImage does not have a function to extract width and height
imWidth :: DynamicImage -> Int
imWidth img = w
where (w, h) = imWidthHeight img
imWidth img = w
where
(w, h) = imWidthHeight img

imHeight :: DynamicImage -> Int
imHeight img = h
where (w, h) = imWidthHeight img
where
(w, h) = imWidthHeight img

-- Helper functions to pattern match on the DynamicImage Constructors
imWidthHeight :: DynamicImage -> (Int, Int)
imWidthHeight (ImageY8 im) = imWH im
imWidthHeight (ImageY16 im) = imWH im
imWidthHeight (ImageYF im) = imWH im
imWidthHeight (ImageYA8 im) = imWH im
imWidthHeight (ImageY8 im) = imWH im
imWidthHeight (ImageY16 im) = imWH im
imWidthHeight (ImageYF im) = imWH im
imWidthHeight (ImageYA8 im) = imWH im
imWidthHeight (ImageYA16 im) = imWH im
imWidthHeight (ImageRGB8 im) = imWH im
imWidthHeight (ImageRGB16 im) = imWH im
imWidthHeight (ImageRGBF im) = imWH im
imWidthHeight (ImageRGB8 im) = imWH im
imWidthHeight (ImageRGB16 im) = imWH im
imWidthHeight (ImageRGBF im) = imWH im
imWidthHeight (ImageRGBA8 im) = imWH im
imWidthHeight (ImageRGBA16 im) = imWH im
imWidthHeight (ImageYCbCr8 im) = imWH im
imWidthHeight (ImageYCbCr8 im) = imWH im
imWidthHeight (ImageCMYK8 im) = imWH im
imWidthHeight (ImageCMYK16 im) = imWH im

imWH :: (Image a) -> (Int, Int)
imWH im = (imageWidth im, imageHeight im)


0 comments on commit 80aa284

Please sign in to comment.