Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

An Application to Create Interesting and Fully Customizable Web Photo Gallery

Rate me:
Please Sign up or sign in to vote.
4.47/5 (16 votes)
4 Apr 2007LGPL32 min read 55.8K   4.6K   81  
An application to create interesting and fully customizable Web photo gallery using plugins for generating pages
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;
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Web Developer
Italy Italy
I'm an electronic engeneer form Genoa.
I like very much dotnet and C#. I've developed some works like an html gallery generator, a graphic formula viewer (user defined), a webcam grabber and html gallery generator, a little framework for image processing, an image resizer, elaboration in batch and some other application tool.
Actually I'm working in VC++ 6 MFC, but I hope that my future is .net !!!
Some of my works are downloadable at:
http://www.solchiere.it
Lorenzo Banderali

Comments and Discussions