Example #1
0
        /// <summary>
        /// The first pass: Process each file, grabbing the type, names, and IDs, along with compacting the HTML.
        /// </summary>
        /// <returns>The changed files names, with the key as the old name and value as the new name.</returns>
        Dictionary<string, string> Pass1()
        {
            Log("***** PASS 1 *****");

            var changedFileNames = new Dictionary<string, string>();
            var htmlComp = new HtmlCompacter(RootDir);

            var files = Directory.GetFiles(RootDir, "*.html", SearchOption.AllDirectories);
            foreach (var f in files)
            {
                // Skip source files for now
                if (f.EndsWith("_source.html"))
                    continue;

                // Read contents
                var html = File.ReadAllText(f);

                // Figure out what type of file this is
                string name;
                var type = GetFileType(html, out name);

                // Compact the HTML and re-save it - using the ID for the name if needed
                html = htmlComp.Compact(html);

                if (type != FileType.Unknown)
                {
                    var dir = Path.GetDirectoryName(f);
                    if (dir == null)
                        throw new Exception();

                    var oldName = Path.GetFileName(f);
                    if (oldName == null)
                        throw new Exception();

                    var newName = GetFileName(name, type);
                    var newPath = Path.Combine(dir, newName);

                    if (f != newPath)
                    {
                        Debug.Assert(type == FileType.Unknown || !File.Exists(newPath));
                        File.WriteAllText(newPath, html);
                        File.Delete(f);
                    }

                    changedFileNames.Add(oldName, newName);

                    Log("Moved: {0} -> {1}", oldName, newName);

                    // For file reference files, also change the source file
                    if (type == FileType.File)
                    {
                        var srcFileOldName = oldName.Substring(0, oldName.Length - ".html".Length) + "_source.html";
                        var srcFileNewName = newName.Substring(0, newName.Length - ".html".Length) + "-s.html";

                        Debug.Assert(!File.Exists(Path.Combine(dir, srcFileNewName)));
                        File.Move(Path.Combine(dir, srcFileOldName), Path.Combine(dir, srcFileNewName));
                        changedFileNames.Add(srcFileOldName, srcFileNewName);
                        Log("Moved: {0} -> {1}", srcFileOldName, srcFileNewName);
                    }
                }
                else
                {
                    // No file renaming
                    File.WriteAllText(f, html);
                }
            }

            return changedFileNames;
        }
Example #2
0
        /// <summary>
        /// The first pass: Process each file, grabbing the type, names, and IDs, along with compacting the HTML.
        /// </summary>
        /// <returns>The changed files names, with the key as the old name and value as the new name.</returns>
        Dictionary <string, string> Pass1()
        {
            Log("***** PASS 1 *****");

            var changedFileNames = new Dictionary <string, string>();
            var htmlComp         = new HtmlCompacter(RootDir);

            var files = Directory.GetFiles(RootDir, "*.html", SearchOption.AllDirectories);

            foreach (var f in files)
            {
                // Skip source files for now
                if (f.EndsWith("_source.html"))
                {
                    continue;
                }

                // Read contents
                var html = File.ReadAllText(f);

                // Figure out what type of file this is
                string name;
                var    type = GetFileType(html, out name);

                // Compact the HTML and re-save it - using the ID for the name if needed
                html = htmlComp.Compact(html);

                if (type != FileType.Unknown)
                {
                    var dir = Path.GetDirectoryName(f);
                    if (dir == null)
                    {
                        throw new Exception();
                    }

                    var oldName = Path.GetFileName(f);
                    if (oldName == null)
                    {
                        throw new Exception();
                    }

                    var newName = GetFileName(name, type);
                    var newPath = Path.Combine(dir, newName);

                    if (f != newPath)
                    {
                        Debug.Assert(type == FileType.Unknown || !File.Exists(newPath));
                        File.WriteAllText(newPath, html);
                        File.Delete(f);
                    }

                    changedFileNames.Add(oldName, newName);

                    Log("Moved: {0} -> {1}", oldName, newName);

                    // For file reference files, also change the source file
                    if (type == FileType.File)
                    {
                        var srcFileOldName = oldName.Substring(0, oldName.Length - ".html".Length) + "_source.html";
                        var srcFileNewName = newName.Substring(0, newName.Length - ".html".Length) + "-s.html";

                        Debug.Assert(!File.Exists(Path.Combine(dir, srcFileNewName)));
                        File.Move(Path.Combine(dir, srcFileOldName), Path.Combine(dir, srcFileNewName));
                        changedFileNames.Add(srcFileOldName, srcFileNewName);
                        Log("Moved: {0} -> {1}", srcFileOldName, srcFileNewName);
                    }
                }
                else
                {
                    // No file renaming
                    File.WriteAllText(f, html);
                }
            }

            return(changedFileNames);
        }