Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Added all source
Added build
  • Loading branch information
hughbe committed Sep 26, 2015
1 parent 673e66d commit 8eeda70
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/HtmlGenerator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HtmlGenerator", "HtmlGenerator\HtmlGenerator.csproj", "{0B79B05A-95B5-4315-9E62-228021F96413}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0B79B05A-95B5-4315-9E62-228021F96413}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B79B05A-95B5-4315-9E62-228021F96413}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B79B05A-95B5-4315-9E62-228021F96413}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B79B05A-95B5-4315-9E62-228021F96413}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
22 changes: 22 additions & 0 deletions src/HtmlGenerator/HtmlDocument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace HtmlGenerator
{
public class HtmlDocument : HtmlElement
{
public HtmlDocument(HtmlHead head, HtmlElement body) : base("html")
{
Head = Add(head ?? new HtmlHead());
Body = Add(body ?? new HtmlElement("body"));
}

public HtmlDocument() : this(null, null)
{
}

public HtmlDocument(string title) : this(new HtmlHead(title), null)
{
}

public HtmlHead Head { get; }
public HtmlElement Body { get; }
}
}
95 changes: 95 additions & 0 deletions src/HtmlGenerator/HtmlElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace HtmlGenerator
{
public class HtmlElement
{
public HtmlElement(string elementTag)
{
ElementTag = elementTag;
}

public HtmlElement WithChildren(params HtmlElement[] children)
{
Children = new Collection<HtmlElement>(children);
return this;
}

public HtmlElement WithClass(string className) => WithAttribute("class", className);
public HtmlElement WithId(string idName) => WithAttribute("id", idName);

public HtmlElement WithAttribute(string key, string value)
{
Attributes.Add(key, value);
return this;
}

public HtmlElement WithAttributes(Dictionary<string, string> attributes)
{
Attributes = attributes;
return this;
}

public HtmlElement WithContent(string content)
{
Content = content;
return this;
}

public string ElementTag { get; }

public Dictionary<string, string> Attributes { get; private set; } = new Dictionary<string, string>();

public string Content { get; private set; }

public HtmlElement Parent { get; internal set; }
public Collection<HtmlElement> Children { get; private set; } = new Collection<HtmlElement>();

public T Add<T>(T element) where T : HtmlElement
{
Children.Add(element);
element.Parent = this;
return element;
}

public string Serialize()
{
var openingTag = SerializeOpenTag();
var closingTag = "</" + ElementTag + ">";

var content = Content ?? "";
foreach (var child in Children)
{
content += child.Serialize();
}

if (string.IsNullOrEmpty(ElementTag))
{
return content;
}
return openingTag + content + closingTag;
}

private string SerializeOpenTag()
{
var tagOpener = "<" + ElementTag;
var tagCloser = ">";

if (Attributes == null || Attributes.Count == 0)
{
return tagOpener + tagCloser;
}

var attributes = " ";
foreach (var attribute in Attributes)
{
attributes += attribute.Key + "=" + "\"" + attribute.Value + "\" ";
}

return tagOpener + attributes + tagCloser;
}

public override string ToString() => SerializeOpenTag();
}
}
51 changes: 51 additions & 0 deletions src/HtmlGenerator/HtmlGenerator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0B79B05A-95B5-4315-9E62-228021F96413}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HtmlGenerator</RootNamespace>
<AssemblyName>HtmlGenerator</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="HtmlDocument.cs" />
<Compile Include="HtmlElement.cs" />
<Compile Include="HtmlHead.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tags.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
15 changes: 15 additions & 0 deletions src/HtmlGenerator/HtmlHead.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace HtmlGenerator
{
public class HtmlHead : HtmlElement
{
public HtmlHead(string title = null) : base("head")
{
if (title != null)
{
Title = Add(Tags.Title.WithContent(title));
}
}

public HtmlElement Title { get; set; }
}
}
39 changes: 39 additions & 0 deletions src/HtmlGenerator/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HtmlGenerator")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HtmlGenerator")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

