Skip to content

Commit

Permalink
Fix wrong interpretation of SassImport
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
jakubfijalkowski committed Nov 25, 2018
1 parent e24db82 commit 5127660
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
34 changes: 19 additions & 15 deletions Text/Sass/Functions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,30 @@ data SassFunction = SassFunction {
-- | Represents a sass import - a sass content with additional metadata.
--
-- Even though this ADT has four fields, you may just provide either
-- 'importPath' and 'importBase' and leave loading to the library, or provide
-- 'importSource' and do not provide 'importPath' nor 'importBase'.
-- 'importPath' and 'importAbsolutePath' and leave loading to the library, or
-- provide 'importSource' and do not provide 'importAbsolutePath'.
-- Nevertheless, you are free to provide all of the fields.
data SassImport = SassImport {
importPath :: Maybe FilePath, -- ^ Path to the import, relative to base.
importBase :: Maybe FilePath, -- ^ Base path.
importSource :: Maybe String, -- ^ Import's source.
importSourceMap :: Maybe String -- ^ Source map of the import.
-- | Path to the import, as requested by the import statement.
importPath :: Maybe FilePath
-- | Absolute path to the file.
, importAbsolutePath :: Maybe FilePath
-- | Import's source.
, importSource :: Maybe String
-- | Source map of the import.
, importSourceMap :: Maybe String
}

-- | Type of the function that acts like an importer.
--
-- You may return the empty list in order to tell libsass to handle the import by
-- itself.
type SassImporterType =
String
-- ^ Path to the import that needs to be loaded.
-> String
-- ^ Absolute path to the importing file.
-> IO [SassImport] -- ^ Imports.
String
-- ^ Path to the import that needs to be loaded.
-> String
-- ^ Absolute path to the importing file.
-> IO [SassImport] -- ^ Imports.

-- | Description of the importer.
data SassImporter = SassImporter {
Expand All @@ -75,7 +79,7 @@ data SassHeader = SassHeader {
makeSourceImport :: String -> SassImport
makeSourceImport s = SassImport Nothing Nothing (Just s) Nothing

-- | 'makePathImport' @p b@ is equivalent to 'SassImport'
-- @(Just p) (Just b) Nothing Nothing@.
makePathImport :: String -> String -> SassImport
makePathImport p b = SassImport (Just p) (Just b) Nothing Nothing
-- | 'makePathImport' @p@ is equivalent to 'SassImport'
-- @(Just p) (Just p) Nothing Nothing@.
makePathImport :: String -> SassImport
makePathImport p = SassImport (Just p) (Just p) Nothing Nothing
12 changes: 6 additions & 6 deletions Text/Sass/Functions/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ wrapImporter fn url _ compiler = do

-- | Converts 'SassImport' into native representation.
makeNativeImport :: SassImport -> IO Lib.SassImportEntry
makeNativeImport el = do
path <- maybeNew newUTF8CString $ importPath el
base <- maybeNew newUTF8CString $ importPath el
source <- maybeNew newUTF8CString $ importSource el
srcmap <- maybeNew newUTF8CString $ importSourceMap el
Lib.sass_make_import path base source srcmap
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

-- | Frees native representation of 'SassImport'.
freeNativeImport :: Lib.SassImportEntry -> IO ()
Expand Down
10 changes: 5 additions & 5 deletions Text/Sass/Marshal/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ import qualified GHC.Foreign as GHC
import qualified GHC.IO.Encoding as E

-- | Marshal a NUL terminated C string (UTF-8 encoded) into Haskell string.
-- | It is equivalent to 'peekCString' but with different encoding.
-- It is equivalent to 'peekCString' but with different encoding.
peekUTF8CString :: CString -> IO String
peekUTF8CString = GHC.peekCString E.utf8

-- | Marshal a Haskell string into a NUL terminated C string (UTF-8 encoded).
-- | It is equivalent to 'newCString' but with different encoding.
-- It is equivalent to 'newCString' but with different encoding.
newUTF8CString :: String -> IO CString
newUTF8CString = GHC.newCString E.utf8

-- | Marshal a Haskell string into a C string (ie, character array)
-- | Marshal a Haskell string into a C string (i.e., character array)
-- in temporary storage, with explicit length information.
-- | It is equivalent to 'withCString' but with different encoding.
-- It is equivalent to 'withCString' but with different encoding.
withUTF8CString :: String -> (CString -> IO a) -> IO a
withUTF8CString = GHC.withCString E.utf8

-- | Copies 'ByteString' to newly allocated 'CString'. The result must be
-- | explicitly freed using 'free' or 'finalizerFree'.
-- explicitly freed using 'free' or 'finalizerFree'.
newCStringFromBS :: ByteString -> IO CString
newCStringFromBS (BI.PS fp o l) = do
buf <- mallocBytes (l + 1)
Expand Down
2 changes: 1 addition & 1 deletion Text/Sass/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ modify' f = do
put $! f s
#endif

-- | 'withOptionalCString' @str action@, if @str@ is 'Nothing', @action@ is not
-- | 'withOptionalCString' @str action@, if @str@ is @Nothing@, @action@ is not
-- invoked, otherwise behaves like 'withCString'.
withOptionalUTF8CString :: Maybe String -> (CString -> IO ()) -> IO ()
withOptionalUTF8CString (Just str) action = withUTF8CString str action
Expand Down
2 changes: 1 addition & 1 deletion test/Text/Sass/EncodingSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ nonUTFNameSig = SassFunction "zażółcić()" returnFunc

importFunction :: FilePath -> String -> String -> IO [SassImport]
importFunction _ "src" _ = return [makeSourceImport testInput]
importFunction p "path" _ = return [makePathImport p "."]
importFunction p "path" _ = return [makePathImport p]
importFunction _ _ _ = fail "Unknown import"

importers :: FilePath -> [SassImporter]
Expand Down

0 comments on commit 5127660

Please sign in to comment.