public static string SourceToHtml(string source, HighlighterBase highlighter) { HtmlParser parser = new HtmlParser(); highlighter.Parser = parser; source = Regex.Replace(source, "<%--Start exclude--%>.*?<%--End exclude--%>", "", RegexOptions.Singleline); string s = highlighter.Parse(source); return string.Concat("<pre style='margin: 0px;font-size:14px;'>", s, "</pre>"); }
public ActionResult GetSourceFile(string file) { string path = this.HttpContext.Server.MapPath(file); string examplesRoot = this.HttpContext.Server.MapPath(ExamplesModel.ApplicationRoot + "/Areas/"); var fi = new FileInfo(path); if (!path.StartsWith(examplesRoot, true, CultureInfo.CurrentCulture)) { return(new HttpStatusCodeResult((int)HttpStatusCode.BadRequest)); } HighlighterBase hb = null; switch (fi.Extension.ToLowerInvariant()) { case ".aspx": case ".ascx": case ".cshtml": case ".master": hb = new ASPXHighlighter(); break; case ".cs": hb = new CSharpHighlighter(); break; case ".xml": case ".xsl": hb = new XMLHighlighter(); break; case ".js": hb = new JavaScriptHighlighter(); break; case ".css": hb = new CSSHighlighter(); break; default: return(Content(System.IO.File.ReadAllText(fi.FullName), "text/plain")); } HtmlParser parser = new HtmlParser(); hb.Parser = parser; string source = Regex.Replace(System.IO.File.ReadAllText(fi.FullName), "<%--Start exclude--%>.*?<%--End exclude--%>", "", RegexOptions.Singleline); source = hb.Parse(source); return(this.Content(string.Concat("<pre style='margin: 0px;font-size:14px;'>", source, "</pre>"), "text/html")); }
public static string SourceToHtml(string source, HighlighterBase highlighter) { HtmlParser parser = new HtmlParser(); highlighter.Parser = parser; source = new Regex("<%--Start exclude--%>.*?<%--End exclude--%>", RegexOptions.Singleline).Replace(source, ""); string s = highlighter.Parse(source); return(string.Concat("<pre style='margin: 0px;font-size:14px;'>", s, "</pre>")); }
/// <summary> /// Parses source code. /// </summary> /// <param name="language">The language used to highlight the text.</param> /// <returns>The highlighter.</returns> protected virtual HighlighterBase GetHighlighter(string language) { Register register = Register.Instance; HighlighterBase highlighter = register.Highlighters[language]; if (highlighter == null) { highlighter = register.Highlighters[this.Language]; } this.EnsureParser(); highlighter = highlighter.Create(); highlighter.Parser = this.htmlParser; highlighter.ForceReset(); return(highlighter); }
public static string SourceToHtml(Uri uri, HighlighterBase highlighter) { string filePath = HttpContext.Current.Server.MapPath(uri.LocalPath); return SourceToHtml(File.ReadAllText(filePath), highlighter); }
public void ProcessRequest(HttpContext context) { string examplesRoot = context.Server.MapPath(UIHelpers.ApplicationRoot + "/Examples/"); string codeRoot = context.Server.MapPath(UIHelpers.ApplicationRoot + "/Code/"); string path; string url; string transmit = context.Request["t"]; if (!string.IsNullOrEmpty(transmit)) { url = context.Request["e"]; if (string.IsNullOrEmpty(url)) { return; } url = "/Examples" + url; path = context.Server.MapPath(url); if (!path.StartsWith(examplesRoot, true, CultureInfo.CurrentCulture) && !path.StartsWith(codeRoot, true, CultureInfo.CurrentCulture)) { return; } context.Response.ContentType = "application/octet-stream"; context.Response.AppendHeader("Connection", "keep-alive"); context.Response.AppendHeader("Content-Disposition", string.Concat(" attachment; filename = ", new DirectoryInfo(path).Name, ".zip")); ZipFiles(path); context.Response.End(); return; } context.Response.ContentType = "text/html"; url = context.Request["f"]; path = context.Server.MapPath(url); FileInfo fi = new FileInfo(path); if (!path.StartsWith(examplesRoot, true, CultureInfo.CurrentCulture) && !path.StartsWith(codeRoot, true, CultureInfo.CurrentCulture)) { return; } HighlighterBase hb = null; switch (fi.Extension.ToLowerInvariant()) { case ".aspx": case ".ascx": case ".master": hb = new ASPXHighlighter(); break; case ".cs": hb = new CSharpHighlighter(); break; case ".xml": case ".xsl": hb = new XMLHighlighter(); break; case ".js": hb = new JavaScriptHighlighter(); break; case ".css": hb = new CSSHighlighter(); break; default: context.Response.ContentType = "text/plain"; context.Response.Write(File.ReadAllText(fi.FullName)); return; } context.Response.Write(HighLighterUtils.SourceToHtml(File.ReadAllText(fi.FullName), hb)); }
public static string SourceToHtml(Uri uri, HighlighterBase highlighter) { string filePath = HttpContext.Current.Server.MapPath(uri.LocalPath); return(SourceToHtml(File.ReadAllText(filePath), highlighter)); }
public ActionResult GetSourceFile(string file) { string path = this.HttpContext.Server.MapPath(file); string examplesRoot = this.HttpContext.Server.MapPath(ExamplesModel.ApplicationRoot + "/Areas/"); var fi = new FileInfo(path); if (!path.StartsWith(examplesRoot, true, CultureInfo.CurrentCulture)) { return(new HttpStatusCodeResult((int)HttpStatusCode.BadRequest)); } HighlighterBase hb = null; bool markup = false; switch (fi.Extension.ToLowerInvariant()) { case ".aspx": case ".ascx": case ".cshtml": case ".master": hb = new ASPXHighlighter(); markup = true; break; case ".cs": hb = new CSharpHighlighter(); break; case ".xml": case ".xsl": hb = new XMLHighlighter(); break; case ".js": hb = new JavaScriptHighlighter(); break; case ".css": hb = new CSSHighlighter(); break; default: return(Content(System.IO.File.ReadAllText(fi.FullName), "text/plain")); } string source = System.IO.File.ReadAllText(fi.FullName); if (markup) { string origCSScriptStart = @"<script runat=""server"">"; string origCSScriptEnd = @"</script>"; string origJSScriptStart = @"<script>"; string origJSScriptEnd = @"</script>"; string origCSScriptText = CutFromTo(source, origCSScriptStart, origCSScriptEnd); string origJSScriptText = CutFromTo(source, origJSScriptStart, origJSScriptEnd); source = HighLighterUtils.SourceToHtml(source, hb); if (!string.IsNullOrEmpty(origCSScriptText)) { string strCSScriptStart = CutFromTo(HighLighterUtils.SourceToHtml(origCSScriptStart, hb), ">", "</pre>"); string strCSScriptEnd = CutFromTo(HighLighterUtils.SourceToHtml(origCSScriptEnd, hb), ">", "</pre>"); string csText = CutFromTo(source, strCSScriptStart, strCSScriptEnd); source = source.Replace(csText, CutFromTo(HighLighterUtils.CSharpToHtml(origCSScriptText), ">", "</pre>")); } if (!string.IsNullOrEmpty(origJSScriptText)) { string strJSScriptStart = CutFromTo(HighLighterUtils.SourceToHtml(origJSScriptStart, hb), ">", "</pre>"); string strJSScriptEnd = CutFromTo(HighLighterUtils.SourceToHtml(origJSScriptEnd, hb), ">", "</pre>"); string jsText = CutFromTo(source, strJSScriptStart, strJSScriptEnd); source = source.Replace(jsText, CutFromTo(HighLighterUtils.JsToHtml(origJSScriptText), ">", "</pre>")); } } else { source = HighLighterUtils.SourceToHtml(source, hb); } return(this.Content(source, "text/html")); }
public void ProcessRequest(HttpContext context) { string examplesRoot = context.Server.MapPath(UIHelpers.ApplicationRoot + "/Moduls/"); string codeRoot = context.Server.MapPath(UIHelpers.ApplicationRoot + "/Code/"); string path; string url; string transmit = context.Request["t"]; if (!string.IsNullOrEmpty(transmit)) { url = context.Request["e"]; if (string.IsNullOrEmpty(url)) { return; } url = "/Moduls" + url; path = context.Server.MapPath(url); if (!path.StartsWith(examplesRoot, true, CultureInfo.CurrentCulture) && !path.StartsWith(codeRoot, true, CultureInfo.CurrentCulture)) { return; } context.Response.ContentType = "application/octet-stream"; context.Response.AppendHeader("Connection", "keep-alive"); context.Response.AppendHeader("Content-Disposition", string.Concat(" attachment; filename = ", new DirectoryInfo(path).Name, ".zip")); ZipFiles(path); context.Response.End(); return; } context.Response.ContentType = "text/html"; url = context.Request["f"]; path = context.Server.MapPath(url); FileInfo fi = new FileInfo(path); if (!path.StartsWith(examplesRoot, true, CultureInfo.CurrentCulture) && !path.StartsWith(codeRoot, true, CultureInfo.CurrentCulture)) { return; } HighlighterBase hb = null; bool markup = false; switch (fi.Extension.ToLowerInvariant()) { case ".aspx": case ".ascx": case ".master": hb = new ASPXHighlighter(); markup = true; break; case ".cs": hb = new CSharpHighlighter(); break; case ".xml": case ".xsl": hb = new XMLHighlighter(); break; case ".js": hb = new JavaScriptHighlighter(); break; case ".css": hb = new CSSHighlighter(); break; default: context.Response.ContentType = "text/plain"; context.Response.Write(File.ReadAllText(fi.FullName)); return; } string fileText = File.ReadAllText(fi.FullName); if (markup) { string origCSScriptStart = @"<script runat=""server"">"; string origCSScriptEnd = @"</script>"; string origJSScriptStart = @"<script>"; string origJSScriptEnd = @"</script>"; string origCSScriptText = CutFromTo(fileText, origCSScriptStart, origCSScriptEnd); string origJSScriptText = CutFromTo(fileText, origJSScriptStart, origJSScriptEnd); fileText = HighLighterUtils.SourceToHtml(fileText, hb); if (!string.IsNullOrEmpty(origCSScriptText)) { string strCSScriptStart = CutFromTo(HighLighterUtils.SourceToHtml(origCSScriptStart, hb), ">", "</pre>"); string strCSScriptEnd = CutFromTo(HighLighterUtils.SourceToHtml(origCSScriptEnd, hb), ">", "</pre>"); string csText = CutFromTo(fileText, strCSScriptStart, strCSScriptEnd); fileText = fileText.Replace(csText, CutFromTo(HighLighterUtils.CSharpToHtml(origCSScriptText), ">", "</pre>")); } if (!string.IsNullOrEmpty(origJSScriptText)) { string strJSScriptStart = CutFromTo(HighLighterUtils.SourceToHtml(origJSScriptStart, hb), ">", "</pre>"); string strJSScriptEnd = CutFromTo(HighLighterUtils.SourceToHtml(origJSScriptEnd, hb), ">", "</pre>"); string jsText = CutFromTo(fileText, strJSScriptStart, strJSScriptEnd); fileText = fileText.Replace(jsText, CutFromTo(HighLighterUtils.JsToHtml(origJSScriptText), ">", "</pre>")); } context.Response.Write(fileText); } else { context.Response.Write(HighLighterUtils.SourceToHtml(fileText, hb)); } }
/// <summary> /// Adds the controls which form the code snippet. /// </summary> /// <param name="source">The source code.</param> /// <param name="language">The language in which the source code is written.</param> protected virtual void RenderCode(string source, string language) { HighlighterBase highlighter = this.GetHighlighter(language); string parsedText = highlighter.Parse(source); string[] lines = parsedText.Replace("\r", String.Empty).Split('\n'); // Find the placeholders for the line numbers and source lines. Control templatedOutLineControl = new SourceHeaderItem(highlighter.FullName, lines.Length); this.Controls.Add(templatedOutLineControl); this.codeLayoutTemplate.InstantiateIn(templatedOutLineControl); PlaceHolder lineNumberPlaceHolder = this.FindLineNumberPlaceHolder(templatedOutLineControl); PlaceHolder sourceLinePlaceHolder = this.FindSourceLinePlaceHolder(templatedOutLineControl); FunctionSelector selector = FunctionSelectorFactory.Get(language); selector.FunctionName = FunctionName; if (lineNumberPlaceHolder != null || sourceLinePlaceHolder != null) { Control templatedLineNumberControl, templatedSourceLineControl, separatorControl; for (int i = 0; i < lines.Length; i++) { int lineNumber = (i + 1); if (lineNumber >= _StartLineNumber && lineNumber <= _EndLineNumber) { string line = lines[i]; if (selector.IsLineBelongingToRequiredFunction(line)) { if (lineNumberPlaceHolder != null && this.lineNumberTemplate != null) { // Parse line number. templatedLineNumberControl = new SourceItem(lineNumber, lines.Length, line); this.lineNumberTemplate.InstantiateIn(templatedLineNumberControl); lineNumberPlaceHolder.Controls.Add(templatedLineNumberControl); if (lineNumber < lines.Length && this.separatorTemplate != null) { separatorControl = new SeparatorItem(); this.separatorTemplate.InstantiateIn(separatorControl); lineNumberPlaceHolder.Controls.Add(separatorControl); } } if (sourceLinePlaceHolder != null && this.sourceLineTemplate != null) { // Parse source line. templatedSourceLineControl = new SourceItem(lineNumber, lines.Length, line); this.sourceLineTemplate.InstantiateIn(templatedSourceLineControl); sourceLinePlaceHolder.Controls.Add(templatedSourceLineControl); if (lineNumber < lines.Length && this.separatorTemplate != null) { separatorControl = new SeparatorItem(); this.separatorTemplate.InstantiateIn(separatorControl); sourceLinePlaceHolder.Controls.Add(separatorControl); } } } } } } templatedOutLineControl.DataBind(); }