[assembly: CLSCompliant(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0b79b05a-95b5-4315-9e62-228021f96413")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
140 changes: 140 additions & 0 deletions src/HtmlGenerator/Tags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
namespace HtmlGenerator
{
public static class Tags
{
//Root
public static HtmlElement Html => new HtmlElement("html");

//Meta
public static HtmlElement Head => new HtmlElement("head");
public static HtmlElement Title => new HtmlElement("title");

public static HtmlElement Script => new HtmlElement("script"); //Uses attributes (async, integrity, src, type, text, defer, crossorigin)
public static HtmlElement Noscript => new HtmlElement("noscript");
public static HtmlElement Canvas => new HtmlElement("canvas"); //Uses attributes (height, width)

//Structure
public static HtmlElement Address => new HtmlElement("address");
public static HtmlElement Article => new HtmlElement("article");
public static HtmlElement Body => new HtmlElement("body");
public static HtmlElement Div => new HtmlElement("div");
public static HtmlElement Footer => new HtmlElement("footer");
public static HtmlElement Header => new HtmlElement("header");
public static HtmlElement Main => new HtmlElement("main");
public static HtmlElement Nav => new HtmlElement("nav");
public static HtmlElement Section => new HtmlElement("section");
public static HtmlElement Span => new HtmlElement("span");
public static HtmlElement Template => new HtmlElement("template"); //Unsupported

//Seperators
public static HtmlElement Br => new HtmlElement("br");
public static HtmlElement Hr => new HtmlElement("hr"); //Uses attributes (color)
public static HtmlElement Wbr => new HtmlElement("wbr");

//Lists
public static HtmlElement Dd => new HtmlElement("dd"); //Uses attributes (nowrap)
public static HtmlElement Dl => new HtmlElement("dl"); //Uses attributes (compact)
public static HtmlElement Dt => new HtmlElement("dt");

public static HtmlElement Li => new HtmlElement("li"); //Uses attributes (value)
public static HtmlElement Ol => new HtmlElement("ol"); //Uses attributes (reversed, start, type)
public static HtmlElement Ul => new HtmlElement("ul");

//Text
public static HtmlElement Anchor => new HtmlElement("a"); //Uses attributes (download, href, hreflang, media, ping, rel, target, type)

public static HtmlElement H1 => new HtmlElement("h1");
public static HtmlElement H2 => new HtmlElement("h2");
public static HtmlElement H3 => new HtmlElement("h3");
public static HtmlElement H4 => new HtmlElement("h4");
public static HtmlElement H5 => new HtmlElement("h5");
public static HtmlElement H6 => new HtmlElement("h6");

public static HtmlElement P => new HtmlElement("p");

//Displaying External Content
public static HtmlElement Cite => new HtmlElement("cite");
public static HtmlElement Data => new HtmlElement("data"); //Uses attributes (value); unsupported
public static HtmlElement Dfn => new HtmlElement("dfn");
public static HtmlElement Figure => new HtmlElement("figure");
public static HtmlElement FigCaption => new HtmlElement("figcaption");
public static HtmlElement Q => new HtmlElement("q"); //Uses attributes (cite)
public static HtmlElement Sub => new HtmlElement("sub");
public static HtmlElement Sup => new HtmlElement("sup");
public static HtmlElement Time => new HtmlElement("time"); //Uses attributes (datetime)

//Formatting
public static HtmlElement Abbr => new HtmlElement("abbr");
public static HtmlElement B => new HtmlElement("b"); //LEGACY
public static HtmlElement Code => new HtmlElement("code");
public static HtmlElement Del => new HtmlElement("del"); //Uses attributes (cite, datetime)
public static HtmlElement Em => new HtmlElement("em");
public static HtmlElement I => new HtmlElement("i");
public static HtmlElement Ins => new HtmlElement("ins"); //Uses attributes (cite, datetime)
public static HtmlElement Kbd => new HtmlElement("kbd");
public static HtmlElement Mark => new HtmlElement("mark");
public static HtmlElement Pre => new HtmlElement("pre");
public static HtmlElement Samp => new HtmlElement("samp");
public static HtmlElement Small => new HtmlElement("small");
public static HtmlElement Strong => new HtmlElement("strong");
public static HtmlElement U => new HtmlElement("u");
public static HtmlElement Var => new HtmlElement("var");

//Localisation
public static HtmlElement Bdi => new HtmlElement("bdi");
public static HtmlElement Rp => new HtmlElement("rp");
public static HtmlElement Ruby => new HtmlElement("ruby");
public static HtmlElement Rt => new HtmlElement("rt");
public static HtmlElement Rtc => new HtmlElement("rtc");

//Multimedia
public static HtmlElement Area => new HtmlElement("area"); //Uses attributes (alt, coords, download, href, hreflang, media, rel, shape, target, type)
public static HtmlElement Audio => new HtmlElement("audio"); //Uses attributes (autoplay, buffered, controls, loop, muted, played, preload, src, volume)
public static HtmlElement Img => new HtmlElement("img"); //Uses attributes (alt, crossorigin, height, ismap, longdesc, sizes, src, srcset, width, usemap)
public static HtmlElement Map => new HtmlElement("img"); //Uses attributes (name)
public static HtmlElement Track => new HtmlElement("img"); //Uses attributes (default, kind, label, src, srclang)
public static HtmlElement Video => new HtmlElement("video"); //Uses attributes (autoplay, buffers, controls, crossorigin, height, loop, muted, played, preload, poster, src, width)

//Embedding
public static HtmlElement Embed => new HtmlElement("embed"); //Uses attributes (height, src, type, width)
public static HtmlElement Iframe => new HtmlElement("iframe"); //Uses attributes (allowfullscreen, height, name, sandbox, seamless, src, srcdoc, width)
public static HtmlElement Object => new HtmlElement("object"); //Uses attributes (data, form, height, height, name, type, typemustmatch, usemap, width)
public static HtmlElement Param => new HtmlElement("param"); //Uses attributes (name, value)
public static HtmlElement Source => new HtmlElement("source"); //Uses attributes (src, type)

//Table
public static HtmlElement Caption => new HtmlElement("caption");
public static HtmlElement Col => new HtmlElement("col"); //Uses attributes (span)
public static HtmlElement Colgroup => new HtmlElement("colgroup"); //Uses attributes (span)
public static HtmlElement Table => new HtmlElement("table");
public static HtmlElement Tbody => new HtmlElement("tbody");
public static HtmlElement Tfoot => new HtmlElement("tfoot");
public static HtmlElement Th => new HtmlElement("th"); //Uses attributes (colspan, headers, rowspan, scope)
public static HtmlElement Thead => new HtmlElement("thead");
public static HtmlElement Tr => new HtmlElement("tr");

//Forms
public static HtmlElement Button => new HtmlElement("button"); //Uses attributes (autofocus, disabled, form, formaction, formenctype, formmethod, formnovalidate, formtarget, name, type, value)
public static HtmlElement Datalist => new HtmlElement("datalist");
public static HtmlElement Fieldset => new HtmlElement("fieldset"); //Uses attributes (disabled, form, name)
public static HtmlElement Form => new HtmlElement("form"); //Uses attributes (accept-charset, action, autocomplete, enctype, method, name, novalidate, target)
public static HtmlElement Input => new HtmlElement("input"); //Uses attributes (type, accept, autocomplete, autofocus, autosave, checked, disabled, form, formaction, formenctype, formmethod, formnovalidate, formtarget, height, inputmode, list, max, maxlength, min, minLength, multiple, name, pattern, placeholder, readonly, required, selectionDirection, size, spellCheck, src, step, tabIndex, value, width)
public static HtmlElement Label => new HtmlElement("label"); //Uses attributes (accesskey, for, form)
public static HtmlElement Legend => new HtmlElement("legend");
public static HtmlElement Meter => new HtmlElement("meter"); //Uses attributes (value, min, max, low, high, optimum, form)
public static HtmlElement Optgroup => new HtmlElement("optgroup"); //Uses attributes (disabled, label)
public static HtmlElement Option => new HtmlElement("option"); //Uses attributes (disabled, label, selected, value)
public static HtmlElement Output => new HtmlElement("output"); //Uses attributes (for, form, name)
public static HtmlElement Progress => new HtmlElement("progress"); //Uses attributes (max, value)
public static HtmlElement Select => new HtmlElement("select"); //Uses attributes (autofocus, disabled, form, multiple, name, required, size)
public static HtmlElement Textarea => new HtmlElement("textarea"); //Uses attributes (autocomplete, autofocus, cols, disabled, form, maxlength, minLength, name, placeholder, readonly, required, rows, selectiondirection, selectionend, selectionstart, spellcheck, wrap)

//Interactivity
public static HtmlElement Details => new HtmlElement("details"); //Uses attributes (open); Unsupported
public static HtmlElement Dialog => new HtmlElement("dialog"); //Uses attributes (open); Unsupported
public static HtmlElement Menu => new HtmlElement("menu"); //Uses attributes (label, type); Unsupported
public static HtmlElement MenuItem => new HtmlElement("menutiem"); //Uses attributes (checked, command, default, disabled, icon, label, radiogroup, type); Unsupported
public static HtmlElement Summary => new HtmlElement("summary"); //Unsupported

}
}

0 comments on commit 8eeda70

Please sign in to comment.