Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Oct 28, 2024
1 parent 7bceea4 commit 714ea04
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 50 deletions.
34 changes: 14 additions & 20 deletions lib/Patat/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
module Patat.Eval
( parseEvalBlocks

, updateVar
, evalVar
, evalVars
, forceEval
, evalActiveVars
, evalAllVars
) where


--------------------------------------------------------------------------------
import qualified Control.Concurrent.Async as Async
import Control.Exception (IOException, catch, finally)
import Control.Monad (foldM, forever)
import Control.Monad.State (StateT, evalStateT, state)
import Control.Monad.State (StateT, runStateT, state)
import Control.Monad.Writer (Writer, runWriter, tell)
import Data.Foldable (for_)
import qualified Data.HashMap.Strict as HMS
Expand All @@ -36,8 +35,9 @@ import qualified Text.Pandoc.Definition as Pandoc
--------------------------------------------------------------------------------
parseEvalBlocks :: Presentation -> Presentation
parseEvalBlocks presentation =
let (pres, evalBlocks) = runWriter $ evalStateT work zeroVarGen in
pres {pEvalBlocks = evalBlocks}
let ((pres, varGen), evalBlocks) = runWriter $
runStateT work (pVarGen presentation) in
pres {pEvalBlocks = evalBlocks, pVarGen = varGen}
where
work = case psEval (pSettings presentation) of
Nothing -> pure presentation
Expand Down Expand Up @@ -108,12 +108,6 @@ evalBlock _ block =
pure [Append [block]]



--------------------------------------------------------------------------------
updateVar :: Var -> [Pandoc.Block] -> Presentation -> Presentation
updateVar var blocks pres = pres {pVars = HMS.insert var blocks $ pVars pres}


--------------------------------------------------------------------------------
evalVar :: Var -> ([Pandoc.Block] -> IO ()) -> Presentation -> IO Presentation
evalVar var writeOutput presentation = case HMS.lookup var evalBlocks of
Expand Down Expand Up @@ -162,17 +156,17 @@ evalVar var writeOutput presentation = case HMS.lookup var evalBlocks of


--------------------------------------------------------------------------------
evalVars
:: Foldable f
=> f Var -> (Var -> [Pandoc.Block] -> IO ()) -> Presentation
-> IO Presentation
evalVars vars update presentation =
foldM (\p var -> evalVar var (update var) p) presentation vars
evalActiveVars
:: (Var -> [Pandoc.Block] -> IO ()) -> Presentation -> IO Presentation
evalActiveVars update presentation = foldM
(\p var -> evalVar var (update var) p)
presentation
(activeVars presentation)


--------------------------------------------------------------------------------
forceEval :: Presentation -> IO Presentation
forceEval pres = do
evalAllVars :: Presentation -> IO Presentation
evalAllVars pres = do
updates <- IORef.newIORef []

let forceEvalVar pres0 var = do
Expand Down
10 changes: 4 additions & 6 deletions lib/Patat/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ main = do
OA.parserFailure parserPrefs parserInfo
(OA.ShowHelpText Nothing) mempty

errOrPres <- readPresentation filePath
errOrPres <- readPresentation zeroVarGen filePath
pres <- either (errorAndExit . return) return errOrPres
let settings = pSettings pres

unless (oForce options) assertAnsiFeatures

if oDump options then
EncodingFallback.withHandle IO.stdout (pEncodingFallback pres) $ do
Eval.forceEval pres >>= dumpPresentation
Eval.evalAllVars pres >>= dumpPresentation
else
-- (Maybe) initialize images backend.
withMaybeHandle Images.withHandle (psImages settings) $ \images ->
Expand Down Expand Up @@ -211,10 +211,8 @@ loop app@App {..} = do
(activeSpeakerNotes aPresentation)

-- Start necessary eval blocks
presentation <- Eval.evalVars
(activeVars aPresentation)
(\var blocks -> Chan.writeChan aCommandChan $ PresentationCommand $
UpdateVar (pVersion aPresentation) var blocks)
presentation <- Eval.evalActiveVars
(\v -> Chan.writeChan aCommandChan . PresentationCommand . UpdateVar v)
aPresentation

size <- getPresentationSize presentation
Expand Down
5 changes: 5 additions & 0 deletions lib/Patat/Presentation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module Patat.Presentation
( PresentationSettings (..)
, defaultPresentationSettings

, VarGen
, Var
, zeroVarGen

, Presentation (..)
, readPresentation

Expand All @@ -23,6 +27,7 @@ module Patat.Presentation
) where

import Patat.Presentation.Display
import Patat.Presentation.Instruction
import Patat.Presentation.Interactive
import Patat.Presentation.Internal
import Patat.Presentation.Read
1 change: 0 additions & 1 deletion lib/Patat/Presentation/Display.hs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ dumpPresentation pres@Presentation {..} =
dumpFragment :: Index -> [PP.Doc]
dumpFragment idx =
case displayPresentation (getSize idx) pres {pActiveFragment = idx} of
-- TODO: wait until everything is evaluated before dumping.
DisplayDoc doc -> [doc]
DisplayImage filepath -> [PP.string $ "{image: " ++ filepath ++ "}"]

Expand Down
13 changes: 4 additions & 9 deletions lib/Patat/Presentation/Interactive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module Patat.Presentation.Interactive

--------------------------------------------------------------------------------
import Data.Char (isDigit)
import qualified Patat.Eval as Eval
import Patat.Presentation.Instruction (Var)
import Patat.Presentation.Internal
import Patat.Presentation.Read
Expand All @@ -34,7 +33,7 @@ data PresentationCommand
| Last
| Reload
| Seek Int
| UpdateVar Version Var [Pandoc.Block]
| UpdateVar Var [Pandoc.Block]
| UnknownCommand String
deriving (Eq, Show)

