Also copy fonts

Do some ugly shenanigans to make this work.
This commit is contained in:
2024-10-05 17:35:47 +02:00
parent 47d086c115
commit 8c2f522bbf
7 changed files with 105 additions and 33 deletions

View File

@@ -2,28 +2,36 @@ module Evaluation.Function
( evalFunction
) where
import Types (Function(..), Value(..), Template(..), fromValue)
import Prelude hiding (String, FilePath)
import qualified Prelude
import Types (Function(..), Value(..), String(..), Template(..), fromValue)
import Data.Char (toLower)
import qualified Data.Text as T
fileComponents :: String -> (String, String)
fileComponents :: Prelude.String -> (Prelude.String, Prelude.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
makeString :: Prelude.String -> Value
makeString = String . StringWrapper
unStringWrapper :: Value -> Prelude.String
unStringWrapper = \case
String (StringWrapper s) -> s
_ -> error "unexpected"
evalFunction :: Function -> Value -> Value
evalFunction f x = case (f, x) of
(AppendStrings, Tuple (String s0, String s1)) ->
String (s0 ++ s1)
(AppendStrings, Tuple (String (StringWrapper s0), String (StringWrapper s1))) ->
makeString (s0 ++ s1)
(ConcatStrings, List vs) ->
String $ concatMap fromValue vs
makeString $ concatMap unStringWrapper vs
(AppendTexts, Tuple (Text t0, Text t1)) ->
Text $ T.append t0 t1
@@ -31,21 +39,23 @@ evalFunction f x = case (f, x) of
(ConcatTexts, List vs) ->
Text $ T.concat $ map fromValue vs
(JoinPaths, Tuple (String s0, String s1)) ->
String (s0 ++ "/" ++ s1)
(JoinPaths, Tuple (String (StringWrapper s0), String (StringWrapper s1))) ->
makeString (s0 ++ "/" ++ s1)
(FileComponents, String s) ->
(FileComponents, String (StringWrapper s)) ->
let (base, ext) = fileComponents s
in Tuple (String base, String ext)
in Tuple (makeString base, makeString ext)
(HasImageExtension, String s) ->
let (_, ext) = fileComponents s
in Bool $ isImageExtension ext
(LowerString, String (StringWrapper s)) ->
makeString $ map toLower s
(ElemOf, Tuple (y, List ys)) ->
Bool (y `elem` ys)
(ApplyTemplate, Tuple (Template (TemplateParts beforeContent afterContent), Text t)) ->
Text $ T.concat [beforeContent, t, afterContent]
(ToText, String s) ->
(ToText, String (StringWrapper s)) ->
Text $ T.pack s
_ ->

View File

@@ -2,6 +2,8 @@ module Evaluation.FunctionIO
( evalFunctionIO
) where
import Prelude hiding (String, FilePath)
import Types.Values
import Types (FunctionIO(..), Value(..), toValue)
@@ -16,20 +18,20 @@ import System.Directory (listDirectory, doesDirectoryExist, createDirectory, cop
evalFunctionIO :: FunctionIO -> Value -> IO Value
evalFunctionIO f x = case (f, x) of
(ListDirectory, String s) ->
(List . map toValue) <$> listDirectory s
(ListDirectory, String (StringWrapper s)) ->
(List . map (toValue . StringWrapper)) <$> listDirectory s
(IsDirectory, String s) ->
(IsDirectory, String (StringWrapper s)) ->
Bool <$> doesDirectoryExist s
(ReadTemplate, String s) -> do
(ReadTemplate, String (StringWrapper 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 source, String target),
(ConvertImage, Tuple (Tuple (String (StringWrapper source), String (StringWrapper target)),
ImageConversionSettings (ResizeToWidth widthResized))) -> do
imageOrig <- CP.readImage source
let imageOrig' = case imageOrig of
@@ -42,19 +44,19 @@ evalFunctionIO f x = case (f, x) of
CP.saveJpgImage 90 target $ CP.ImageRGB8 imageResized
pure Empty
(SaveFile, Tuple (Text t, String s)) -> do
(SaveFile, Tuple (Text t, String (StringWrapper s))) -> do
T.writeFile s t
pure Empty
(CopyFile, Tuple (String source, String target)) -> do
(CopyFile, Tuple (String (StringWrapper source), String (StringWrapper target))) -> do
copyFile source target
pure Empty
(MakeDir, String s) -> do
(MakeDir, String (StringWrapper s)) -> do
createDirectory s
pure Empty
(RunPandoc, String s) -> do
(RunPandoc, String (StringWrapper s)) -> do
contents <- T.readFile s
html <- P.runIOorExplode (P.writeHtml5 P.def =<< P.readMarkdown (P.def { P.readerExtensions = P.extensionsFromList [ P.Ext_raw_html ] }) contents)
pure $ Text $ TL.toStrict $ B.renderHtml html