public AspxDocument Parse(string contents) { //_contents = contents; _doc = new AspxDocument(contents) { ContentStart = 0, ContentLength = contents.Length }; //1. Extract the directives ParseDirectives(); //2. If has master page, extract hte MainContent if (_doc.HasMaster) { ParseMainContent(); } //3. Extract Code Blocks ParseCodeBlocks(); //4. Find all the literals between code blocks ParseLiteralBlocks(); //5. Parse C# code CodeBlockStream stream = new CodeBlockStream(_doc); AstGenerator generator = new AstGenerator(); generator.Build(stream); return(_doc); }
public CodeBlockStream(AspxDocument doc) : base(doc.Contents) { this._doc = doc; foreach (Block b in _doc.Blocks) { CodeBlock cb = b as CodeBlock; if (cb != null) { _currentCodeBlock = cb; _currentEndIndex = _currentCodeBlock.Index + _currentCodeBlock.Length; this.Seek(_currentCodeBlock.Index + 2); //Skip "<%" break; } } }
public void Convert(string infile) { string ext = Path.GetExtension(infile).ToLower(); if (".master".Equals(ext) || ".aspx".Equals(ext) || ".ascx".Equals(ext)) { Trace.WriteLine("Converting " + infile + "..."); int x = infile.LastIndexOf("."); string outfile = infile.Substring(0, x) + ".cshtml"; AspxParser parser = new AspxParser(); AspxDocument doc = parser.Parse(File.OpenText(infile)); RazorGenerator generator = new RazorGenerator(); generator.Generate(doc, outfile); Trace.WriteLine("Saved to " + outfile); } }
public void Generate(AspxDocument doc, string outfile) { using (StreamWriter sw = new StreamWriter(outfile, false, new UTF8Encoding(true))) { if (!string.IsNullOrEmpty(doc.Inherits)) { //sw.WriteLine(string.Format("@inherits {0}", doc.Inherits)); } foreach (Directive directive in doc.Directives) { PageDirective pageDirective = directive as PageDirective; if (pageDirective != null) { if (!string.IsNullOrEmpty(pageDirective.MasterPageFile)) { sw.WriteLine(string.Format("@{{\r\n\tLayout = \"{0}\";\r\n}}", pageDirective.MasterPageFile.Replace(".Master", ".cshtml"))); } } ImportDirective importDirective = directive as ImportDirective; if (importDirective != null) { sw.WriteLine(string.Format("@using {0}", importDirective.Namespace)); } } bool afterCodeBlock = false; foreach (Block block in doc.Blocks) { HtmlBlock htmlBlock = block as HtmlBlock; if (htmlBlock != null) { string contents = doc.Contents.Substring(block.Index, block.Length); Regex regex = new Regex("^\\s*<"); //Check if started with < if (!afterCodeBlock || regex.IsMatch(contents)) { sw.Write(contents); //Write the contents directlhy } else { //Wrap it inside <text>...</text> sw.Write("<text>"); sw.Write(contents); sw.Write("</text>"); } } afterCodeBlock = false; CodeBlock codeBlock = block as CodeBlock; if (codeBlock != null) { if (codeBlock.IsFirst) { sw.WriteLine(" @{"); } sw.WriteLine("\r\n" + doc.Contents.Substring(block.Index + 2, block.Length - 4)); if (codeBlock.IsLast) { sw.WriteLine("}"); } afterCodeBlock = true; } ExpressionBlock expressionBlock = block as ExpressionBlock; if (expressionBlock != null) { sw.Write(string.Format("@{0} ", doc.Contents.Substring(block.Index + 3, block.Length - 5).Trim())); } CommentBlock commentBlock = block as CommentBlock; if (commentBlock != null) { sw.Write(string.Format("@*{0}*@", doc.Contents.Substring(block.Index + 3, block.Length - 6))); } ContentPlaceHolderBlock cphBlock = block as ContentPlaceHolderBlock; if (cphBlock != null) { if ("MainContent".Equals(cphBlock.ID)) { sw.WriteLine("\r\n\t@RenderBody()"); } } } } }