public void InstantiateIn(Control c) { SourceHeaderItem container = (SourceHeaderItem)c; if (c.Page != null && c.Page.Header != null && c.Page.Items["shl_ln"] == null) { HtmlGenericControl lineNumberStyle = new HtmlGenericControl("style"); lineNumberStyle.Attributes.Add("type", "text/css"); lineNumberStyle.InnerHtml = ".shl_ln { border-right: 1px solid #000; padding-right: 2px; }"; c.Page.Header.Controls.Add(lineNumberStyle); c.Page.Items["shl_ln"] = new object(); } HtmlTable root = new HtmlTable(); root.Style.Add(HtmlTextWriterStyle.Width, "100%"); container.Controls.Add(root); HtmlTableRow headerRow = new HtmlTableRow(); root.Rows.Add(headerRow); HtmlTableCell headerCell = new HtmlTableCell(); headerCell.ColSpan = 2; headerCell.Style.Add(HtmlTextWriterStyle.FontWeight, "bold"); headerCell.InnerText = container.Language; headerRow.Cells.Add(headerCell); HtmlTableRow bodyRow = new HtmlTableRow(); root.Rows.Add(bodyRow); HtmlTableCell lineNumberCell = new HtmlTableCell(); lineNumberCell.Style.Add(HtmlTextWriterStyle.TextAlign, "right"); lineNumberCell.Style.Add(HtmlTextWriterStyle.VerticalAlign, "top"); lineNumberCell.Style.Add(HtmlTextWriterStyle.Width, "1%"); bodyRow.Cells.Add(lineNumberCell); HtmlGenericControl lineNumberPre = new HtmlGenericControl("pre"); lineNumberCell.Controls.Add(lineNumberPre); LineNumberPlaceHolder lineNumberPH = new LineNumberPlaceHolder(); lineNumberPre.Controls.Add(lineNumberPH); HtmlTableCell sourceLineCell = new HtmlTableCell(); sourceLineCell.Style.Add(HtmlTextWriterStyle.VerticalAlign, "top"); sourceLineCell.Style.Add(HtmlTextWriterStyle.Width, "99%"); bodyRow.Cells.Add(sourceLineCell); HtmlGenericControl sourceLinePre = new HtmlGenericControl("pre"); sourceLineCell.Controls.Add(sourceLinePre); SourceLinePlaceHolder sourceLinePH = new SourceLinePlaceHolder(); sourceLinePre.Controls.Add(sourceLinePH); }
/// <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(); }