Exemple #1
0
        private string RewriteUrls(string content, string path)
        {
            SassCompilationContext context = Context;

            var compiler = (SassCompiler)context.Compiler;

            path = compiler.GetBasePath(path, context.RootPath);

            return(compiler.RewriteUrls(content, path, context));
        }
Exemple #2
0
        public bool FileExists(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            SassCompilationContext context = Context;

            context.CancellationToken.ThrowIfCancellationRequested();

            Microsoft.Extensions.FileProviders.IFileInfo fileInfo = context.FileProvider.GetFileInfo(path);
            return(fileInfo.Exists && !fileInfo.IsDirectory);
        }
        public string ReadFile(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            SassCompilationContext context = Context;

            context.CancellationToken.ThrowIfCancellationRequested();

            using (Stream stream = context.FileProvider.GetFileInfo(path).CreateReadStream())
                using (var reader = new StreamReader(stream))
                    return(reader.ReadToEnd());
        }
Exemple #4
0
        public Task <SassCompilationResult> CompileAsync(string content, string virtualPathPrefix, string filePath, IFileProvider fileProvider, CancellationToken token)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            token.ThrowIfCancellationRequested();

            string rootPath;

            if (filePath != null)
            {
                var index = filePath.LastIndexOf('/');
                rootPath = filePath.Substring(0, index + 1);
            }
            else
            {
                filePath = rootPath = "/";
            }

            CompilationResult compilationResult;

            using (var context = new SassCompilationContext(this, rootPath, fileProvider, token))
            {
                content = RewriteUrls(content, string.Empty, context);

                try
                {
                    compilationResult = LibSassHost.SassCompiler.Compile(content, filePath);
                }
                catch (Exception ex)
                {
                    _logger.LogWarning($"Sass compilation of '{{FILEPATH}}' failed.{Environment.NewLine}{{REASON}}",
                                       (filePath ?? "(content)"),
                                       ex.Message);

                    compilationResult = null;
                }
            }

            return(Task.FromResult(
                       compilationResult != null ?
                       new SassCompilationResult(compilationResult.CompiledContent, compilationResult.IncludedFilePaths) :
                       SassCompilationResult.Failure));
        }
Exemple #5
0
 internal static void SetCompilationContext(SassCompilationContext context)
 {
     s_compilationContext.Value = context;
 }
Exemple #6
0
        protected internal virtual string RewriteUrls(string content, string basePath, SassCompilationContext context)
        {
            // 1) url values can usually be SASS expressions, there's no easy way to decide,
            // so only relative paths starting with "./" or "../" are rebased

            // 2) urls ending with '.css' must be rebased as they are not included
            // https://stackoverflow.com/questions/7111610/import-regular-css-file-in-scss-file

            return(s_rewriteUrlsRegex.Replace(content,
                                              m =>
            {
                var value = m.Groups["url"].Value;
                var quote = StringUtils.RemoveQuotes(ref value);

                return string.Concat(
                    m.Groups["before"].Value,
                    quote, RebaseUrl(value, basePath, context), quote,
                    m.Groups["after"].Value);
            }));
        }
Exemple #7
0
 protected internal virtual string RebaseUrl(string value, string basePath, SassCompilationContext context)
 {
     return(basePath + value);
 }