{-# LANGUAGE OverloadedStrings #-}

module Drasil.Data.Formats.Jupyter.Render
  ( CellSourceRenderStyle(..), NotebookRenderOptions(..), defaultNotebookRenderOptions
  , notebookToJSON, renderNotebook
  ) where

import Data.Text (Text)
import qualified Data.Text as T (lines)
import Prettyprinter (Doc)
import Drasil.Data.Formats.JSON
  ( JSON(..), JSONRenderOptions, renderJSON )
import Drasil.Data.Formats.Jupyter.Core
  ( Notebook(..), Cell(..), NotebookMetadata(..), KernelSpec(..), LanguageInfo(..) )

-- | JSON representation of cell source.
data CellSourceRenderStyle
  = ListOfLines -- ^ One string per line, with a trailing LF.
  | SingleString -- ^ One string containing the whole cell.
  deriving (CellSourceRenderStyle -> CellSourceRenderStyle -> Bool
(CellSourceRenderStyle -> CellSourceRenderStyle -> Bool)
-> (CellSourceRenderStyle -> CellSourceRenderStyle -> Bool)
-> Eq CellSourceRenderStyle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CellSourceRenderStyle -> CellSourceRenderStyle -> Bool
== :: CellSourceRenderStyle -> CellSourceRenderStyle -> Bool
$c/= :: CellSourceRenderStyle -> CellSourceRenderStyle -> Bool
/= :: CellSourceRenderStyle -> CellSourceRenderStyle -> Bool
Eq, Int -> CellSourceRenderStyle -> ShowS
[CellSourceRenderStyle] -> ShowS
CellSourceRenderStyle -> String
(Int -> CellSourceRenderStyle -> ShowS)
-> (CellSourceRenderStyle -> String)
-> ([CellSourceRenderStyle] -> ShowS)
-> Show CellSourceRenderStyle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CellSourceRenderStyle -> ShowS
showsPrec :: Int -> CellSourceRenderStyle -> ShowS
$cshow :: CellSourceRenderStyle -> String
show :: CellSourceRenderStyle -> String
$cshowList :: [CellSourceRenderStyle] -> ShowS
showList :: [CellSourceRenderStyle] -> ShowS
Show)

-- | Notebook format options, separate from JSON layout options.
-- Version numbers label the output; they do not convert the notebook structure.
data NotebookRenderOptions = NotebookRenderOptions
  { NotebookRenderOptions -> Int
notebookFormat :: Int
  , NotebookRenderOptions -> Int
notebookFormatMinor :: Int
  , NotebookRenderOptions -> CellSourceRenderStyle
sourceFormat :: CellSourceRenderStyle
  } deriving (NotebookRenderOptions -> NotebookRenderOptions -> Bool
(NotebookRenderOptions -> NotebookRenderOptions -> Bool)
-> (NotebookRenderOptions -> NotebookRenderOptions -> Bool)
-> Eq NotebookRenderOptions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NotebookRenderOptions -> NotebookRenderOptions -> Bool
== :: NotebookRenderOptions -> NotebookRenderOptions -> Bool
$c/= :: NotebookRenderOptions -> NotebookRenderOptions -> Bool
/= :: NotebookRenderOptions -> NotebookRenderOptions -> Bool
Eq, Int -> NotebookRenderOptions -> ShowS
[NotebookRenderOptions] -> ShowS
NotebookRenderOptions -> String
(Int -> NotebookRenderOptions -> ShowS)
-> (NotebookRenderOptions -> String)
-> ([NotebookRenderOptions] -> ShowS)
-> Show NotebookRenderOptions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NotebookRenderOptions -> ShowS
showsPrec :: Int -> NotebookRenderOptions -> ShowS
$cshow :: NotebookRenderOptions -> String
show :: NotebookRenderOptions -> String
$cshowList :: [NotebookRenderOptions] -> ShowS
showList :: [NotebookRenderOptions] -> ShowS
Show)

-- | Use notebook format 4.4 and a list of lines per cell by default.
defaultNotebookRenderOptions :: NotebookRenderOptions
defaultNotebookRenderOptions :: NotebookRenderOptions
defaultNotebookRenderOptions = Int -> Int -> CellSourceRenderStyle -> NotebookRenderOptions
NotebookRenderOptions Int
4 Int
4 CellSourceRenderStyle
ListOfLines

-- | Encode a new notebook using the selected version and source representation.
notebookToJSON :: NotebookRenderOptions -> Notebook -> JSON
notebookToJSON :: NotebookRenderOptions -> Notebook -> JSON
notebookToJSON NotebookRenderOptions
options (Notebook NotebookMetadata
metadata [Cell]
cells) = [(Text, JSON)] -> JSON
JObject
  [ (Text
"cells", [JSON] -> JSON
JArray ([JSON] -> JSON) -> [JSON] -> JSON
forall a b. (a -> b) -> a -> b
$ CellSourceRenderStyle -> Cell -> JSON
cellToJSON (NotebookRenderOptions -> CellSourceRenderStyle
sourceFormat NotebookRenderOptions
options) (Cell -> JSON) -> [Cell] -> [JSON]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Cell]
cells)
  , (Text
"metadata", NotebookMetadata -> JSON
metadataToJSON NotebookMetadata
metadata)
  , (Text
"nbformat", Scientific -> JSON
JNumber (Scientific -> JSON) -> Scientific -> JSON
forall a b. (a -> b) -> a -> b
$ Int -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Scientific) -> Int -> Scientific
forall a b. (a -> b) -> a -> b
$ NotebookRenderOptions -> Int
notebookFormat NotebookRenderOptions
options)
  , (Text
"nbformat_minor", Scientific -> JSON
JNumber (Scientific -> JSON) -> Scientific -> JSON
forall a b. (a -> b) -> a -> b
$ Int -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Scientific) -> Int -> Scientific
forall a b. (a -> b) -> a -> b
$ NotebookRenderOptions -> Int
notebookFormatMinor NotebookRenderOptions
options)
  ]

-- | Render a notebook with the shared JSON renderer.
renderNotebook :: NotebookRenderOptions -> JSONRenderOptions -> Notebook -> Doc ann
renderNotebook :: forall ann.
NotebookRenderOptions -> JSONRenderOptions -> Notebook -> Doc ann
renderNotebook NotebookRenderOptions
options JSONRenderOptions
jsonOptions = JSONRenderOptions -> JSON -> Doc ann
forall ann. JSONRenderOptions -> JSON -> Doc ann
renderJSON JSONRenderOptions
jsonOptions (JSON -> Doc ann) -> (Notebook -> JSON) -> Notebook -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NotebookRenderOptions -> Notebook -> JSON
notebookToJSON NotebookRenderOptions
options

metadataToJSON :: NotebookMetadata -> JSON
metadataToJSON :: NotebookMetadata -> JSON
metadataToJSON NotebookMetadata
metadata = [(Text, JSON)] -> JSON
JObject
  [ (Text
"kernelspec", KernelSpec -> JSON
kernelToJSON (KernelSpec -> JSON) -> KernelSpec -> JSON
forall a b. (a -> b) -> a -> b
$ NotebookMetadata -> KernelSpec
notebookKernel NotebookMetadata
metadata)
  , (Text
"language_info", LanguageInfo -> JSON
languageToJSON (LanguageInfo -> JSON) -> LanguageInfo -> JSON
forall a b. (a -> b) -> a -> b
$ NotebookMetadata -> LanguageInfo
notebookLanguage NotebookMetadata
metadata)
  ]

kernelToJSON :: KernelSpec -> JSON
kernelToJSON :: KernelSpec -> JSON
kernelToJSON KernelSpec
kernel = [(Text, JSON)] -> JSON
JObject
  [ (Text
"display_name", Text -> JSON
JString (Text -> JSON) -> Text -> JSON
forall a b. (a -> b) -> a -> b
$ KernelSpec -> Text
kernelDisplayName KernelSpec
kernel)
  , (Text
"language", Text -> JSON
JString (Text -> JSON) -> Text -> JSON
forall a b. (a -> b) -> a -> b
$ KernelSpec -> Text
kernelLanguage KernelSpec
kernel)
  , (Text
"name", Text -> JSON
JString (Text -> JSON) -> Text -> JSON
forall a b. (a -> b) -> a -> b
$ KernelSpec -> Text
kernelName KernelSpec
kernel)
  ]

languageToJSON :: LanguageInfo -> JSON
languageToJSON :: LanguageInfo -> JSON
languageToJSON LanguageInfo
language = [(Text, JSON)] -> JSON
JObject
  [ (Text
"codemirror_mode", LanguageInfo -> JSON
languageCodeMirrorMode LanguageInfo
language)
  , (Text
"file_extension", Text -> JSON
JString (Text -> JSON) -> Text -> JSON
forall a b. (a -> b) -> a -> b
$ LanguageInfo -> Text
languageFileExtension LanguageInfo
language)
  , (Text
"mimetype", Text -> JSON
JString (Text -> JSON) -> Text -> JSON
forall a b. (a -> b) -> a -> b
$ LanguageInfo -> Text
languageMimeType LanguageInfo
language)
  , (Text
"name", Text -> JSON
JString (Text -> JSON) -> Text -> JSON
forall a b. (a -> b) -> a -> b
$ LanguageInfo -> Text
languageName LanguageInfo
language)
  , (Text
"nbconvert_exporter", Text -> JSON
JString (Text -> JSON) -> Text -> JSON
forall a b. (a -> b) -> a -> b
$ LanguageInfo -> Text
languageExporter LanguageInfo
language)
  , (Text
"pygments_lexer", Text -> JSON
JString (Text -> JSON) -> Text -> JSON
forall a b. (a -> b) -> a -> b
$ LanguageInfo -> Text
languageLexer LanguageInfo
language)
  , (Text
"version", Text -> JSON
JString (Text -> JSON) -> Text -> JSON
forall a b. (a -> b) -> a -> b
$ LanguageInfo -> Text
languageVersion LanguageInfo
language)
  ]

-- | Encode a new cell; code is unexecuted and has no outputs.
cellToJSON :: CellSourceRenderStyle -> Cell -> JSON
cellToJSON :: CellSourceRenderStyle -> Cell -> JSON
cellToJSON CellSourceRenderStyle
format (MarkdownCell [(Text, JSON)]
metadata Text
source) = [(Text, JSON)] -> JSON
JObject
  [ (Text
"cell_type", JSON
"markdown")
  , (Text
"metadata", [(Text, JSON)] -> JSON
JObject [(Text, JSON)]
metadata)
  , (Text
"source", CellSourceRenderStyle -> Text -> JSON
formatSource CellSourceRenderStyle
format Text
source)
  ]
cellToJSON CellSourceRenderStyle
format (CodeCell [(Text, JSON)]
metadata Text
source) = [(Text, JSON)] -> JSON
JObject
  [ (Text
"cell_type", JSON
"code")
  , (Text
"execution_count", JSON
JNull)
  , (Text
"metadata", [(Text, JSON)] -> JSON
JObject [(Text, JSON)]
metadata)
  , (Text
"outputs", [JSON] -> JSON
JArray [])
  , (Text
"source", CellSourceRenderStyle -> Text -> JSON
formatSource CellSourceRenderStyle
format Text
source)
  ]

-- | Line lists append LF to each line; single strings preserve the source verbatim.
formatSource :: CellSourceRenderStyle -> Text -> JSON
formatSource :: CellSourceRenderStyle -> Text -> JSON
formatSource CellSourceRenderStyle
format Text
source = case CellSourceRenderStyle
format of
  CellSourceRenderStyle
ListOfLines -> [JSON] -> JSON
JArray ([JSON] -> JSON) -> [JSON] -> JSON
forall a b. (a -> b) -> a -> b
$ Text -> JSON
JString (Text -> JSON) -> [Text] -> [JSON]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text]
sourceLines
  CellSourceRenderStyle
SingleString -> Text -> JSON
JString Text
source
  where
    sourceLines :: [Text]
sourceLines = (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n") (Text -> Text) -> [Text] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> [Text]
T.lines Text
source