コード例 #1
0
        private Type GetResourceView(string path)
        {
            Type t;

            if (_compiledResourceView.TryGetValue(path, out t))
            {
                return(t);
            }

            lock (_compiledResourceView)
            {
                if (_compiledResourceView.TryGetValue(path, out t))
                {
                    return(t);
                }

                object res = GetResource(path);
                if (res is string)
                {
                    t = new ViewCompiler((string)res).Build();
                    _compiledResourceView.Add(path, t);
                    return(t);
                }
                return(null);
            }
        }
コード例 #2
0
        public HttpView GetView(string path)
        {
            if (path.StartsWith("?"))
            {
                path = path.Substring(1);
                Type t = GetResourceView(path);
                if (t == null)
                {
                    throw new Exception(String.Format("Resource {0} is not found", path));
                }
                return((HttpView)Activator.CreateInstance(t));
            }
            var filePath = Path.Combine(_viewDir, path);

            return(ViewCompiler.CompileFile(filePath));
        }
コード例 #3
0
        public static HttpView CompileFile(string filePath)
        {
            CachedView cached;
            DateTime   fw = File.GetLastWriteTime(filePath);

            if (!_fileCache.TryGetValue(filePath, out cached) || cached.LastWrite != fw) // TODO Static mode without date checking
            {
                lock (_fileCache)
                {
                    _fileCache.TryGetValue(filePath, out cached);

                    if (cached == null || cached.LastWrite != fw)
                    {
                        string text = File.ReadAllText(filePath);

                        Type t = new ViewCompiler(text).Build();
                        cached = new CachedView(fw, t);
                        _fileCache[filePath] = cached;
                    }
                }
            }

            return((HttpView)Activator.CreateInstance(cached.TView));
        }
コード例 #4
0
 public static string Render(string cshtml)
 {
     return(ViewCompiler.CompileSource(cshtml).Process());
 }
コード例 #5
0
        public static HttpView CompileSource(string source)
        {
            Type t = new ViewCompiler(source).Build();

            return((HttpView)Activator.CreateInstance(t));
        }
コード例 #6
0
        private void WriteCode(string code)
        {
            bool htmlMode = false;

            //StringBuilder html = null;
            //string regexp = null; // Closing regular

            string[] lines = code.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < lines.Length; i++)
            {
                var line = lines[i].Trim();
                if (line.Length == 0)
                {
                    continue;
                }

                if (line[0] == '@') // Начинается с @ - у нас код
                {
                    if (line.Length == 1)
                    {
                        throw new Exception("Escape symbol error");
                    }

                    var first = line[1];
                    if (Char.IsLetter(first) || first == '_')
                    {
                        if (DEBUG)
                        {
                            Console.WriteLine("Compile line: " + line);
                        }
                        ViewCompiler htmlCompiler = new ViewCompiler(this, line);
                        string       cs           = htmlCompiler.GenerateRender();
                        csCode.Append(cs);
                    }
                    else if (first == ':')
                    {
                        if (DEBUG)
                        {
                            Console.WriteLine("Compile line: " + line.Substring(2));
                        }
                        ViewCompiler htmlCompiler = new ViewCompiler(this, line.Substring(2));
                        string       cs           = htmlCompiler.GenerateRender();
                        csCode.Append(cs);
                    }
                    else if (first == '{')
                    {
                        if (DEBUG)
                        {
                            Console.WriteLine("CS: " + line.Substring(1));
                        }
                        csCode.Append(line.Substring(1));
                    }
                    else
                    {
                        throw new Exception("Unknown escape");
                    }

                    continue;
                }


                if (line.StartsWith("<"))
                {
                    if (DEBUG)
                    {
                        Console.WriteLine("HTML: " + line);
                    }
                    ViewCompiler htmlCompiler = new ViewCompiler(this, line);
                    string       cs           = htmlCompiler.GenerateRender();
                    csCode.Append(cs);
                }
                else
                {
                    if (DEBUG)
                    {
                        Console.WriteLine("CS: " + line);
                    }
                    csCode.AppendLine(line);
                }

                /*if (!htmlMode)
                 * {
                 *  var matches = Regex.Matches(line, "^<\\w+");
                 *  if (matches.Count > 0)
                 *  {
                 *      var m = matches[0];
                 *      regexp = "<\\/" + m.Value.Substring(1) + ">";
                 *      htmlMode = true;
                 *
                 *      html = new StringBuilder();
                 *  }
                 *  else
                 *      csCode.AppendLine(line);
                 * }
                 *
                 * if (htmlMode)
                 * {
                 *  html.Append(line);
                 *  if (Regex.IsMatch(line, regexp))
                 *  {
                 *      ViewCompiler htmlCompiler = new ViewCompiler(this, html.ToString());
                 *      string cs = htmlCompiler.GenerateRender();
                 *      csCode.Append(cs);
                 *      htmlMode = false;
                 *      html = null;
                 *  }
                 * }*/
            }

            if (htmlMode)
            {
                throw new Exception("Closing tag not found");
            }
        }
コード例 #7
0
 public ViewCompiler(ViewCompiler parent, string cshtml)
 {
     usings      = parent.usings;
     this.cshtml = cshtml;
 }