Пример #1
0
        protected override void DoRun(IShellContext context)
        {
            object         model    = null;
            IModelProvider provider = null;

            if (Model is string)
            {
                model = context.Evaluate((string)Model);
                if (model is IModelProvider)
                {
                    provider = (IModelProvider)model;
                    model    = provider.GetModel(context);
                }
            }
            else if (Model is IModelProvider)
            {
                provider = (IModelProvider)Model;
                model    = provider.GetModel(context);
            }
            else
            {
                model = Model;
            }

            _log.InfoFormat("DBSH-00074 Apply template {0}=>{1}", TemplateFile ?? "(inline template)", File);

            string templateData = LoadTemplate(context);

            try
            {
                string fn = context.ResolveFile(context.Replace(File), ResolveFileMode.Output);
                context.OutputMessage("Generating file " + fn);
                using (var sw = new StreamWriter(fn))
                {
                    RazorScripting.ParseRazor(templateData, sw.Write, model,
                                              provider != null ? provider.InitializeTemplate : (Action <IRazorTemplate, IShellContext>)null, context);
                }
            }
            catch (RazorEngine.Templating.TemplateCompilationException err)
            {
                _log.ErrorFormat("DBSH-00075 Error compiling template {0}", TemplateFile);
                foreach (var error in err.Errors)
                {
                    _log.Error(error.ToString());
                }
                throw;
            }
        }
Пример #2
0
        protected string LoadTextContent(object model, IShellContext context)
        {
#if !NETSTANDARD2_0
            string templateData = LoadTemplate(context);
            if (templateData != null)
            {
                var sw = new StringWriter();
                RazorScripting.ParseRazor(templateData, sw.Write, model);
                return(sw.ToString());
            }
#endif

            string textData = TextData;

            if (textData == null && TextFile != null)
            {
                using (var sr = new StreamReader(System.IO.File.OpenRead(context.ResolveFile(context.Replace(TextFile), ResolveFileMode.Input))))
                {
                    textData = sr.ReadToEnd();
                }
            }
            return(textData);
        }
Пример #3
0
        public string PreprocessScript(string content, bool queryContainsProlog)
        {
            if (IsEmpty)
            {
                return(content);
            }
            if (queryContainsProlog)
            {
                var sb    = new StringBuilder();
                int index = 0;
                foreach (string line in content.Split('\n'))
                {
                    if (index < LineCount)
                    {
                        sb.Append("\r\n");
                    }
                    else
                    {
                        sb.Append(line + "\n");
                    }
                    index++;
                }
                content = sb.ToString();
            }

#if !NETSTANDARD2_0
            if (IsRazor)
            {
                char ch = RazorChar ?? '\0';
                if (ch != '\0')
                {
                    //content = Regex.Replace(content, @"^\s*--\s*#\s*razor" + ch, "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                    //content = Regex.Replace(content, @"^\s*--\s*#", "--", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                    content = content.Replace("@", "@@").Replace(ch, '@');
                }
                var sw = new StringWriter();
                RazorScripting.ParseRazor(content, sw.Write, new object());
                content = sw.ToString();
            }
#endif

            if (Regex.Match(content, @"^\s*--\s*#\s*region\s", RegexOptions.Multiline).Success)
            {
                // process regions
                var  regs       = Regions;
                var  sb         = new StringBuilder();
                bool isAllowed  = true;
                bool isInRegion = false;
                foreach (string line in content.Split('\n'))
                {
                    bool isControlLine = false;
                    var  mBegin        = Regex.Match(line, @"^\s*--\s*#\s*region\s+([^\s]+)");
                    if (mBegin.Success)
                    {
                        if (isInRegion)
                        {
                            throw new Exception("DBSH-00111 Nested regions are not allowed");
                        }
                        isInRegion = true;
                        var    item   = ParseLine(line);
                        string region = item.Arguments[0];
                        isAllowed     = regs.Contains(region);
                        isControlLine = true;
                    }
                    var mEnd = Regex.Match(line, @"^\s*--\s*#\s*endregion");
                    if (mEnd.Success)
                    {
                        if (!isInRegion)
                        {
                            throw new Exception("DBSH-00112 #endregion without region");
                        }
                        isInRegion    = false;
                        isAllowed     = true;
                        isControlLine = true;
                    }

                    if (isAllowed && !isControlLine)
                    {
                        sb.Append(line + "\n");
                    }
                    else
                    {
                        sb.Append("\r\n");
                    }
                }
                if (isInRegion)
                {
                    throw new Exception("DBSH-00113 Unclosed region");
                }
                content = sb.ToString();
            }

            foreach (var replace in Replaces)
            {
                content = content.Replace(replace.Arguments[0], replace.Arguments[1]);
            }
            return(content);
        }