using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Text.RegularExpressions;
namespace Html
{
public class CHtmlParser
{
/// <summary>
/// Constructor
/// </summary>
public CHtmlParser()
{
properties = new Properties();
}
private Properties properties;
/// <summary>
/// Class to store usefull properties of the template
/// </summary>
private class Properties
{
public Color BackColor = Color.Transparent;
public Color TextColor = Color.Transparent;
public Color LinkColor = Color.Transparent;
public Color VLinkColor = Color.Transparent;
public Color ALinkColor = Color.Transparent;
public Properties()
{
}
}
/// <summary>
/// Converts the input color to hex value
/// </summary>
/// <param name="aColor"></param>
/// <returns></returns>
public String ColorToHex(Color aColor)
{
int R = aColor.R;
int G = aColor.G;
int B = aColor.B;
uint uiDecimal;
uiDecimal = checked((uint)System.Convert.ToUInt32(R));
string RR = String.Format("{0:x2}", uiDecimal);
uiDecimal = checked((uint)System.Convert.ToUInt32(G));
string GG = String.Format("{0:x2}", uiDecimal);
uiDecimal = checked((uint)System.Convert.ToUInt32(B));
string BB = String.Format("{0:x2}", uiDecimal);
string RGB = "#" + RR.ToUpper() + GG.ToUpper() + BB.ToUpper();
return (RGB);
}
/// <summary>
/// Converts a Hex value to Color
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
public Color HextToColor(string hex)
{
if (hex.Length != 6) return Color.Black;
string rs = hex.Substring(0, 2);
string gs = hex.Substring(2, 2);
string bs = hex.Substring(4, 2);
int r, g, b;
r = g = b = 0;
try
{
r = int.Parse(rs, System.Globalization.NumberStyles.HexNumber);
}
catch
{
r = 0;
}
try
{
g = int.Parse(gs, System.Globalization.NumberStyles.HexNumber);
}
catch
{
g = 0;
}
try
{
b = int.Parse(bs, System.Globalization.NumberStyles.HexNumber);
}
catch
{
b = 0;
}
Color col = Color.FromArgb(r, g, b);
return col;
}
/// <summary>
/// Gets Body properties background
/// </summary>
/// <returns></returns>
public Color GetBackgroundColor(List<string> BeforeTag)
{
if (properties.BackColor != Color.Transparent) return properties.BackColor;
bool findedbody = false;
System.Text.RegularExpressions.Regex rxbk = new Regex("bgcolor=\"\\#[(0-9|A-F)]{6}\"", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
System.Text.RegularExpressions.Regex rxb = new Regex("body", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
foreach (string str in BeforeTag)
{
// Check the presence of "body"
if (rxb.IsMatch(str, 0)) findedbody = true;
// try the match
if (rxbk.IsMatch(str, 0) && (findedbody))
{
// If match
Match match = rxbk.Match(str);
string value = match.Value;
int start = value.IndexOf("#", 0);
string hex = value.Substring(start + 1, 6);
Color bkcolor = HextToColor(hex);
properties.BackColor = bkcolor;
return bkcolor;
}
}
return Color.Transparent;
}
/// <summary>
/// Gets Body properties background
/// </summary>
/// <returns></returns>
public Color GetTextColor(List<string> BeforeTag)
{
if (properties.TextColor != Color.Transparent) return properties.TextColor;
bool findedbody = false;
System.Text.RegularExpressions.Regex rxbk = new Regex("bgcolor=\"\\#[(0-9|A-F)]{6}\"", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
System.Text.RegularExpressions.Regex rxb = new Regex("body", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
foreach (string str in BeforeTag)
{
// Check the presence of "body"
if (rxb.IsMatch(str, 0)) findedbody = true;
// try the match
if (rxbk.IsMatch(str, 0) && (findedbody))
{
// If match
Match match = rxbk.Match(str);
string value = match.Value;
int start = value.IndexOf("#", 0);
string hex = value.Substring(start + 1, 6);
Color bkcolor = HextToColor(hex);
properties.TextColor = bkcolor;
return bkcolor;
}
}
return Color.Transparent;
}
/// <summary>
/// Gets Body properties link
/// </summary>
/// <returns></returns>
public Color GetLinkColor(List<string> BeforeTag)
{
if (properties.LinkColor != Color.Transparent) return properties.LinkColor;
bool findedbody = false;
System.Text.RegularExpressions.Regex rxl = new Regex("link=\"\\#[(0-9|A-F)]{6}\"", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
System.Text.RegularExpressions.Regex rxb = new Regex("body", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
foreach (string str in BeforeTag)
{
// Check the presence of "body"
if (rxb.IsMatch(str, 0)) findedbody = true;
// try the match
if (rxl.IsMatch(str, 0) && (findedbody))
{
// If match
Match match = rxl.Match(str);
string value = match.Value;
int start = value.IndexOf("#", 0);
string hex = value.Substring(start + 1, 6);
Color lcolor = HextToColor(hex);
properties.LinkColor = lcolor;
return lcolor;
}
}
return Color.Transparent;
}
/// <summary>
/// Gets Body properties vlink
/// </summary>
/// <returns></returns>
public Color GetVLinkColor(List<string> BeforeTag)
{
if (properties.VLinkColor != Color.Transparent) return properties.VLinkColor;
bool findedbody = false;
System.Text.RegularExpressions.Regex rxl = new Regex("vlink=\"\\#[(0-9|A-F)]{6}\"", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
System.Text.RegularExpressions.Regex rxb = new Regex("body", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
foreach (string str in BeforeTag)
{
// Check the presence of "body"
if (rxb.IsMatch(str, 0)) findedbody = true;
// try the match
if (rxl.IsMatch(str, 0) && (findedbody))
{
// If match
Match match = rxl.Match(str);
string value = match.Value;
int start = value.IndexOf("#", 0);
string hex = value.Substring(start + 1, 6);
Color lcolor = HextToColor(hex);
properties.VLinkColor = lcolor;
return lcolor;
}
}
return Color.Transparent;
}
/// <summary>
/// Gets Body properties alink
/// </summary>
/// <returns></returns>
public Color GetALinkColor(List<string> BeforeTag)
{
if (properties.ALinkColor != Color.Transparent) return properties.ALinkColor;
bool findedbody = false;
System.Text.RegularExpressions.Regex rxl = new Regex("alink=\"\\#[(0-9|A-F)]{6}\"", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
System.Text.RegularExpressions.Regex rxb = new Regex("body", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
foreach (string str in BeforeTag)
{
// Check the presence of "body"
if (rxb.IsMatch(str, 0)) findedbody = true;
// try the match
if (rxl.IsMatch(str, 0) && (findedbody))
{
// If match
Match match = rxl.Match(str);
string value = match.Value;
int start = value.IndexOf("#", 0);
string hex = value.Substring(start + 1, 6);
Color lcolor = HextToColor(hex);
properties.ALinkColor = lcolor;
return lcolor;
}
}
return Color.Transparent;
}
/// <summary>
/// Return true if line is a metatag
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
public bool IsMeta(string line)
{
bool finded = false;
System.Text.RegularExpressions.Regex rxm = new Regex("meta", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
// Check the presence of "meta"
if (rxm.IsMatch(line, 0)) finded= true;
return finded;
}
/// <summary>
/// Return true if line is the head of document
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
public bool IsHead(string line)
{
bool finded = false;
System.Text.RegularExpressions.Regex rxm = new Regex("<head>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
// Check the presence of "head"
if (rxm.IsMatch(line, 0)) finded = true;
return finded;
}
/// <summary>
/// Return true if line is page title
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
public bool IsTitle(string line)
{
bool finded = false;
System.Text.RegularExpressions.Regex rxm = new Regex("<title>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
// Check the presence of "title"
if (rxm.IsMatch(line, 0)) finded = true;
return finded;
}
/// <summary>
/// Return true if line is a metatag
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
public bool IsBody(string line)
{
bool finded = false;
System.Text.RegularExpressions.Regex rxm = new Regex("<body", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
// Check the presence of "body"
if (rxm.IsMatch(line, 0)) finded = true;
return finded;
}
}
}