Expand Down Expand Up @@ -110,14 +109,11 @@ updatePresentation cmd presentation = case cmd of
Seek n -> pure $ goToSlide $ \_ -> (n - 1, 0)
Reload -> reloadPresentation
UnknownCommand _ -> pure $ UpdatedPresentation presentation
UpdateVar v x b -> pure $ UpdatedPresentation $
if v /= version then presentation else Eval.updateVar x b presentation
UpdateVar v b -> pure $ UpdatedPresentation $ updateVar v b presentation
where
numSlides :: Presentation -> Int
numSlides pres = length (pSlides pres)

version = pVersion presentation

clip :: Index -> Presentation -> Index
clip (slide, fragment) pres
| slide >= numSlides pres = (numSlides pres - 1, lastFragments - 1)
Expand All @@ -141,10 +137,9 @@ updatePresentation cmd presentation = case cmd of
}

reloadPresentation = do
errOrPres <- readPresentation (pFilePath presentation)
errOrPres <- readPresentation (pVarGen presentation) (pFilePath presentation)
return $ case errOrPres of
Left err -> ErroredPresentation err
Right pres -> UpdatedPresentation $ pres
{ pVersion = Version (unVersion version + 1)
, pActiveFragment = clip (pActiveFragment presentation) pres
{ pActiveFragment = clip (pActiveFragment presentation) pres
}
16 changes: 9 additions & 7 deletions lib/Patat/Presentation/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Patat.Presentation.Internal
( Version (..)
, Breadcrumbs
( Breadcrumbs
, Presentation (..)
, PresentationSettings (..)
, defaultPresentationSettings
Expand Down Expand Up @@ -39,6 +38,8 @@ module Patat.Presentation.Internal

, Size
, getPresentationSize

, updateVar
) where


Expand All @@ -61,18 +62,13 @@ import qualified Skylighting as Skylighting
import qualified Text.Pandoc as Pandoc


--------------------------------------------------------------------------------
newtype Version = Version {unVersion :: Int} deriving (Eq, Show)


--------------------------------------------------------------------------------
type Breadcrumbs = [(Int, [Pandoc.Inline])]


--------------------------------------------------------------------------------
data Presentation = Presentation
{ pFilePath :: !FilePath
, pVersion :: !Version
, pEncodingFallback :: !EncodingFallback
, pTitle :: ![Pandoc.Inline]
, pAuthor :: ![Pandoc.Inline]
Expand All @@ -84,6 +80,7 @@ data Presentation = Presentation
, pActiveFragment :: !Index
, pSyntaxMap :: !Skylighting.SyntaxMap
, pEvalBlocks :: !Eval.EvalBlocks
, pVarGen :: !Instruction.VarGen
, pVars :: !(HMS.HashMap Instruction.Var [Pandoc.Block])
}

Expand Down Expand Up @@ -203,3 +200,8 @@ getPresentationSize pres = do
pure $ Size {sRows = rows, sCols = cols}
where
settings = activeSettings pres


--------------------------------------------------------------------------------
updateVar :: Instruction.Var -> [Pandoc.Block] -> Presentation -> Presentation
updateVar var blocks pres = pres {pVars = HMS.insert var blocks $ pVars pres}
14 changes: 7 additions & 7 deletions lib/Patat/Presentation/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import qualified Patat.Eval as Eval
import qualified Patat.Presentation.Comments as Comments
import Patat.Presentation.Fragment
import qualified Patat.Presentation.Instruction as Instruction
import Patat.Presentation.Instruction (VarGen)
import Patat.Presentation.Internal
import Patat.Transition (parseTransitionSettings)
import Prelude
Expand All @@ -47,8 +48,8 @@ import qualified Text.Pandoc.Extended as Pandoc


--------------------------------------------------------------------------------
readPresentation :: FilePath -> IO (Either String Presentation)
readPresentation filePath = runExceptT $ do
readPresentation :: VarGen -> FilePath -> IO (Either String Presentation)
readPresentation varGen filePath = runExceptT $ do
-- We need to read the settings first.
(enc, src) <- liftIO $ EncodingFallback.readFile filePath
homeSettings <- ExceptT readHomeSettings
Expand All @@ -71,7 +72,7 @@ readPresentation filePath = runExceptT $ do
Right x -> return x

pres <- ExceptT $ pure $
pandocToPresentation filePath enc settings syntaxMap doc
pandocToPresentation varGen filePath enc settings syntaxMap doc
pure $ Eval.parseEvalBlocks pres
where
ext = takeExtension filePath
Expand Down Expand Up @@ -122,12 +123,11 @@ readExtension (ExtensionList extensions) fileExt = case fileExt of

--------------------------------------------------------------------------------
pandocToPresentation
:: FilePath -> EncodingFallback -> PresentationSettings
:: VarGen -> FilePath -> EncodingFallback -> PresentationSettings
-> Skylighting.SyntaxMap -> Pandoc.Pandoc -> Either String Presentation
pandocToPresentation pFilePath pEncodingFallback pSettings pSyntaxMap
pandocToPresentation pVarGen pFilePath pEncodingFallback pSettings pSyntaxMap
pandoc@(Pandoc.Pandoc meta _) = do
let !pVersion = Version 0
!pTitle = case Pandoc.docTitle meta of
let !pTitle = case Pandoc.docTitle meta of
[] -> [Pandoc.Str . T.pack . snd $ splitFileName pFilePath]
title -> title
!pSlides = pandocToSlides pSettings pandoc
Expand Down

0 comments on commit 714ea04

Please sign in to comment.