public bool Render(object o, string description, int depth, XhtmlTextWriter writer) { //there are two principal ways of rendering an object, // if it's an enumerable we render each object as a row in a table // if it's jut an object we render each property of the object as a row in a table if (!String.IsNullOrWhiteSpace(description)) { writer.RenderBeginTag(HtmlTextWriterTag.H3); writer.Write(description); writer.RenderEndTag(); writer.WriteLine(); } if (depth <= 0) { return(true); } if (o is IEnumerable && !(o is String)) { return(RenderEnumerable((IEnumerable)o, depth, writer)); } else { return(RenderObject(o, depth, writer)); } }
/// <summary> /// Converts a UfDataNode structure into a very basic form HTML. /// </summary> /// <param name="node">Node</param> /// <param name="formatDescriber">Microformat format describer object</param> /// <returns>HTML string</returns> public string Convert(UfDataNode node, UfFormatDescriber formatDescriber) { string output = string.Empty; StringWriter stringWriter = new StringWriter(); UfElementDescriber elementDescriber = formatDescriber.BaseElement; using (XhtmlTextWriter writer = new XhtmlTextWriter(stringWriter)) { writer.WriteBeginTag("div"); writer.WriteAttribute("class", "microformats"); writer.Write(HtmlTextWriter.TagRightChar); foreach (UfDataNode child in node.Nodes) { writer.WriteLine(); AddNode(child, elementDescriber, writer); } writer.WriteEndTag("div"); writer.WriteLine(); } return(stringWriter.ToString()); }
public bool RenderObject(object o, int depth, XhtmlTextWriter writer) { var objectType = o.GetType(); if (IsSimpleType(objectType)) { writer.RenderBeginTag(HtmlTextWriterTag.P); writer.Write(o.ToString()); writer.RenderEndTag(); writer.WriteLine(); return(true); } //get the members (fields and properties) var members = GetMembers(objectType); //create a two column table containing member and then value writer.RenderBeginTag(HtmlTextWriterTag.Table); //write a header containing the class name writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2"); writer.RenderBeginTag(HtmlTextWriterTag.Th); writer.Write(objectType.Name); writer.RenderEndTag(); //th writer.RenderEndTag(); //tr foreach (var member in members) { writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.AddAttribute(HtmlTextWriterAttribute.Class, "left"); writer.RenderBeginTag(HtmlTextWriterTag.Th); writer.Write(member.Name); writer.RenderEndTag(); //th writer.RenderBeginTag(HtmlTextWriterTag.Td); RenderMember(member, o, depth, writer); writer.RenderEndTag(); //td writer.RenderEndTag(); //tr } writer.RenderEndTag(); //table writer.WriteLine(); return(true); }
private void InitHeader() { writer.WriteLineNoTabs("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); writer.AddAttribute("xmlns", "http://www.w3.org/1999/xhtml"); writer.AddAttribute("xml:lang", "en"); writer.RenderBeginTag(HtmlTextWriterTag.Html); writer.RenderBeginTag(HtmlTextWriterTag.Head); writer.RenderBeginTag(HtmlTextWriterTag.Title); writer.Write("XhtmlDumper"); writer.RenderEndTag(); writer.AddAttribute("http-equiv", "content-type"); writer.AddAttribute(HtmlTextWriterAttribute.Content, "text/html;charset=utf-8"); writer.RenderBeginTag(HtmlTextWriterTag.Meta); writer.RenderEndTag(); writer.WriteLine(); writer.AddAttribute(HtmlTextWriterAttribute.Name, "generator"); writer.AddAttribute(HtmlTextWriterAttribute.Content, "XhtmlDumper"); writer.RenderBeginTag(HtmlTextWriterTag.Meta); writer.RenderEndTag(); writer.WriteLine(); writer.AddAttribute(HtmlTextWriterAttribute.Name, "description"); writer.AddAttribute(HtmlTextWriterAttribute.Content, "Generated on: " + DateTime.Now); writer.RenderBeginTag(HtmlTextWriterTag.Meta); writer.RenderEndTag(); writer.WriteLine(); writer.AddAttribute("type", "text/css"); writer.RenderBeginTag(HtmlTextWriterTag.Style); writer.WriteLineNoTabs(Resources.StyleSheet); writer.RenderEndTag(); // style writer.RenderEndTag(); // Head writer.WriteLine(); writer.RenderBeginTag(HtmlTextWriterTag.Body); }
// <snippet3> protected override void Render(HtmlTextWriter writer) { // Create an instance of the XhtmlTextWriter class, // named w, and cast the HtmlTextWriter passed // in the writer parameter to w. XhtmlTextWriter w = new XhtmlTextWriter(writer); // Create a string variable, named value, to hold // the control's Text property value. String value = Control.Text; // Create a Boolean variable, named attTest, // to test whether the Style attribute is // valid in the page that the control is // rendered to. Boolean attTest = w.IsValidFormAttribute("style"); // Check whether attTest is true or false. // If true, a style is applied to the XHTML // content. If false, no style is applied. if (attTest) { w.EnterStyle(Control.ControlStyle); } // Write the Text property value of the control, // a <br> element, and a string. Consider encoding the value using WriteEncodedText. w.Write(value); w.WriteBreak(); w.Write("This control conditionally rendered its styles for XHTML."); // Check whether attTest is true or false. // If true, the XHTML style is closed. // If false, nothing is rendered. if (attTest) { w.ExitStyle(Control.ControlStyle); } }
private void RenderMember(MemberInfo memberInfo, object o, int depth, XhtmlTextWriter writer) { //try to get the value object value = null; try { value = GetValue(memberInfo, o); } catch (Exception ex) { value = "Error getting member value: " + ex.Message; } if (value == null) { writer.Write("null"); return; } var valueType = GetValueType(memberInfo); if (valueType == null) { writer.Write("Error getting member type."); } else if (valueType == typeof(double) || valueType == typeof(decimal) || valueType == typeof(float)) { //if the property seems to contain percent data we format it appropriately if (memberInfo.Name.ToLower().Contains("percent") || memberInfo.Name.ToLower().Contains("prc")) { writer.Write("{0:0.00%}", value); } else { writer.Write("{0:0.00}", value); } } else if (valueType == typeof(DateTime) || valueType == typeof(DateTimeOffset)) { //todo: optional date time formatting writer.Write(value); } else if (IsSimpleType(valueType)) { writer.Write(value); } else { //recurively call the render method to descend one level in the object tree Render(value, null, depth - 1, writer); } }
public Stream RenderPageEnd() { XhtmlTextWriter xhtmlWriter = new XhtmlTextWriter(Writer); xhtmlWriter.Indent = 3; // Flush writer contents to output. xhtmlWriter.Flush(); if (ReportFooter != null && ReportFooter.Length > 0) { xhtmlWriter.RenderBeginTag("hr"); xhtmlWriter.WriteEndTag("hr"); // xhtmlWriter.AddAttribute("class", "footer"); xhtmlWriter.RenderBeginTag("div"); xhtmlWriter.Write(ReportFooter); xhtmlWriter.WriteEndTag("div"); xhtmlWriter.RenderBeginTag("hr"); xhtmlWriter.WriteEndTag("hr"); xhtmlWriter.WriteLine(Writer.NewLine); // </h2> } //-------------------------------------------------------- // No matter what state the Renderer left us in, close // off the <body> and <html> elements. //-------------------------------------------------------- // </body> xhtmlWriter.WriteEndTag("body"); // </html> xhtmlWriter.WriteEndTag("html"); // Flush writer contents to output. xhtmlWriter.Flush(); return(Writer.BaseStream); }
/// <summary> /// Parses source code. /// </summary> /// <param name="source">The source code which will be parsed.</param> /// <param name="scannerResult">The result returned by the scanners after scanning the source code.</param> /// <returns>The highlighted source code.</returns> public override string Parse(string source, IList <Occurrence> scannerResult) { StringWriter htmlStringWriter = new StringWriter(); XhtmlTextWriter htmlWriter = new XhtmlTextWriter(htmlStringWriter); int lastIndex = 0; for (int i = 0; i < scannerResult.Count; i++) { if ((scannerResult[i].Start - lastIndex) >= 0) { if (scannerResult[i].Start > 0) { // Encode non-highlighted text first. htmlWriter.WriteEncodedText(source.Substring(lastIndex, scannerResult[i].Start - lastIndex)); } htmlWriter.Write(this.ParseToken(source.Substring(scannerResult[i].Start, scannerResult[i].Length), scannerResult[i].Node)); } lastIndex = scannerResult[i].Start + scannerResult[i].Length; } // Encode the last bit of nonhighlighted text. if (lastIndex < source.Length) { htmlWriter.WriteEncodedText(source.Substring(lastIndex)); } string result = htmlStringWriter.ToString(); htmlWriter.Close(); htmlStringWriter.Close(); return(result); }
private void AddNode(UfDataNode node, UfElementDescriber elementDescriber, XhtmlTextWriter writer) { if (node.Name != string.Empty) { indentNum++; writer.Indent = indentNum; UfElementDescriber currentDescriber = elementDescriber; foreach (UfElementDescriber childElementDescriber in elementDescriber.Elements) { if (node.Name == childElementDescriber.Name || node.Name == childElementDescriber.CompoundName) { currentDescriber = childElementDescriber; } } if (currentDescriber.Attribute == "class") { writer.WriteBeginTag("div"); if (currentDescriber.CompoundName == "") { writer.WriteAttribute("class", node.Name); } else { writer.WriteAttribute("class", node.Name + " " + currentDescriber.Name); } if (!string.IsNullOrEmpty(node.ElementId)) { writer.WriteAttribute("id", node.ElementId); } writer.Write(HtmlTextWriter.TagRightChar); writer.WriteEncodedText(node.Value); foreach (UfDataNode child in node.Nodes) { writer.WriteLine(); AddNode(child, currentDescriber, writer); } if (node.Name != string.Empty) { writer.WriteEndTag("div"); writer.WriteLine(); } } if (currentDescriber.Attribute == "rel") { writer.WriteBeginTag("a"); writer.WriteAttribute("href", node.DescendantValue("link")); writer.WriteAttribute("rel", node.Name); writer.Write(HtmlTextWriter.TagRightChar); writer.WriteEncodedText(node.DescendantValue("text")); writer.WriteEndTag("a"); writer.WriteLine(); } indentNum--; writer.Indent = indentNum; } }
/// <summary> /// Visit XHTMLElement and write it to stream /// </summary> /// <param name="element">Element to visit</param> public void Visit(XHTMLElement element) { if (element.GetTag() == "cdata") { //CData need different handling WriteCData(element); } else if (element.GetTag() == "comment") { //Write comment WriteComment(element); } else { //Normal XHTML tag handling XhtmlWriter.BeginRender(); //Check if element is tag, if not write tag, otherwise handle as XHTML tag if (!element.GetTagState()) { XhtmlWriter.BeginRender(); //Get escape chars out of this tag CharIterator charIterator = new CharIterator(); String tag = charIterator.ParseText(element.GetTag()); XhtmlWriter.Write(tag); XhtmlWriter.EndRender(); XhtmlWriter.Flush(); return; } XhtmlWriter.WriteBeginTag(element.GetTag()); foreach (KeyValuePair <String, String> pair in element.GetAttributes()) { XhtmlWriter.WriteAttribute(pair.Key, pair.Value, false); } if (IsEmptyElement(element)) { //Nothing inside element, so write tag end XhtmlWriter.Write(XhtmlTextWriter.SelfClosingTagEnd); } else if (IsXHTMLTag(element.GetTag())) { //Write tag opening closing XhtmlWriter.Write(XhtmlTextWriter.TagRightChar); //Write content XhtmlWriter.Write(element.GetContent()); //Visit children foreach (XHTMLElement child in element.GetChildren()) { Visit(child); } //Write closing tag XhtmlWriter.WriteEndTag(element.GetTag()); XhtmlWriter.Flush(); } else { //Just write it XhtmlWriter.Write(element.GetTag()); XhtmlWriter.EndRender(); XhtmlWriter.Flush(); } } }
public Stream RenderPageBegin() { XhtmlTextWriter xhtmlWriter = new XhtmlTextWriter(Writer); xhtmlWriter.Indent = 3; xhtmlWriter.AddAttribute("xmlns", "http://www.w3.org/1999/xhtml"); xhtmlWriter.RenderBeginTag("html"); xhtmlWriter.RenderBeginTag("head"); if (ReportHeading != null && ReportHeading.Length > 0) { xhtmlWriter.RenderBeginTag("title"); xhtmlWriter.Write(ReportHeading); xhtmlWriter.WriteEndTag("title"); } // <style type='text/css'> xhtmlWriter.WriteLine(Writer.NewLine); xhtmlWriter.AddAttribute(HtmlTextWriterAttribute.Type, "text/css"); xhtmlWriter.RenderBeginTag("style"); foreach (var style in _styles) { xhtmlWriter.WriteLine(style.Key); xhtmlWriter.WriteLine("{"); xhtmlWriter.WriteLine(style.Value); xhtmlWriter.WriteLine("}"); } xhtmlWriter.WriteEndTag("style"); xhtmlWriter.WriteLine(Writer.NewLine); // </style> xhtmlWriter.WriteEndTag("head"); // <body> xhtmlWriter.RenderBeginTag("body"); if (ReportHeading != null && ReportHeading.Length > 0) { // <h2 class='heading'> xhtmlWriter.AddAttribute("class", "heading"); xhtmlWriter.RenderBeginTag("h2"); xhtmlWriter.Write(ReportHeading); xhtmlWriter.WriteEndTag("h2"); xhtmlWriter.WriteLine(Writer.NewLine); // </h2> } if (!(Renderer is ITextControl) && Text != null && Text.Length > 0) { // <p class='text'> xhtmlWriter.AddAttribute("class", "caption"); xhtmlWriter.RenderBeginTag("p"); xhtmlWriter.Write(Text); xhtmlWriter.WriteEndTag("p"); xhtmlWriter.WriteLine(Writer.NewLine); // </p> } // Flush writer contents to output. xhtmlWriter.Flush(); return(Writer.BaseStream); }
public bool RenderEnumerable(IEnumerable enumerable, int depth, XhtmlTextWriter writer) { var elementType = GetElementTypeOfEnumerable(enumerable); if (elementType == null) { return(false); } //get the members (fields and properties) var members = GetMembers(elementType); //write the headers and start the table writer.RenderBeginTag(HtmlTextWriterTag.Table); //if the element type is an element that needs to be rendered atomically we use a different method if (IsSimpleType(elementType)) { writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.RenderBeginTag(HtmlTextWriterTag.Th); writer.Write(elementType.Name); writer.RenderEndTag(); writer.RenderEndTag(); foreach (var element in enumerable) { writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.Write(element.ToString()); writer.RenderEndTag(); writer.RenderEndTag(); } } else { writer.RenderBeginTag(HtmlTextWriterTag.Tr); foreach (var member in members) { writer.RenderBeginTag(HtmlTextWriterTag.Th); writer.Write(member.Name); writer.RenderEndTag(); } writer.RenderEndTag(); //tr writer.WriteLine(); //write all the members foreach (var element in enumerable) { writer.RenderBeginTag(HtmlTextWriterTag.Tr); foreach (var member in members) { writer.RenderBeginTag(HtmlTextWriterTag.Td); RenderMember(member, element, depth, writer); writer.RenderEndTag(); //td } writer.RenderEndTag(); //tr writer.WriteLine(); } } //end the table writer.RenderEndTag(); writer.WriteLine(); return(true); }