Skip to content

Instantly share code, notes, and snippets.

@dwcullop
Created July 22, 2015 21:58
Show Gist options
  • Save dwcullop/37c98a27197b67480206 to your computer and use it in GitHub Desktop.
Save dwcullop/37c98a27197b67480206 to your computer and use it in GitHub Desktop.
Started work on an HTML builder class but can't finish it now... Throwing it here for safe keeping / whatever anyone else wants to do with it. Its basically a thin wrapper around using Linq/XML to generate HTML to make it more readable. What we really need is some sort of short hand code for describing how to display a document. Some sort of way…
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace DareWare.Utils
{
// This is basically a wrapper around using the XML/Linq to generate HTML to make the code more readable
// It is mostly done but not 100% and I can't spend any more time on it right now so I'm throwing it out there
// in case someone else had a similiar idea and/or doesn't want to start from scratch
static class Demo
{
static public XDocument TheOldWay()
{
return new XDocument( new XDocumentType( "html", null, null, null ),
new XElement( "html",
new XElement( "head",
new XElement( "title", "Document Title" ),
new XElement( "meta",
new XAttribute( "name", "Generator" ),
new XAttribute( "content", "I Made This") ) ),
new XElement( "body",
new XElement( "table",
new XAttribute( "class", "tableClass" ),
new XElement( "thead",
new XElement( "tr",
new XElement( "th",
new XAttribute( "colspan", 2 ),
"Table Header" ) ) ),
new XElement( "tbody",
new XElement( "tr",
new XElement( "td", "Cell One" ),
new XElement( "td", "Cell Two" ) ) ) ) ) ) );
}
// Isn't that much nicer? It will generate the same HTML output
static public XDocument TheNewWay()
{
return CreateDocument(
Html( Head( "DocumentTitle", Meta( "Generator", "I Made This" ) ),
Body( Table( "tableClass",
THead( TR( TH( 2, 1, Div( "Table Header" ) ) ) ),
TBody( TR( TD( Div( "Cell One" ) ),
TD( Div( "Cell Two" ) ) ) ) ) ) ) );
}
}
public class HtmlBuilder
{
#region Tag Classes
static public class Tags
{
#region Base Classes
public abstract class Attrib : XAttribute
{
protected Attrib( string name, string value ) : base( name, value ) { }
public string AttribName { get { return this.Name.ToString(); } }
}
public abstract class TagBase : XElement
{
//protected TagBase( string name, Class className, params object[] children ) : base (name, className, children) { }
protected TagBase( string name, params object[] children ) : base( name, children ) { }
public string TagName { get { return this.Name.ToString(); } }
}
public abstract class Tag : TagBase
{
protected Tag( string name, Id id, Class className, params object[] children ) : base( name, id, className, children ) { }
protected Tag( string name, Class className, params object[] children ) : base( name, className, children ) { }
protected Tag( string name, Id id, params object[] children ) : base( name, id, children ) { }
protected Tag( string name, Id id, Class className, string content ) : base( name, id, className, content ) { }
protected Tag( string name, Class className, string content ) : base( name, className, content ) { }
protected Tag( string name, Id id, string content ) : base( name, id, content ) { }
}
public abstract class HeadTag : TagBase
{
protected HeadTag( string name, params object[] children ) : base( name, children ) { }
}
public sealed class Document : XDocument
{
public Document( Html html )
: base( new XDocumentType( "html", null, null, null ),
html )
{
}
}
#endregion
#region Attributes
public class Class : Attrib
{
public Class( string name ) : base( "class", name ) { }
public static implicit operator Class( string name ) { return !String.IsNullOrEmpty( name ) ? new Class( name ) : null; }
//public static implicit operator String( Class className ) { return (className != null) ? className.AttribName : null; }
}
public class ColSpan : Attrib
{
public ColSpan( int colspan ) : base( "colspan", colspan.ToString() ) { }
public static implicit operator ColSpan( int colspan ) { return (colspan > 1) ? new ColSpan( colspan ) : null; }
}
public class RowSpan : Attrib
{
public RowSpan( int rowspan ) : base( "rowspan", rowspan.ToString() ) { }
public static implicit operator RowSpan( int rowspan ) { return (rowspan > 1) ? new RowSpan( rowspan ) : null; }
}
public class Src : Attrib
{
public Src( string url ) : base( "src", url ) { }
public static implicit operator Src( string url ) { return !String.IsNullOrEmpty( url ) ? new Src( url ) : null; }
}
public class Href : Attrib
{
public Href( string url ) : base( "href", url ) { }
public static implicit operator Href( string url ) { return !String.IsNullOrEmpty( url ) ? new Href( url ) : null; }
}
public class Name : Attrib
{
public Name( string name ) : base( "name", name ) { }
public static implicit operator Name( string name ) { return !String.IsNullOrEmpty( name ) ? new Name( name ) : null; }
}
public class Id : Attrib
{
public Id( string id ) : base( "id", id ) { }
public static implicit operator Id( string id ) { return !String.IsNullOrEmpty( id ) ? new Id( id ) : null; }
}
public class Rel : Attrib
{
public Rel( string rel ) : base( "rel", rel ) { }
public static implicit operator Rel( string rel ) { return !String.IsNullOrEmpty( rel ) ? new Rel( rel ) : null; }
}
public class Type : Attrib
{
public Type( string type ) : base( "type", type ) { }
public static implicit operator Type( string type ) { return !String.IsNullOrEmpty( type ) ? new Type( type ) : null; }
}
public class Content : Attrib
{
public Content( string content ) : base( "content", content ) { }
public static implicit operator Content( string content ) { return !String.IsNullOrEmpty( content ) ? new Content( content ) : null; }
}
#endregion
#region Head Tags
public class Meta : HeadTag
{
public Meta( Name name, Content content ) : base( "meta", name, content ) { }
}
public class Link : HeadTag
{
public Link( Rel rel, Href href ) : base( "link", rel, href ) { }
}
public class Title : HeadTag
{
public Title( string title ) : base( "title", title ) { }
public static implicit operator Title( string title ) { return !String.IsNullOrEmpty( title ) ? new Title( title ) : null; }
}
#endregion
#region Tags
public class Body : TagBase
{
public Body( Class className, params Tag[] children ) : base( "body", className, children ) { }
public Body( params Tag[] children ) : this( null, children ) { }
}
public class Head : TagBase
{
public Head( Title title, params HeadTag[] children ) : base( "head", title, children ) { }
public Head( params HeadTag[] children ) : base( "head", children ) { }
}
public class Html : TagBase
{
public Html( Head head, Body body ) : base( "html", head, body ) { }
}
public abstract class CellBase : Tag
{
protected CellBase( string tagName, Class className, ColSpan colSpan, RowSpan rowSpan, params Tag[] children ) : base( tagName, className, colSpan, rowSpan, children ) { }
protected CellBase( string tagName, params Tag[] children ) : this( tagName, null, null, null, children ) { }
}
public class TD : CellBase
{
public TD( Class className, ColSpan colSpan, RowSpan rowSpan, params Tag[] children ) : base( "td", className, colSpan, rowSpan, children ) { }
}
public class TH : CellBase
{
public TH( Class className, ColSpan colSpan, RowSpan rowSpan, params Tag[] children ) : base( "th", className, colSpan, rowSpan, children ) { }
}
public class TR<CELL_TYPE> : Tag where CELL_TYPE : CellBase
{
public TR( Class className, params CELL_TYPE[] children ) : base( "tr", className, children ) { }
public TR( params CELL_TYPE[] children ) : this( null, children ) { }
}
public class TBody : Tag
{
public TBody( Class className, params TR<TD>[] children ) : base( "tbody", className, children ) { }
public TBody( params TR<TD>[] children ) : this( null, children ) { }
public static implicit operator TBody( TR<TD>[] rows ) { return new TBody( rows ); }
}
public class THead : Tag
{
public THead( Class className, params TR<TH>[] children ) : base( "thead", className, children ) { }
public THead( params TR<TH>[] children ) : this( null, children ) { }
public static implicit operator THead( TR<TH>[] rows ) { return new THead( rows ); }
}
public class Table : Tag
{
public Table( Class className, THead head, TBody body ) : base( "table", className, head, body ) { }
}
public class Div : Tag
{
public Div( Class className, params Tag[] children ) : base( "div", className, children ) { }
public Div( Class className, string content ) : base( "div", className, content ) { }
//public Div( params Tag[] children ) : this( null, children ) { }
}
public class P : Tag
{
public P( Class className, params Tag[] children ) : base( "p", className, children ) { }
public P( Class className, string content ) : base( "p", className, content ) { }
//public Div( params Tag[] children ) : this( null, children ) { }
}
public class Span : Tag
{
public Span( Class className, params Tag[] children ) : base( "span", className, children ) { }
public Span( Class className, string content ) : base( "span", className, content ) { }
//public Span( params Tag[] children ) : this( null, children ) { }
}
public class A : Tag
{
public A( Class className, Href href, params Tag[] children ) : base( "a", className, href, children ) { }
public A( Class className, Href href, string content ) : base( "a", className, href, content ) { }
public A( Class className, Name name, params Tag[] children ) : base( "a", className, name, children ) { }
public A( Class className, Name name, string content ) : base( "a", className, name, content ) { }
}
public class Img : Tag
{
public Img( Class className, Src source ) : base( "img", className, source ) { }
}
#endregion
}
#endregion
#region TD
public static Tags.TD TD( Tags.Class className, Tags.ColSpan colSpan, Tags.RowSpan rowSpan, params Tags.Tag[] children ) { return new Tags.TD( className, colSpan, rowSpan, children ); }
public static Tags.TD TD( Tags.Class className, Tags.ColSpan colSpan, params Tags.Tag[] children ) { return TD( className, colSpan, null, children ); }
public static Tags.TD TD( Tags.Class className, Tags.RowSpan rowSpan, params Tags.Tag[] children ) { return TD( className, null, rowSpan, children ); }
public static Tags.TD TD( Tags.ColSpan colSpan, Tags.RowSpan rowSpan, params Tags.Tag[] children ) { return TD( null, colSpan, rowSpan, children ); }
public static Tags.TD TD( Tags.Class className, params Tags.Tag[] children ) { return TD( className, null, null, children ); }
public static Tags.TD TD( Tags.ColSpan colSpan, params Tags.Tag[] children ) { return TD( null, colSpan, null, children ); }
public static Tags.TD TD( Tags.RowSpan rowSpan, params Tags.Tag[] children ) { return TD( null, null, rowSpan, children ); }
public static Tags.TD TD( params Tags.Tag[] children ) { return TD( null, null, null, children ); }
#endregion
#region TH
public static Tags.TH TH( Tags.Class className, Tags.ColSpan colSpan, Tags.RowSpan rowSpan, params Tags.Tag[] children ) { return new Tags.TH( className, colSpan, rowSpan, children ); }
public static Tags.TH TH( Tags.Class className, Tags.ColSpan colSpan, params Tags.Tag[] children ) { return TH( className, colSpan, null, children ); }
public static Tags.TH TH( Tags.Class className, Tags.RowSpan rowSpan, params Tags.Tag[] children ) { return TH( className, null, rowSpan, children ); }
public static Tags.TH TH( Tags.ColSpan colSpan, Tags.RowSpan rowSpan, params Tags.Tag[] children ) { return TH( null, colSpan, rowSpan, children ); }
public static Tags.TH TH( Tags.Class className, params Tags.Tag[] children ) { return TH( className, null, null, children ); }
public static Tags.TH TH( Tags.ColSpan colSpan, params Tags.Tag[] children ) { return TH( null, colSpan, null, children ); }
public static Tags.TH TH( Tags.RowSpan rowSpan, params Tags.Tag[] children ) { return TH( null, null, rowSpan, children ); }
public static Tags.TH TH( params Tags.Tag[] children ) { return TH( null, null, null, children ); }
#endregion
#region TR
public static Tags.TR<Tags.TD> TR( Tags.Class className, params Tags.TD[] children ) { return new Tags.TR<Tags.TD>( className, children ); }
public static Tags.TR<Tags.TD> TR( params Tags.TD[] children ) { return new Tags.TR<Tags.TD>( children ); }
public static Tags.TR<Tags.TH> TR( Tags.Class className, params Tags.TH[] children ) { return new Tags.TR<Tags.TH>( className, children ); }
public static Tags.TR<Tags.TH> TR( params Tags.TH[] children ) { return new Tags.TR<Tags.TH>( children ); }
#endregion
#region THead / TBody
public static Tags.THead THead( Tags.Class className, params Tags.TR<Tags.TH>[] children ) { return new Tags.THead( className, children ); }
public static Tags.THead THead( params Tags.TR<Tags.TH>[] children ) { return THead( null, children ); }
public static Tags.TBody TBody( Tags.Class className, params Tags.TR<Tags.TD>[] children ) { return new Tags.TBody( className, children ); }
public static Tags.TBody TBody( params Tags.TR<Tags.TD>[] children ) { return TBody( null, children ); }
#endregion
#region Table
public static Tags.Table Table( Tags.Class className, Tags.THead head, Tags.TBody body ) { return new Tags.Table( className, head, body ); }
public static Tags.Table Table( Tags.Class className, Tags.THead head ) { return Table( className, head, null ); }
public static Tags.Table Table( Tags.Class className, Tags.TBody body ) { return Table( className, null, body ); }
public static Tags.Table Table( Tags.THead head, Tags.TBody body ) { return Table( null, head, body ); }
public static Tags.Table Table( Tags.Class className ) { return Table( className, (Tags.THead)null, null ); }
public static Tags.Table Table( Tags.THead head ) { return Table( null, head, null ); }
public static Tags.Table Table( Tags.TBody body ) { return Table( null, null, body ); }
public static Tags.Table Table( Tags.Class className, params Tags.TR<Tags.TD>[] children ) { return Table( className, TBody( children ) ); }
public static Tags.Table Table( params Tags.TR<Tags.TD>[] children ) { return Table( TBody( children ) ); }
#endregion
#region Content Tags (Div, P, A)
public static Tags.Div Div( Tags.Class className, params Tags.Tag[] children ) { return new Tags.Div( className, children ); }
public static Tags.Div Div( params Tags.Tag[] children ) { return Div( null, children ); }
public static Tags.Div Div( Tags.Class className, string content ) { return new Tags.Div( className, content ); }
public static Tags.Div Div( string content ) { return Div( null, content ); }
public static Tags.Span Span( Tags.Class className, params Tags.Tag[] children ) { return new Tags.Span( className, children ); }
public static Tags.Span Span( params Tags.Tag[] children ) { return Span( null, children ); }
public static Tags.Span Span( Tags.Class className, string content ) { return new Tags.Span( className, content ); }
public static Tags.Span Span( string content ) { return Span( null, content ); }
public static Tags.P P( Tags.Class className, params Tags.Tag[] children ) { return new Tags.P( className, children ); }
public static Tags.P P( params Tags.Tag[] children ) { return P( null, children ); }
public static Tags.P P( Tags.Class className, string content ) { return new Tags.P( className, content ); }
public static Tags.P P( string content ) { return P( null, content ); }
#endregion
#region Img
public static Tags.Img Img( Tags.Class className, Tags.Src source ) { return new Tags.Img( className, source ); }
public static Tags.Img Img( Tags.Src source ) { return Img( null, source ); }
#endregion
#region A
public static Tags.A A( Tags.Class className, Tags.Href href, params Tags.Tag[] children ) { return new Tags.A( className, href, children ); }
public static Tags.A A( Tags.Class className, Tags.Href href, string content ) { return new Tags.A( className, href, content ); }
public static Tags.A A( Tags.Class className, Tags.Name name, params Tags.Tag[] children ) { return new Tags.A( className, name,children ); }
public static Tags.A A( Tags.Class className, Tags.Name name, string content ) { return new Tags.A( className, name, content ); }
public static Tags.A A( Tags.Href href, params Tags.Tag[] children ) { return A( null, href, children ); }
public static Tags.A A( Tags.Href href, string content ) { return A( null, href, content ); }
public static Tags.A A( Tags.Name name, params Tags.Tag[] children ) { return A( null, name, children ); }
public static Tags.A A( Tags.Name name, string content ) { return A( null, name, content ); }
#endregion
#region Head Tags
public static Tags.Meta Meta( Tags.Name name, Tags.Content content ) { return new Tags.Meta( name, content ); }
#endregion
#region Top Level Tags
public static Tags.Head Head( string title, params Tags.HeadTag[] children ) { return new Tags.Head( title, children ); }
public static Tags.Head Head( params Tags.HeadTag[] children ) { return new Tags.Head( children ); }
public static Tags.Body Body( params Tags.Tag[] children ) { return new Tags.Body( children ); }
public static Tags.Html Html( Tags.Head head, Tags.Body body ) { return new Tags.Html( head, body ); }
public static Tags.Html Html( Tags.Body body ) { return Html( null, body ); }
public static Tags.Document CreateDocument( Tags.Html html ) { return new Tags.Document( html ); }
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment