Saturday, July 31, 2010

How to convert HTML files to aspx files automatically - Html file converter for asp.net

Please note: This tool aim to create a asp.net web application based on html files provided. 

Download Installer - (X86)
Download Source
Download .EXE File (X86) Only 
  1. Download and install HtmlAspxFileConverter
  2. Run from the link provided in the Programmes (HtmlAspxFileConverter)
  3. Select a source direcotry where you have static html files css java scripts and other files
  4. Select a destination folder. If destination folder excists programme deletes the folder without any prompt and if the folder does not exists creates a new folder with the give path or in a case of invalid path provide you an error
  5. Enter an namespace for the asp.net web application
  6. Click on convet button.
  7. Open up visual studio and select new project
  8. Select Web tab and select Asp.net Empty Web Application
  9. Provide suitable name and click ok
  10. Change the namespace/assembly name with the name space provide to convert html in HtmlAspxFileConverter
  11. Copy all the files convertered by HtmlAspxFileConverter to the WebApplication folder. 
  12. In the solution explorer click on show all files option at the top
  13. Select non included aspx pages and just copied other folders and files and rightclick
  14. Select Include In Project option 
    For VB please change the source and run the programme
    Source:
    public partial class HtmlAspxFileConverter : Form
    {
        #region Attributes
     
        string strNameSpace = string.Empty;
        string head = @"
            <head runat=""server"">
                {0}
            </head>
        ";
        string body = @"
            <body{0}>
                <form id=""form1"" runat=""server"">
                    {1}
                </form>
            </body>
        ";   
        private string code = @"
            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Web;
            using System.Web.UI;
            using System.Web.UI.WebControls;
     
            namespace {0}
            {{
                public partial class {1} : Page
                {{
                    protected void Page_Load(object sender, EventArgs e)
                    {{
     
                    }}
                }}
            }}
        ";
        private string designer = @"
            namespace {0}
            {{
                public partial class {1}
                {{
                }}
            }}
        ";
        private string header = @"<%@ Page Language=\""C#\"" AutoEventWireup=\""true\"" 
                                CodeBehind=\""{0}.cs\"" Inherits=\""{1}.{2}\"" %>";
        string[] keywords = { "abstract""event""new""struct""as""explicit""null switch""base", 
                                "extern""object""this""bool""false""operator""throw""break", 
                                "finally""out""true""byte""fixed""""override""try", 
                                "case""float""params""typeof""catch""for""private""uint",
                                "char""foreach""protected""ulong""checkek","goto""public", 
                                "unchecked""class""if""readonly""unsafe""const""implicit", 
                                "ref""ushort""continue""in""return""using""decimal""int", 
                                "sbyte""virtual""default""interface""sealed""volatile""delegate",
                                "internal""short""void""do""is""sizeof""while""double""lock",
                                "stackalloc""else""long""static""enum""namespace""string" };
     
        #endregion
     
        #region Constructors
     
        public HtmlAspxFileConverter()
        {
            InitializeComponent();
        }
     
        #endregion
     
        #region Support Methods
     
        private void ConverHtmlFileToAspxFiles(DirectoryInfo o, DirectoryInfo n)
        {
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo textInfo = cultureInfo.TextInfo;
            foreach (FileInfo f in o.GetFiles())
            {
                string nfn = f.FullName.Replace(o.FullName, n.FullName);
                nfn = nfn.Replace(f.Name, textInfo.ToTitleCase(f.Name));
                if (f.Extension.ToLower().Equals(".html") || f.Extension.ToLower().Equals(".htm"))
                    this.SaveAsAspx(f, new FileInfo(nfn));
                else
                    File.Copy(f.FullName, nfn);
            }
            foreach (DirectoryInfo c in o.GetDirectories())
            {
                string ndn = c.FullName.Replace(o.FullName, n.FullName);
                ndn = ndn.Replace(c.Name, textInfo.ToTitleCase(c.Name));
                Directory.CreateDirectory(ndn);
                this.ConverHtmlFileToAspxFiles(c, new DirectoryInfo(ndn));
            }
        }
        private void SaveAsAspx(FileInfo h, FileInfo a)
        {
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo textInfo = cultureInfo.TextInfo;
            string fn =  a.FullName;
            string fileName=string.Empty;
            if(fn.Contains("html"))fileName = fn.Replace("html""aspx");
            else if (fn.Contains("Html")) fileName = fn.Replace("Html""aspx");
            else if (fn.Contains("htm")) fileName = fn.Replace("htm""aspx");
            else if (fn.Contains("Htm")) fileName = fn.Replace("Htm""aspx");  
            string classFileName = new FileInfo(fileName).Name;
            string currentFolder = fileName.Replace(classFileName,string.Empty);
            string className = string.Empty;
            if (a.Name.Contains(h.Extension)) className = a.Name.Replace(h.Extension, string.Empty);
            else
            {
                string ext = textInfo.ToTitleCase(h.Extension);
                className = a.Name.Replace(ext, string.Empty);
            }
            if (keywords.ToList<string>().Exists(x => x.Equals(className.ToLower()))) 
                className = string.Format("_{0}", className);
            File.WriteAllText(Path.Combine(currentFolder, string.Format("{0}.cs", 
                classFileName)), string.Format(code, this.strNameSpace, className));
            File.WriteAllText(Path.Combine(currentFolder, string.Format("{0}.designer.cs", 
                classFileName)), string.Format(designer, this.strNameSpace, className));
            string html = File.ReadAllText(h.FullName);
            using (StreamWriter stream = new StreamWriter(fileName))
            {                
                stream.WriteLine(string.Format(header, classFileName, this.strNameSpace, className));
                stream.WriteLine("<html>");
                int s = html.ToLower().IndexOf(">", html.ToLower().IndexOf("<head")) + 1;
                int e = html.ToLower().IndexOf("</head") - 1;
                string headContent = e - s > 0 ? html.Substring(s, e - s) : string.Empty;
                stream.Write(string.Format(head, headContent));                
                s = html.ToLower().IndexOf("<body") + 5;
                e = html.IndexOf(">", s);
                string bodyAttributes = string.Empty;
                if (s != e && e > s) bodyAttributes = html.Substring(s, e - s);
                if (bodyAttributes.Contains(">") || bodyAttributes.Contains(">")) 
                    bodyAttributes = string.Empty;                
                s = html.ToLower().IndexOf(">", html.ToLower().IndexOf("<body")) + 1;
                e = html.ToLower().IndexOf("</body") - 1;
                string bodyContent = e - s > 0 ? html.Substring(s, e - s) : string.Empty;
                string[] exts = { "html","Html","htm","Htm"};
                foreach (string ext in exts) bodyContent = bodyContent.Replace(ext, "aspx");
                stream.Write(string.Format(body, bodyAttributes, bodyContent));
                stream.WriteLine("</html>");
                stream.Close();
            }
                
        }
        private void btnConvert_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.txtNameSpace.Text)) throw new Exception();
                this.strNameSpace = this.txtNameSpace.Text;
                DirectoryInfo s = new DirectoryInfo(this.txtSource.Text);
                if (!s.Exists) { MessageBox.Show("Destination folder doesnot exists"); return; }
                DirectoryInfo d = new DirectoryInfo(this.txtDestiation.Text);
                if (d.Exists) this.DeleteFolder(d);
                Directory.CreateDirectory(d.FullName);
                this.toolStripStatusLabel1.Text = "Converting, please wait...";
                this.SetControlStatus(false);
                this.ConverHtmlFileToAspxFiles(s, d);
                this.SetControlStatus(true);
                this.toolStripStatusLabel1.Text = "Conversion Finished.";
            }
            catch
            {
                MessageBox.Show("There is a problem");
            }
     
     
        }
        private void SetControlStatus(bool status)
        {
            this.txtSource.Enabled = 
            this.txtDestiation.Enabled = 
            this.txtNameSpace.Enabled = 
            this.btnConvert.Enabled = 
            this.btnSelectDestination.Enabled = 
            this.btnSelectSource.Enabled = status;
        }
        public void DeleteFolder(DirectoryInfo d)
        {
            foreach (FileInfo f in d.GetFiles())
                File.Delete(f.FullName);
            foreach (DirectoryInfo c in d.GetDirectories())
                this.DeleteFolder(c);
            Directory.Delete(d.FullName);
        }
     
        #endregion
    }

    No comments:

    iPhone Launch Screen Sizes

    iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...