コード例 #1
0
        public static CompileResult CompileLess(string file, string contents, string theme, Dictionary<string, string> themeLessVariables)
        {
            if (string.IsNullOrEmpty(theme))
                theme = "Default";

            var importedFilePaths = new HashSet<string>();
            var logger = new ThemedLessLogger(theme);
            var engine = new LessEngine(
                new Parser(new ConsoleStylizer(), new Importer(importedFilePaths, file, theme)),
                logger, false, false)
            {
                Plugins = new List<IPluginConfigurator>
                {
                    new VariableOverridePluginConfigurator(themeLessVariables)
                }
            };
            if (string.IsNullOrEmpty(contents))
            {
                using (var sr = new StreamReader(System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(file).Open()))
                {
                    contents = sr.ReadToEnd();
                }
            }
            var result = engine.TransformToCss(contents, file);
            if (logger.LoggedErrors.Any())
            {
                throw new Exception("Error(s) compiling less. " +
                                    string.Join(" - ", logger.LoggedErrors.Select(x => "\"" + x + "\"").ToArray()));
            }
            return new CompileResult(result, importedFilePaths);
        }
コード例 #2
0
        public static CompileResult CompileLess(string file, string contents, string theme, Dictionary <string, string> themeLessVariables)
        {
            if (string.IsNullOrEmpty(theme))
            {
                theme = "Default";
            }

            var importedFilePaths = new HashSet <string>();
            var logger            = new ThemedLessLogger(theme);
            var engine            = new LessEngine(
                new Parser(new ConsoleStylizer(), new Importer(importedFilePaths, file, theme)),
                logger, false, false)
            {
                Plugins = new List <IPluginConfigurator>
                {
                    new VariableOverridePluginConfigurator(themeLessVariables)
                }
            };

            if (string.IsNullOrEmpty(contents))
            {
                using (var sr = new StreamReader(System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(file).Open()))
                {
                    contents = sr.ReadToEnd();
                }
            }
            var result = engine.TransformToCss(contents, file);

            if (logger.LoggedErrors.Any())
            {
                throw new Exception("Error(s) compiling less. " +
                                    string.Join(" - ", logger.LoggedErrors.Select(x => "\"" + x + "\"").ToArray()));
            }
            return(new CompileResult(result, importedFilePaths));
        }