Esempio n. 1
0
        public string GenerateResult(Timetable tt)
        {
            // Allowed types whitlisted by extensions (for this specific template type or generic (e.g. type == null)).
            var extensionAllowedTypes = pluginInterface.GetRegistered <ITemplateWhitelistEntry>()
                                        .Where(w => w.TemplateType == TemplateType || w.TemplateType == null)
                                        .Select(w => w.GetWhitelistType());
            // Globally whitelisted: From FPLedit.Shared, marked with TemplateSafeAttribute.
            var allowedTypes = typeof(Timetable).Assembly.GetTypes()
                               .Where(type => type.GetCustomAttributes(typeof(TemplateSafeAttribute), true).Length > 0)
                               .Concat(extensionAllowedTypes)
                               .Concat(new[] { typeof(Enumerable) }); // Also whitelist type used for LINQ.

            var engine = new Engine();

            foreach (var type in allowedTypes) // Register all allowed types
            {
                engine.SetValue(type.Name, type);
            }

            TemplateDebugger.GetInstance().SetContext(this); // Move "Debugger" context to current template.

            const string polyFillsPath          = "Templating.TemplatePolyfills.js";
            var          polyfillsParserOptions = new ParserOptions(polyFillsPath)
            {
                Tolerant = false
            };
            var templateCodeParserOptions = new ParserOptions(Identifier);

            return(engine
                   .SetValue("tt", tt)
                   .SetValue("debug", new Action <object>((o) => pluginInterface.Logger.Info($"{o?.GetType()?.FullName ?? "null"}: {o ?? "null"}")))
                   .SetValue("debug_print", new Action <object>((o) => pluginInterface.Logger.Info($"{o}")))
                   .SetValue("clr_typename", new Func <object, string>(o => o.GetType().Name))
                   .SetValue("clr_typefullname", new Func <object, string>(o => o.GetType().FullName))
                   .SetValue("clr_toArray", new Func <IEnumerable <object>, object[]>(o => o.ToArray()))
                   .Execute(ResourceHelper.GetStringResource(polyFillsPath), polyfillsParserOptions) // Load polyfills
                   .Execute("var __builder = '';", polyfillsParserOptions)                           // Create output variable
                   .Execute(CompiledCode, templateCodeParserOptions)
                   .GetValue("__builder")
                   .ToString());
        }
Esempio n. 2
0
        public string GenerateResult(Timetable tt)
        {
            if (!Enabled || tmpl == null)
            {
                return(null);
            }

            try
            {
                return(tmpl.GenerateResult(tt));
            }
            catch (JavaScriptException ex)
            {
                var source   = ex.Location.Source ?? Identifier;
                var isModule = source != Identifier;
                logger.Error(T._("Fehler im {0} {1}: {2} in line {3}, column {4}",
                                 (isModule ? "Modul" : "Template"), source, ex.Message, ex.LineNumber, ex.Column));
                if (!isModule)
                {
                    TemplateDebugger.GetInstance().Navigate(ex.LineNumber, ex.Column);
                }
            }
            catch (Esprima.ParserException ex)
            {
                var source   = ex.SourceText ?? Identifier;
                var isModule = source != Identifier;
                logger.Error(T._("Fehler im {0} {1}: {2} in line {3}, column {4}",
                                 (isModule ? "Modul" : "Template"), source, ex.Message, ex.LineNumber, ex.Column));
                if (!isModule)
                {
                    TemplateDebugger.GetInstance().Navigate(ex.LineNumber, ex.Column);
                }
            }
            catch (Exception ex)
            {
                logger.Error(T._("Fehler im Template {0}: {1}", Identifier, ex.Message));
                TemplateDebugger.GetInstance().OpenDebugger();
            }

            return(null);
        }