As it’s written somewhere here, web sites, web files, HTTP protocol and all about and above it is being used and developed daily. Web is for now large and important part of Mad Library. It contains managing of web files, web directories, web pages, web sites, HTML documents, CSS files, etc. and it works good. Out web “toolkit” (:D) is simple and usable. It’s not based on .NET’s System.Xml, it’s our own creation.

5.1 HTML

HTML is markup language for web pages which are being transferred by HTTP protocol. It’s similar to XML and by the years it started to be more freely in writing and it changed in a lot things. There where added new tags which are now supported by most of modern web browsers. Mad Library is primary and mostly working with classic XHTML 1.0 Strict, so when you’re trying to parse some strings or files into HTML classes in library, be sure that your strings and files are complying basic important rules of that version. So here is small example of that what you can do with HTML in Mad Library.

HtmlDocument doc = new HtmlDocument(); //creating new HTML document
HtmlElement html = new HtmlElement("html", ElementType.Compound); //creating root element
html.Elements.Add(new HtmlElement("head", ElementType.Compound));
HtmlElement body = new HtmlElement("body", ElementType.Compound);
html.Elements.Add(body);
HtmlElement div = new HtmlElement("div", ElementType.Compound);
div.InnerText = "HELLO WORLD!";
div.Attributes.Add(new HtmlAttribute("style", "background-color: red;color: black;font-weight: bold;"));
body.Elements.Add(div);
doc.Elements.Add(html);
doc.Save("C:\\Web\\index.html");

Output:

<html>

  <head>
  
  </head>
  <body>
  
    <div style="background-color: red;color: black;font-weight: bold;">
    HELLO WORLD!
    </div>
  </body>
</html>

5.2 CSS

CSS is style sheet language which is being daily used by web designers. It’s often used with markup languages and it has simple syntax with simple rules. For now, lastest version of CSS is CSS 3.0 with more delicate and harder functions for making web page more beautiful. Mad Library contains simple small parser and builder for this language, so you can use it and you don’t have to code all parsing processes. Down is small example of CSS management in Mad Library.

CssDocument doc = new CssDocument();
CssStyle style = new CssStyle(new CssGroup("div", CssGroupType.Element));
style.Properties.Add(new CssProperty("background-color", "black"), 
    new CssProperty("color", "white"));
CssStyle hover = new CssStyle(new CssGroup[]{new CssGroup("div", CssGroupType.Element)}, 
    new CssGroup("hover", CssGroupType.PseudoClass));
hover.Properties.Add(new CssProperty("background-color", "white"), 
    new CssProperty("color", "black"));
doc.Styles.Add(style, hover);
doc.Save("C:\\Web\\main.css");

Output:

div
{
  background-color: black;
  color: white;
}

div:hover
{
  background-color: white;
  color: black;
}

 

5.3 Web pages, sites, directories and files

All web documents specified in Mad Library are derived from WebFile class which contains basic properties common for this kind of documents. There’s also WebDirectory which loads files of web pages by HTTP call path with WebPage class. And there’s also WebSite class which is usable for management of some portal or some portfolio, simply – for web site management. Here’s small example:

HtmlDocument doc = new HtmlDocument("index");
CssDocument css = new CssDocument("main");
WebDirectory directory = new WebDirectory("Web", doc, css);
WebPage page = new WebPage(doc, css);
WebSite site = new WebSite(directory, "C:\\Web");
CssDocument ncss = site.GetStyles("main.css");
Console.WriteLine(css.Name == ncss.Name);

Output:

True

Last edited Jun 17, 2012 at 6:03 PM by FrewCen, version 6

Comments

No comments yet.