Implement functions
This commit is contained in:
@@ -2,36 +2,51 @@ module Evaluation.Function
|
||||
( evalFunction
|
||||
) where
|
||||
|
||||
import Types (Function(..), Value(..))
|
||||
import Types (Function(..), Value(..), Template(..), fromValue)
|
||||
|
||||
import Data.Char (toLower)
|
||||
import qualified Data.Text as T
|
||||
|
||||
fileComponents :: String -> (String, String)
|
||||
fileComponents s =
|
||||
let (lastRev, firstRev) = span (/= '.') $ reverse s
|
||||
in case firstRev of
|
||||
_ : firstRev' -> (reverse firstRev', reverse lastRev)
|
||||
[] -> (reverse lastRev, "")
|
||||
|
||||
isImageExtension :: String -> Bool
|
||||
isImageExtension = (`elem` ["jpg"]) . map toLower
|
||||
|
||||
evalFunction :: Function -> Value -> Value
|
||||
evalFunction f x = case (f, x) of
|
||||
(AppendStrings, Tuple (String _, String _)) ->
|
||||
String undefined
|
||||
(AppendStrings, Tuple (String s0, String s1)) ->
|
||||
String (s0 ++ s1)
|
||||
|
||||
(ConcatStrings, List _) ->
|
||||
String undefined
|
||||
(ConcatStrings, List vs) ->
|
||||
String $ concatMap fromValue vs
|
||||
|
||||
(AppendTexts, Tuple (Text _, Text _)) ->
|
||||
Text undefined
|
||||
(AppendTexts, Tuple (Text t0, Text t1)) ->
|
||||
Text $ T.append t0 t1
|
||||
|
||||
(ConcatTexts, List _) ->
|
||||
Text undefined
|
||||
(ConcatTexts, List vs) ->
|
||||
Text $ T.concat $ map fromValue vs
|
||||
|
||||
(JoinPaths, Tuple (String _, String _)) ->
|
||||
String undefined
|
||||
(JoinPaths, Tuple (String s0, String s1)) ->
|
||||
String (s0 ++ "/" ++ s1)
|
||||
|
||||
(FileComponents, String _) ->
|
||||
Tuple (String undefined, String undefined)
|
||||
(FileComponents, String s) ->
|
||||
let (base, ext) = fileComponents s
|
||||
in Tuple (String base, String ext)
|
||||
|
||||
(IsImageExtension, String _) ->
|
||||
Bool undefined
|
||||
(HasImageExtension, String s) ->
|
||||
let (_, ext) = fileComponents s
|
||||
in Bool $ isImageExtension ext
|
||||
|
||||
(ApplyTemplate, Tuple (Template _, Text _)) ->
|
||||
Text undefined
|
||||
(ApplyTemplate, Tuple (Template (TemplateParts beforeContent afterContent), Text t)) ->
|
||||
Text $ T.concat [beforeContent, t, afterContent]
|
||||
|
||||
(ToText, String _) ->
|
||||
Text undefined
|
||||
(ToText, String s) ->
|
||||
Text $ T.pack s
|
||||
|
||||
_ ->
|
||||
error "unexpected combination of function and argument type"
|
||||
|
||||
@@ -2,30 +2,59 @@ module Evaluation.FunctionIO
|
||||
( evalFunctionIO
|
||||
) where
|
||||
|
||||
import Types (FunctionIO(..), Value(..))
|
||||
import Types.Values
|
||||
import Types (FunctionIO(..), Value(..), toValue)
|
||||
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.Lazy as TL
|
||||
import qualified Data.Text.IO as T
|
||||
import qualified Text.Pandoc as P
|
||||
import qualified Text.Blaze.Html.Renderer.Text as B
|
||||
import qualified Codec.Picture as CP
|
||||
import qualified Codec.Picture.STBIR as CPS
|
||||
import System.Directory (listDirectory, createDirectory, copyFile)
|
||||
|
||||
evalFunctionIO :: FunctionIO -> Value -> IO Value
|
||||
evalFunctionIO f x = case (f, x) of
|
||||
(ListDirectory, String _) ->
|
||||
pure $ List undefined
|
||||
(ListDirectory, String s) ->
|
||||
(List . map toValue) <$> listDirectory s
|
||||
|
||||
(ReadTemplate, String _) ->
|
||||
pure $ Template undefined
|
||||
(ReadTemplate, String s) -> do
|
||||
t <- T.readFile s
|
||||
let c = "CONTENT"
|
||||
(beforeContent, after) = T.breakOn c t
|
||||
afterContent = T.drop (T.length c) after
|
||||
pure $ Template $ TemplateParts beforeContent afterContent
|
||||
|
||||
(ConvertImage, Tuple (Tuple (String _, String _), ImageConversionSettings _)) ->
|
||||
pure $ Empty
|
||||
(ConvertImage, Tuple (Tuple (String source, String target),
|
||||
ImageConversionSettings (ResizeToWidth widthResized))) -> do
|
||||
imageOrig <- CP.readImage source
|
||||
let imageOrig' = case imageOrig of
|
||||
Left s -> error ("unexpected error: " ++ s)
|
||||
Right image -> CP.convertRGB8 image
|
||||
sizeFactor :: Double
|
||||
sizeFactor = fromIntegral (CP.imageWidth imageOrig') / fromIntegral widthResized
|
||||
heightResized = round (fromIntegral (CP.imageHeight imageOrig') / sizeFactor)
|
||||
imageResized = CPS.resize CPS.defaultOptions widthResized heightResized imageOrig'
|
||||
CP.saveJpgImage 90 target $ CP.ImageRGB8 imageResized
|
||||
pure Empty
|
||||
|
||||
(SaveFile, Tuple (Text _, String _)) ->
|
||||
pure $ Empty
|
||||
(SaveFile, Tuple (Text t, String s)) -> do
|
||||
T.writeFile s t
|
||||
pure Empty
|
||||
|
||||
(CopyFile, Tuple (String _, String _)) ->
|
||||
pure $ Empty
|
||||
(CopyFile, Tuple (String source, String target)) -> do
|
||||
copyFile source target
|
||||
pure Empty
|
||||
|
||||
(MakeDir, String _) ->
|
||||
pure $ Empty
|
||||
(MakeDir, String s) -> do
|
||||
createDirectory s
|
||||
pure Empty
|
||||
|
||||
(RunPandoc, String _) ->
|
||||
pure $ Text undefined
|
||||
(RunPandoc, String s) -> do
|
||||
contents <- T.readFile s
|
||||
html <- P.runIOorExplode (P.writeHtml5 P.def =<< P.readMarkdown P.def contents)
|
||||
pure $ Text $ TL.toStrict $ B.renderHtml html
|
||||
|
||||
_ ->
|
||||
error "unexpected combination of function and argument type"
|
||||
|
||||
Reference in New Issue
Block a user