HTMLWriter 1.2

And with another iteration the library has improved yet again. See the previous or original posts for details about where it came from.

With this version I’ve made some big changes internally to the library. You can still use the basic functionality as with version 1.0 but the auto indentation has been improved greatly – and simplified in the code. Essentially it now properly formats the generated html with indentation for tags specified to do so.

To make it much easier to manage auto formatting tags I modified the AppendTagStart and AppendTagEnd methods to make use of some string arrays that define tags that require standard behavior Рlike always insert a crlf in-front of it or always append crlf etc.

The arrays looks like this:

startTagsAutoOnNewLine = new string[]
{

“address”,
“blockquote”,
“code”,
“div”,
“h1″,”h2″,”h3″,”h4″,”h5″,”h6”,
“iframe”,
“ol”, “ul”, “dl”,
“table”, “thead”, “tfoot”, “tbody”, “tr”, “td”, “th”

};

tagsAutoIncDecIndentation = new string[]
{

“address”,
“blockquote”,
“ol”, “ul”, “dl”,
“table”, “thead”, “tfoot”, “tbody”, “tr”

};

endTagsAutoOnNewLine = new string[]
{

“address”,
“blockquote”,
“ol”, “ul”, “dl”,
“table”, “thead”, “tfoot”, “tbody”, “tr”,

};

endTagsAutoAppendCRLF = new string[]
{

“address”,
“blockquote”,
“code”,
“h1″,”h2″,”h3″,”h4″,”h5″,”h6”,
“iframe”,
“ol”, “ul”, “dl”,
“table”

};

The 2 methods look like this:

protected void AppendTagStartInternal(string tagName, string className, params CustomAttribute[] customAttributes)
{

if (AutoFormatting)
{

if (startTagsAutoOnNewLine.Contains(tagName.ToLower()))
{

AppendNewLineInternal();
AppendIndentation();

}
else if (lastWrittenCRLF)
{

AppendIndentation();

}

}
this.AppendInternal(string.Format(“<{0}”, tagName));
if (className.Length > 0)

this.AppendInternal(string.Format(” class=\”{0}\””, className));

foreach (CustomAttribute customAttribute in customAttributes)
{

this.AppendInternal(” ” + customAttribute.ToString());

}
this.AppendInternal(“>”);
tags.Push(tagName);
if (AutoFormatting)
{

if (tagsAutoIncDecIndentation.Contains(tagName.ToLower()))
{

IndentationInc();
AppendNewLineInternal();

}

}

}

protected void AppendTagEndInternal(string tagName, bool autoPop)
{

if (autoPop && tags.Peek() == tagName)

tags.Pop();

if (AutoFormatting)
{

if (tagsAutoIncDecIndentation.Contains(tagName.ToLower()))
{

IndentationDec();

}
if (endTagsAutoOnNewLine.Contains(tagName.ToLower()))
{

AppendNewLineInternal(); ;
AppendIndentation();

}
else if (lastWrittenCRLF)
{

AppendIndentation();

}

}

this.AppendInternal(“</” + tagName + “>”);

if (AutoFormatting)
{

if (endTagsAutoAppendCRLF.Contains(tagName.ToLower()))
{

AppendNewLineInternal();

}

}

}

Additionally I split the HTMLWriter class into 2 – the plain HTMLWriter and a base class – HTMLWriterBase. The reason is simple – it makes it easier to maintain core/internal functionality separately but also allow for another custom implementation of another type of xyzWriter.

The DataTable specific methods were also enhanced to allow for a hyperlink (anchor tag) to be embedded based on one of the fields inside the DataTable. It allows you to specify an editing page, the parameter name passed to the page, the linked id field from the DataTable and the display field in which the link will be placed.

Available here.

Leave a Reply

%d bloggers like this: