module Text.Sass.Functions.Internal
(
wrapFunction
, makeNativeFunction
, freeNativeFunction
, makeNativeFunctionList
, freeNativeFunctionList
, wrapImporter
, makeNativeImport
, freeNativeImport
, makeNativeImportList
, freeNativeImportList
, makeNativeImporter
, freeNativeImporter
, makeNativeImporterList
, freeNativeImporterList
) where
import qualified Bindings.Libsass as Lib
import Foreign
import Text.Sass.Functions
import Text.Sass.Marshal.Internal
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 <- newUTF8CString sig'
wrapped <- Lib.mkSassFunctionFn $ wrapFunction fn
Lib.sass_make_function sig wrapped nullPtr
freeNativeFunction :: Lib.SassFunctionEntry -> IO ()
freeNativeFunction = Lib.sass_delete_function
makeNativeFunctionList :: [SassFunction] -> IO Lib.SassFunctionList
makeNativeFunctionList =
copyToCList Lib.sass_make_function_list makeNativeFunction pokeElemOff
freeNativeFunctionList :: Lib.SassFunctionList -> IO ()
freeNativeFunctionList = Lib.sass_delete_function_list
wrapImporter :: SassImporterType -> Lib.SassImporterFnType
wrapImporter fn url _ compiler = do
lastImport <- Lib.sass_compiler_get_last_import compiler
absPath <- Lib.sass_import_get_abs_path lastImport >>= peekUTF8CString
url' <- peekUTF8CString url
importList <- fn url' absPath
case importList of
[] -> return nullPtr
xs -> makeNativeImportList xs
makeNativeImport :: SassImport -> IO Lib.SassImportEntry
makeNativeImport el =
maybeWith withUTF8CString (importPath el) $ \path ->
maybeWith withUTF8CString (importAbsolutePath el) $ \absPath -> do
source <- maybeNew newUTF8CString $ importSource el
srcmap <- maybeNew newUTF8CString $ importSourceMap el
Lib.sass_make_import path absPath 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