module Text.Sass.Functions.Internal
(
wrapFunction
, makeNativeFunction
, clearNativeFunction
, freeNativeFunction
, makeNativeFunctionList
, clearNativeFunctionList
, freeNativeFunctionList
, wrapImporter
, makeNativeImport
, freeNativeImport
, makeNativeImportList
, freeNativeImportList
, makeNativeImporter
, freeNativeImporter
, makeNativeImporterList
, freeNativeImporterList
) where
import qualified Bindings.Libsass as Lib
import Foreign
import Foreign.C
import Text.Sass.Functions
import Text.Sass.Utils
import Text.Sass.Values.Internal
wrapFunction :: SassFunctionType -> Lib.SassFunctionFnType
wrapFunction fn args _ _ = fromNativeValue args >>= fn >>= toNativeValue
makeNativeFunction :: SassFunction -> IO Lib.SassFunctionEntry
makeNativeFunction (SassFunction sig' fn) = do
sig <- newCString sig'
wrapped <- Lib.mkSassFunctionFn $ wrapFunction fn
Lib.sass_make_function sig wrapped nullPtr
clearNativeFunction :: Lib.SassFunctionEntry -> IO ()
clearNativeFunction entry = do
sig <- Lib.sass_function_get_signature entry
free sig
freeNativeFunction :: Lib.SassFunctionEntry -> IO ()
freeNativeFunction = free
makeNativeFunctionList :: [SassFunction] -> IO Lib.SassFunctionList
makeNativeFunctionList =
copyToCList Lib.sass_make_function_list makeNativeFunction pokeElemOff
clearNativeFunctionList :: Lib.SassFunctionList -> IO ()
clearNativeFunctionList = loopCList clearNativeFunction
freeNativeFunctionList :: Lib.SassFunctionList -> IO ()
freeNativeFunctionList = loopCList freeNativeFunction
wrapImporter :: SassImporterType -> Lib.SassImporterFnType
wrapImporter fn url _ _ = peekCString url >>= fn >>= \case
[] -> return nullPtr
xs -> makeNativeImportList xs
makeNativeImport :: SassImport -> IO Lib.SassImportEntry
makeNativeImport el = do
path <- maybeNew newCString $ importPath el
base <- maybeNew newCString $ importPath el
source <- maybeNew newCString $ importSource el
srcmap <- maybeNew newCString $ importSourceMap el
Lib.sass_make_import path base source srcmap
freeNativeImport :: Lib.SassImportEntry -> IO ()
freeNativeImport = Lib.sass_delete_import
makeNativeImportList :: [SassImport] -> IO Lib.SassImportList
makeNativeImportList =
copyToCList Lib.sass_make_import_list makeNativeImport pokeElemOff
freeNativeImportList :: Lib.SassImportList -> IO ()
freeNativeImportList = Lib.sass_delete_import_list
makeNativeImporter :: SassImporter -> IO Lib.SassImporterEntry
makeNativeImporter (SassImporter p func) = do
func' <- Lib.mkSassImporterFn $ wrapImporter func
Lib.sass_make_importer func' (realToFrac p) nullPtr
freeNativeImporter :: Lib.SassImporterEntry -> IO ()
freeNativeImporter = Lib.sass_delete_importer
makeNativeImporterList :: [SassImporter] -> IO Lib.SassImporterList
makeNativeImporterList =
copyToCList Lib.sass_make_importer_list makeNativeImporter pokeElemOff
freeNativeImporterList :: Lib.SassImporterList -> IO ()
freeNativeImporterList lst = loopCList freeNativeImporter lst >> free lst