示例#1
0
        public static string render(string code, Action <int, int, string> onerror = null)
        {
            var js = new JavaScript(@"
                less.Parser().parse(external.Get('code'), function(err, tree){
                    console.log('done');
                    if(err) {
                        external.AddMessage(err.line || 0, err.column || 0, 2, err.message + ': ' err.extract.join(' '));
                    } else {
                        external.Set('result', tree.toCSS());
                    }
                    external.Finished();
                });
                ", new Dictionary <string, object> {
                { "code", code }
            },
                                    new JavaScript.Requirement {
                Path = "https://raw.github.com/cloudhead/less.js/master/dist/less-1.1.4.js", PostSource = "window.less = less;"
            });

            js.AutoFinish = false;
            js.Execute();

            if (onerror != null)
            {
                foreach (var msg in js.Messages)
                {
                    if (msg.Type == JavaScript.Message.Types.Error)
                    {
                        onerror(msg.Line + 1, msg.Column, msg.Content);
                    }
                }
            }

            return(js.Get("result") as string);
        }
示例#2
0
        public static string render(string code, Action<int, int, string> onerror = null)
        {
            var js = new JavaScript(@"
                less.Parser().parse(external.Get('code'), function(err, tree){
                    console.log('done');
                    if(err) {
                        external.AddMessage(err.line || 0, err.column || 0, 2, err.message + ': ' err.extract.join(' '));
                    } else {
                        external.Set('result', tree.toCSS());
                    }
                    external.Finished();
                });
                ", new Dictionary<string, object> { { "code", code } },
                  new JavaScript.Requirement { Path = "https://raw.github.com/cloudhead/less.js/master/dist/less-1.1.4.js", PostSource = "window.less = less;" });

            js.AutoFinish = false;
            js.Execute();

            if (onerror != null) {
                foreach (var msg in js.Messages) {
                    if (msg.Type == JavaScript.Message.Types.Error) {
                        onerror(msg.Line + 1, msg.Column, msg.Content);
                    }
                }
            }

            return js.Get("result") as string;
        }
示例#3
0
        public static string js_beautify(string code, options options = null)
        {
            var data = new Dictionary <string, object> {
                { "code", code },
                { "options", options },
            };
            var optionCode = "";
            var moptions   = rxDetectOptions.Match(code);

            if (moptions.Success)
            {
                optionCode = moptions.Groups[1].Value;
                code       = code.Remove(moptions.Index, moptions.Length);
            }
            else if (options != null)
            {
                foreach (var prop in typeof(options).GetProperties())
                {
                    optionCode += "\r\n\t'" + prop.Name + "': options0['" + prop.Name + "'],";
                }
            }

            var js = JavaScript.Execute(@"var options0 = external.Get('options'), options1 = {
                        " + optionCode.TrimEnd(',') + @"
                    };
                    external.Set('result', js_beautify(external.Get('code'), options1));",
                                        new Dictionary <string, object> {
                { "code", code }, { "options", options }
            },
                                        new JavaScript.Requirement {
                Path = "http://jsbeautifier.org/beautify.js", PostSource = "window.js_beautify = js_beautify;"
            });

            return(js.Get("result") as string);
        }
示例#4
0
        public static string compile(string source, options options = null)
        {
            var script   = "external.Set('result', CoffeeScript.compile(external.Get('source'), options));";
            var moptions = rxDetectOptions.Match(source);

            if (moptions.Success)
            {
                script = "var options = { " + moptions.Groups[1].Value + "}; " + script;
                source = source.Remove(moptions.Index, moptions.Length);
            }
            else if (options != null)
            {
                script = string.Format("var options = {{ 'bare': {0} }}; ", options.bare.ToString().ToLower()) + script;
            }
            else
            {
                script = "var options = undefined; " + script;
            }

            var js = JavaScript.Execute(script,
                                        new Dictionary <string, object> {
                { "source", source }
            },
                                        new JavaScript.Requirement {
                Path = "http://jashkenas.github.com/coffee-script/extras/coffee-script.js"
            });

            return(js.Get("result") as string);
        }
示例#5
0
        public static JavaScript Execute(string code, object data, params Requirement[] requirements)
        {
            var js = new JavaScript(code, data, requirements);

            js.Execute();
            return(js);
        }
示例#6
0
        public static string squeeze_it(string source, Action <int, int, string> onerror = null)
        {
            var obj = JavaScript.Execute(@"external.Set('source', module.exports(external.Get('source')));",
                                         new Dictionary <string, object> {
                { "source", source }
            },
                                         "https://raw.github.com/mishoo/UglifyJS/master/uglify-js.js");

            if (onerror != null)
            {
                foreach (var msg in obj.Messages)
                {
                    onerror(msg.Line + 1, msg.Column, msg.Content);
                }
            }

            return(obj.Get("source") as string);
        }
示例#7
0
 public static JavaScript Execute(string code, object data, params Requirement[] requirements)
 {
     var js = new JavaScript(code, data, requirements);
     js.Execute();
     return js;
 }