Пример #1
0
        /// <summary>
        /// Transforms the content of the given string.
        /// </summary>
        /// <param name="input">The input string to transform.</param>
        /// <param name="path">The path to the given input string to transform.</param>
        /// <param name="cruncher">The cruncher that is running the transform.</param>
        /// <returns>The transformed string.</returns>
        public string Transform(string input, string path, CruncherBase cruncher)
        {
            try
            {
                var options = new LibSass.Compiler.Options.SassOptions
                {
                    InputPath        = path,
                    OutputStyle      = LibSass.Compiler.Options.SassOutputStyle.Expanded,
                    Precision        = 5,
                    IsIndentedSyntax = System.IO.Path.GetExtension(path).Equals(".sass", StringComparison.OrdinalIgnoreCase)
                };
                var compiler = new LibSass.Compiler.SassCompiler(options);
                var result   = compiler.Compile();

                foreach (var file in result.IncludedFiles)
                {
                    cruncher.AddFileMonitor(file, "not empty");
                }

                return(result.Output);
            }
            catch (Exception ex)
            {
                throw new SassAndScssCompilingException(ex.Message, ex.InnerException);
            }
        }
Пример #2
0
        /// <summary>
        /// Transforms the content of the given string from Less into CSS.
        /// </summary>
        /// <param name="input">The input string to transform.</param>
        /// <param name="path">The path to the given input string to transform.</param>
        /// <param name="cruncher">The cruncher that is running the transform.</param>
        /// <returns>The transformed string.</returns>
        public string Transform(string input, string path, CruncherBase cruncher)
        {
            // The standard engine returns a FileNotFoundExecption so I've rolled my own path resolver.
            Parser parser = new Parser();
            DotLessPathResolver dotLessPathResolver = new DotLessPathResolver(path);
            FileReader          fileReader          = new FileReader(dotLessPathResolver);

            parser.Importer = new Importer(fileReader);
            ILessEngine lessEngine = new LessEngine(parser);

            try
            {
                string result = lessEngine.TransformToCss(input, path);

                if (cruncher.Options.CacheFiles)
                {
                    // Add each import as a file dependency so that the cache will clean itself.
                    IEnumerable <string> imports    = lessEngine.GetImports();
                    IList <string>       enumerable = imports as IList <string> ?? imports.ToList();

                    if (enumerable.Any())
                    {
                        foreach (string import in enumerable)
                        {
                            if (!import.Contains("://"))
                            {
                                string filePath =
                                    HostingEnvironment.MapPath(
                                        VirtualPathUtility.Combine(dotLessPathResolver.CurrentFileDirectory, import));

                                cruncher.AddFileMonitor(filePath, "not empty");
                            }
                        }
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                throw new LessCompilingException(ex.Message, ex.InnerException);
            }
        }
Пример #3
0
        /// <summary>
        /// Parses the string for CSS imports and replaces them with the referenced CSS.
        /// </summary>
        /// <param name="sass">
        /// The SASS to parse for import statements.
        /// </param>
        /// <param name="cruncher">The cruncher that is running the transform.</param>
        private void AddImportsToFileMonitors(string sass, CruncherBase cruncher)
        {
            // Check for imports and parse if necessary.
            if (!sass.Contains("@import", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            // Recursively parse the sass for imports.
            foreach (Match match in ImportsRegex.Matches(sass))
            {
                // Recursively parse the sass for imports.
                GroupCollection   groups       = match.Groups;
                CaptureCollection fileCaptures = groups["filename"].Captures;

                if (fileCaptures.Count > 0)
                {
                    string fileName = fileCaptures[0].ToString();

                    if (!fileName.Contains("://"))
                    {
                        // Check and add the @import the match.
                        FileInfo fileInfo =
                            new FileInfo(Path.GetFullPath(Path.Combine(cruncher.Options.RootFolder, fileName)));

                        // Read the file.
                        if (fileInfo.Exists)
                        {
                            string file = fileInfo.FullName;

                            using (StreamReader reader = new StreamReader(file))
                            {
                                // Recursively check the children.
                                this.AddImportsToFileMonitors(reader.ReadToEnd(), cruncher);
                            }

                            // Cache if applicable.
                            cruncher.AddFileMonitor(file, "not empty");
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Transforms the content of the given string from Less into CSS. 
        /// </summary>
        /// <param name="input">The input string to transform.</param>
        /// <param name="path">The path to the given input string to transform.</param>
        /// <param name="cruncher">The cruncher that is running the transform.</param>
        /// <returns>The transformed string.</returns>
        public string Transform(string input, string path, CruncherBase cruncher)
        {
            // The standard engine returns a FileNotFoundExecption so I've rolled my own path resolver.
            Parser parser = new Parser();
            DotLessPathResolver dotLessPathResolver = new DotLessPathResolver(path);
            FileReader fileReader = new FileReader(dotLessPathResolver);
            parser.Importer = new Importer(fileReader);
            ILessEngine lessEngine = new LessEngine(parser);

            try
            {
                string result = lessEngine.TransformToCss(input, path);

                if (cruncher.Options.CacheFiles)
                {
                    // Add each import as a file dependency so that the cache will clean itself.
                    IEnumerable<string> imports = lessEngine.GetImports();
                    IList<string> enumerable = imports as IList<string> ?? imports.ToList();

                    if (enumerable.Any())
                    {
                        foreach (string import in enumerable)
                        {
                            if (!import.Contains(Uri.SchemeDelimiter))
                            {
                                string filePath =
                                    HostingEnvironment.MapPath(VirtualPathUtility.Combine(dotLessPathResolver.CurrentFileDirectory, import));

                                cruncher.AddFileMonitor(filePath, "not empty");
                            }
                        }
                    }
                }

                return result;
            }
            catch (Exception ex)
            {
                throw new LessCompilingException(ex.Message, ex.InnerException);
            }
        }
        /// <summary>
        /// Parses the string for CSS imports and replaces them with the referenced CSS.
        /// </summary>
        /// <param name="sass">
        /// The SASS to parse for import statements.
        /// </param>
        /// <param name="cruncher">The cruncher that is running the transform.</param>
        private void AddImportsToFileMonitors(string sass, CruncherBase cruncher)
        {
            // Check for imports and parse if necessary.
            if (!sass.Contains("@import", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            // Recursively parse the sass for imports.
            foreach (Match match in ImportsRegex.Matches(sass))
            {
                // Recursively parse the sass for imports.
                GroupCollection groups = match.Groups;
                CaptureCollection fileCaptures = groups["filename"].Captures;

                if (fileCaptures.Count > 0)
                {
                    string fileName = fileCaptures[0].ToString();

                    if (!fileName.Contains("://"))
                    {
                        // Check and add the @import the match.
                        FileInfo fileInfo =
                            new FileInfo(Path.GetFullPath(Path.Combine(cruncher.Options.RootFolder, fileName)));

                        // Read the file.
                        if (fileInfo.Exists)
                        {
                            string file = fileInfo.FullName;

                            using (StreamReader reader = new StreamReader(file))
                            {
                                // Recursively check the children.
                                this.AddImportsToFileMonitors(reader.ReadToEnd(), cruncher);
                            }

                            // Cache if applicable.
                            cruncher.AddFileMonitor(file, "not empty");
                        }
                    }
                }
            }
        }